src/Component/Core/Shop/Listener/MailerListener.php line 48

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace BitBag\OpenMarketplace\Component\Core\Shop\Listener;
  4. use BitBag\OpenMarketplace\Component\Brevo\Emails;
  5. use BitBag\OpenMarketplace\Component\Customer\Model\CustomerInterface;
  6. use BitBag\OpenMarketplace\Component\Organization\Entity\OrganizationInterface;
  7. use BitBag\OpenMarketplace\Component\Vendor\Entity\ShopUserInterface;
  8. use BitBag\OpenMarketplace\Component\Vendor\Entity\VendorInterface;
  9. use Sylius\Component\Mailer\Sender\SenderInterface;
  10. use Symfony\Component\EventDispatcher\GenericEvent;
  11. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  12. use Symfony\Component\Routing\RouterInterface;
  13. final class MailerListener
  14. {
  15.     public function __construct(
  16.         private SenderInterface $emailSender,
  17.         private RouterInterface $router,
  18.     ) {
  19.     }
  20.     public function sendResetPasswordTokenEmail(GenericEvent $event): void
  21.     {
  22.         $user $event->getSubject();
  23.         if (!$user instanceof ShopUserInterface) {
  24.             return;
  25.         }
  26.         $url $this->router->generate(
  27.             'sylius_shop_password_reset',
  28.             ['token' => $user->getPasswordResetToken()],
  29.             UrlGeneratorInterface::ABSOLUTE_PATH,
  30.         );
  31.         $this->emailSender->send(
  32.             Emails::PASSWORD_RESET,
  33.             [$user->getEmail()],
  34.             [
  35.                 'user' => $user,
  36.                 'url' => $url,
  37.             ],
  38.         );
  39.     }
  40.     public function sendVerificationTokenEmail(GenericEvent $event): void
  41.     {
  42.         $user $event->getSubject();
  43.         if (!$user instanceof ShopUserInterface) {
  44.             return;
  45.         }
  46.         $url $this->router->generate(
  47.             'sylius_shop_user_verification',
  48.             ['token' => $user->getEmailVerificationToken()],
  49.             UrlGeneratorInterface::ABSOLUTE_PATH,
  50.         );
  51.         $this->emailSender->send(
  52.             $this->resolveEmailCode($user),
  53.             [$user->getEmail()],
  54.             [
  55.                 'user' => $user,
  56.                 'url' => $url,
  57.             ],
  58.         );
  59.     }
  60.     public function sendAccountUnderVerificationEmail(GenericEvent $event): void
  61.     {
  62.         $user $event->getSubject();
  63.         if (!$user instanceof ShopUserInterface) {
  64.             return;
  65.         }
  66.         if (false === $this->isVendorAccount($user)) {
  67.             return;
  68.         }
  69.         $this->emailSender->send(
  70.             Emails::ACCOUNT_UNDER_VERIFICATION,
  71.             [$user->getEmail()],
  72.         );
  73.     }
  74.     private function resolveEmailCode(ShopUserInterface $user): string
  75.     {
  76.         return true === $this->isVendorAccount($user) ?
  77.             Emails::SELLER_REGISTRATION :
  78.             Emails::BUYER_REGISTRATION;
  79.     }
  80.     private function isVendorAccount(ShopUserInterface $user): bool
  81.     {
  82.         $organization $this->getOrganization($user);
  83.         if (!$organization instanceof OrganizationInterface) {
  84.             return false;
  85.         }
  86.         return $organization->getVendor() instanceof VendorInterface;
  87.     }
  88.     private function getOrganization(ShopUserInterface $user): ?OrganizationInterface
  89.     {
  90.         $customer $user->getCustomer();
  91.         if (!$customer instanceof CustomerInterface) {
  92.             return null;
  93.         }
  94.         $organization $customer->getOrganization();
  95.         if (!$organization instanceof OrganizationInterface) {
  96.             return null;
  97.         }
  98.         return $organization;
  99.     }
  100. }