src/Component/Organization/EventListener/OrganizationManagementEventListener.php line 33

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace BitBag\OpenMarketplace\Component\Organization\EventListener;
  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\Organization\OrganizationRoles;
  8. use BitBag\OpenMarketplace\Component\Vendor\Entity\ShopUserInterface;
  9. use BitBag\SyliusB2BPlugin\Repository\UserRepositoryInterface;
  10. use Sylius\Component\Core\Context\ShopperContextInterface;
  11. use Sylius\Component\Mailer\Sender\SenderInterface;
  12. use Sylius\Component\User\Security\Generator\GeneratorInterface;
  13. use Symfony\Component\EventDispatcher\GenericEvent;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. use Symfony\Component\Routing\RouterInterface;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. final class OrganizationManagementEventListener
  18. {
  19.     public function __construct(
  20.         private GeneratorInterface $tokenGenerator,
  21.         private UserRepositoryInterface $userRepository,
  22.         private SenderInterface $emailSender,
  23.         private RouterInterface $router,
  24.         private ShopperContextInterface $shopperContext,
  25.         private TranslatorInterface $translator,
  26.         ) {
  27.     }
  28.     public function onAddUser(GenericEvent $event): void
  29.     {
  30.         $customer $event->getSubject();
  31.         if (!$customer instanceof CustomerInterface) {
  32.             return;
  33.         }
  34.         $user $customer->getUser();
  35.         if (!$user instanceof ShopUserInterface) {
  36.             return;
  37.         }
  38.         $organization $customer->getOrganization();
  39.         if (!$organization instanceof OrganizationInterface) {
  40.             return;
  41.         }
  42.         /** @var CustomerInterface $inviter */
  43.         $inviter $this->shopperContext->getCustomer();
  44.         $customer->setInviter($inviter);
  45.         $this->sendActivationEmail($user$organization);
  46.     }
  47.     private function sendActivationEmail(ShopUserInterface $userOrganizationInterface $organization): void
  48.     {
  49.         $token $this->tokenGenerator->generate();
  50.         $user->setActivationToken($token);
  51.         $this->userRepository->add($user);
  52.         $emailCode null !== $organization->getVendor() ?
  53.             Emails::ADD_USER_TO_SELLER_ORGANIZATION :
  54.             Emails::ADD_USER_TO_BUYER_ORGANIZATION;
  55.         $url $this->router->generate(
  56.             'bitbag_sylius_organization_plugin_shop_active_user',
  57.             ['token' => $user->getActivationToken()],
  58.             UrlGeneratorInterface::ABSOLUTE_PATH,
  59.         );
  60.         $this->emailSender->send(
  61.             $emailCode,
  62.             [$user->getEmail()],
  63.             [
  64.                 'user' => $user,
  65.                 'organization' => $organization,
  66.                 'url' => $url,
  67.                 'localeCode' => $this->shopperContext->getLocaleCode(),
  68.                 'inviter' => $this->shopperContext->getCustomer(),
  69.                 'roles' => $this->resolveRoles($user),
  70.             ],
  71.         );
  72.     }
  73.     private function resolveRoles(ShopUserInterface $user): array
  74.     {
  75.         $roles = [];
  76.         foreach ($user->getRoles() as $role) {
  77.             if (in_array($roleOrganizationRoles::IGNORED_ROLEStrue)) {
  78.                 continue;
  79.             }
  80.             $roles[] = $this->translator->trans(sprintf('app.ui.roles.%s'strtolower($role)));
  81.         }
  82.         return $roles;
  83.     }
  84. }