custom/static-plugins/SamsonAmpc/src/Subscriber/AssetManagementCustomerSubscriber.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Samson\Subscriber;
  3. /***
  4.  *
  5.  * This file is part of the "SAMSON Shop" project.
  6.  *
  7.  * For the full copyright and license information, please read the
  8.  * LICENSE.txt file that was distributed with this source code.
  9.  *
  10.  *  (c) 2024
  11.  *
  12.  ***/
  13. use Shopware\Core\Checkout\Customer\CustomerEntity;
  14. use Shopware\Core\Checkout\Customer\CustomerEvents;
  15. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class AssetManagementCustomerSubscriber implements EventSubscriberInterface
  20. {
  21.     private EntityRepositoryInterface $customerRepository;
  22.     public function __construct(EntityRepositoryInterface $customerRepository)
  23.     {
  24.         $this->customerRepository $customerRepository;
  25.     }
  26.     /**
  27.      * @inheritDoc
  28.      */
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [CustomerEvents::CUSTOMER_LOADED_EVENT => 'addCorporation'];
  32.     }
  33.     public function addCorporation(EntityLoadedEvent $event)
  34.     {
  35.         if (empty($event->getEntities())) {
  36.             return;
  37.         }
  38.         $customer $event->getEntities()[0];
  39.         if (is_null($customer->getExtension("corporations"))) {
  40.             $criteria = new Criteria([$customer->getId()]);
  41.             $criteria->addAssociation('corporations');
  42.             /** @var CustomerEntity $customerEntity */
  43.             $customerEntity $this->customerRepository->search($criteria$event->getContext())->first();
  44.             $corporations $customerEntity->getExtension('corporations');
  45.             $customer->addExtension('corporations'$corporations);
  46.         }
  47.     }
  48. }