custom/plugins/CioBudget/src/Subscriber/CustomerGroupsLoadedSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. namespace CioBudget\Subscriber;
  3. use CioBudget\Service\BudgetLoaderService;
  4. use CioBudget\Service\SessionService;
  5. use CioCustomerPermissionGroups\Event\CustomerGroupsLoadedEvent;
  6. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class CustomerGroupsLoadedSubscriber implements EventSubscriberInterface
  10. {
  11.     private SessionService $sessionService;
  12.     private EntityRepositoryInterface $customerRepository;
  13.     private BudgetLoaderService $budgetLoaderService;
  14.     public function __construct(EntityRepositoryInterface $customerRepository,
  15.                                 SessionService $sessionService,
  16.                                 BudgetLoaderService $budgetLoaderService)
  17.     {
  18.         $this->budgetLoaderService $budgetLoaderService;
  19.         $this->sessionService $sessionService;
  20.         $this->customerRepository $customerRepository;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             CustomerGroupsLoadedEvent::class => 'onCustomerGroupsLoadedEvent'
  26.         ];
  27.     }
  28.     public function onCustomerGroupsLoadedEvent(CustomerGroupsLoadedEvent $event)
  29.     {
  30.         $groups $event->getGroups();
  31.         $currentBudgetId $this->sessionService->getCurrentBudgetId();
  32.         if (is_array($event->getCustomer()->getCustomFields()) && key_exists('cio_stores'$event->getCustomer()->getCustomFields()) && is_array($event->getCustomer()->getCustomFields()['cio_stores'])) {
  33.             $currentBudget null;
  34.             // find current budget in all budgets of the customer
  35.             foreach ($event->getCustomer()->getCustomFields()['cio_stores'] as $customerStore) {
  36.                 if (is_array($customerStore) && key_exists('id'$customerStore)) {
  37.                     $storeBudgetId $this->budgetLoaderService->getBudgetIdByStoreId($customerStore['id']);
  38.                     if ($storeBudgetId == $currentBudgetId) {
  39.                         // store data with ACL becomes budget data
  40.                         $currentBudget $customerStore;
  41.                         $currentBudget['id'] = $storeBudgetId;
  42.                         break;
  43.                     }
  44.                 }
  45.             }
  46.             // iterate over all aclgroups of the current selected budget of the customer
  47.             if ($currentBudget && $currentBudget['aclgroup']) {
  48.                 if (is_array($currentBudget['aclgroup'])) {
  49.                     foreach ($currentBudget['aclgroup'] as $aclgroup) {
  50.                         $groups[] = $aclgroup;
  51.                     }
  52.                 }
  53.             }
  54.         }
  55.         $event->setGroups($groups);
  56.     }
  57. }