custom/plugins/OkeonlineKejeLogin/src/Core/Checkout/Customer/Subscriber/CustomerEntityWrittenSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. namespace Okeonline\KejeLogin\Core\Checkout\Customer\Subscriber;
  3. use Doctrine\DBAL\Connection;
  4. use Okeonline\KejeLogin\Core\Checkout\Customer\Exception\LoginIdentifierNotUniqueException;
  5. use Shopware\Core\Checkout\Customer\CustomerEvents;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  11. use Shopware\Core\System\SystemConfig\SystemConfigService;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class CustomerEntityWrittenSubscriber implements EventSubscriberInterface
  14. {
  15.     protected $systemConfigService;
  16.     protected $customerRepository;
  17.     protected $connection;
  18.     public function __construct(
  19.         SystemConfigService $systemConfigService,
  20.         EntityRepository $customerRepository,
  21.         Connection $connection
  22.     )
  23.     {
  24.         $this->systemConfigService $systemConfigService;
  25.         $this->customerRepository $customerRepository;
  26.         $this->connection $connection;
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             CustomerEvents::CUSTOMER_WRITTEN_EVENT => 'customerEntityWritten'
  32.         ];
  33.     }
  34.     public function customerEntityWritten(EntityWrittenEvent $event)
  35.     {
  36.         // check if module is active
  37.         if(!$this->systemConfigService->get('OkeonlineKejeLogin.config.active'))
  38.         {
  39.             return;
  40.         }
  41.         $identifier $this->systemConfigService->get('OkeonlineKejeLogin.config.identifier') ?? 'customer.customerNumber';
  42.         $namespace explode('.'$identifier);
  43.         $identifier array_pop($namespace);
  44.         foreach($event->getWriteResults() as $writeResult)
  45.         {
  46.             $writeKey $writeResult->getPrimaryKey();
  47.             // Is there any $identifier key changed? No? Continue
  48.             if(!array_key_exists($identifier$writeResult->getPayload()))
  49.             {
  50.                 continue;
  51.             }
  52.             $identifierValue $writeResult->getPayload()[$identifier];
  53.             // search repository, where not id, and is $identifier = $identifierValue
  54.             $criteria = new Criteria();
  55.             $criteria->addFilter(new EqualsFilter($identifier$identifierValue));
  56.             $criteria->addFilter(new NotFilter(
  57.                 NotFilter::CONNECTION_AND,
  58.                 [
  59.                     new EqualsFilter('id'$writeKey)
  60.                 ]
  61.             ));
  62.             $result $this->customerRepository->search($criteria$event->getContext());
  63.             // Other entities with same $identifier?
  64.             if($result->getTotal() !== 0)
  65.             {
  66.                 $this->connection->executeQuery(
  67.                     'UPDATE `customer` SET `'.$this->_decamelize($identifier).'` = "'.$identifierValue.'_notUnique" WHERE `id` = UNHEX(\''.$writeKey.'\');'
  68.                 );
  69.                 throw new LoginIdentifierNotUniqueException($identifier$identifierValue);
  70.             }
  71.             unset($writeKey$identifierValue);
  72.         }
  73.     }
  74.     protected function _decamelize($string) {
  75.         return strtolower(preg_replace(['/([a-z\d])([A-Z])/''/([^_])([A-Z][a-z])/'], '$1_$2'$string));
  76.     }
  77. }
  78. ?>