custom/plugins/SwagB2bPlatform/components/EasyMode/BridgePlatform/B2bCustomerDataSubscriber.php line 44

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\B2B\EasyMode\BridgePlatform;
  3. use Shopware\B2B\Acl\Framework\AclRepository;
  4. use Shopware\B2B\Common\IdValue;
  5. use Shopware\B2B\Common\Repository\NotFoundException;
  6. use Shopware\B2B\Debtor\Framework\DebtorService;
  7. use Shopware\B2B\Role\Framework\RoleRepository;
  8. use Shopware\B2B\Shop\BridgePlatform\B2bCustomerDataDefinition;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use function array_key_exists;
  12. class B2bCustomerDataSubscriber implements EventSubscriberInterface
  13. {
  14.     private RoleRepository $roleRepository;
  15.     private EasyModeService $easyModeService;
  16.     private AclRepository $aclRoleRepository;
  17.     private DebtorService $debtorService;
  18.     public function __construct(
  19.         RoleRepository $roleRepository,
  20.         EasyModeService $easyModeService,
  21.         AclRepository $aclRoleRepository,
  22.         DebtorService $debtorService
  23.     ) {
  24.         $this->roleRepository $roleRepository;
  25.         $this->easyModeService $easyModeService;
  26.         $this->aclRoleRepository $aclRoleRepository;
  27.         $this->debtorService $debtorService;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             B2bCustomerDataDefinition::ENTITY_NAME '.written' => 'updateEasyModeAcl',
  33.         ];
  34.     }
  35.     public function updateEasyModeAcl(EntityWrittenEvent $event): void
  36.     {
  37.         $writtenResults $event->getPayloads();
  38.         $identities = [];
  39.         foreach ($writtenResults as $customerData) {
  40.             $customerId IdValue::create($customerData['customerId']);
  41.             $isInEasyMode array_key_exists('isInEasyMode'$customerData) && $customerData['isInEasyMode'] === true;
  42.             if ($isInEasyMode === false) {
  43.                 continue;
  44.             }
  45.             try {
  46.                 $identity $this->debtorService->loadIdentityByAuthentication($customerId);
  47.             } catch (NotFoundException $exception) {
  48.                 continue;
  49.             }
  50.             $identities[] = $identity;
  51.         }
  52.         if (!$identities) {
  53.             return;
  54.         }
  55.         foreach ($identities as $identity) {
  56.             $easyModeRole $this->easyModeService->getEasyModeRole($identity->getOwnershipContext(), $this->roleRepository);
  57.             $this->aclRoleRepository->allow($easyModeRole$easyModeRole->id);
  58.         }
  59.     }
  60. }