custom/plugins/TdsMerwareDiscounts/src/Core/Exchange/Import/Customer/Subscriber/CustomersReadSubscriber.php line 71

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Tds\MerwareDiscounts\Core\Exchange\Import\Customer\Subscriber;
  3. use Shopware\Core\Checkout\Customer\CustomerEntity;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Tds\Merware\Core\Exchange\Import\Event\Customer\CustomersReadEvent;
  12. use Tds\Merware\Core\Exchange\Import\Struct\Customer\CustomerStruct;
  13. /**
  14.  * Class CustomersReadSubscriber
  15.  *
  16.  * @package Tds\MerwareDiscounts\Core\Exchange\Import\Customer\Subscriber
  17.  */
  18. class CustomersReadSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var SystemConfigService
  22.      */
  23.     private $systemConfigService;
  24.     /**
  25.      * @var EntityRepositoryInterface
  26.      */
  27.     private $customerRepository;
  28.     /**
  29.      * @return array
  30.      */
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             CustomersReadEvent::class => 'onCustomersRead'
  35.         ];
  36.     }
  37.     /**
  38.      * CustomersReadSubscriber constructor
  39.      *
  40.      * @param SystemConfigService $systemConfigService
  41.      * @param EntityRepositoryInterface $customerRepository
  42.      */
  43.     public function __construct(SystemConfigService $systemConfigServiceEntityRepositoryInterface $customerRepository)
  44.     {
  45.         $this->systemConfigService $systemConfigService;
  46.         $this->customerRepository $customerRepository;
  47.     }
  48.     /**
  49.      * Function to catch imported (but maybe unsaved customers).
  50.      *
  51.      * @param CustomersReadEvent $event
  52.      *
  53.      * @throws InconsistentCriteriaIdsException
  54.      */
  55.     public function onCustomersRead(CustomersReadEvent $event)
  56.     {
  57.         $customerStructs $event->getCustomerStructs();
  58.         if (!empty($customerStructs)) {
  59.             $this->updateCustomerDiscounts($event->getContext(), $customerStructs);
  60.         }
  61.     }
  62.     /**
  63.      * Function to update customer discounts.
  64.      *
  65.      * @param Context $context
  66.      * @param CustomerStruct[] $customerStructs
  67.      */
  68.     private function updateCustomerDiscounts($context$customerStructs)
  69.     {
  70.         $requests $this->buildCustomerRequests($context$customerStructs);
  71.         if (!empty($requests)) {
  72.             $this->customerRepository->update($requests$context);
  73.         }
  74.     }
  75.     /**
  76.      * Helper-function to build a list of DAL requests to update customer custom fields.
  77.      *
  78.      * @param Context $context
  79.      * @param CustomerStruct[] $customerStructs
  80.      *
  81.      * @return array
  82.      */
  83.     private function buildCustomerRequests($context$customerStructs)
  84.     {
  85.         $requests = [];
  86.         foreach ($customerStructs as $customerStruct) {
  87.             $email $customerStruct->getEmail();
  88.             if (!empty($email)) {
  89.                 $customer $this->getCustomer($context$email);
  90.                 if (!empty($customer)) {
  91.                     $customFields $customer->getCustomFields();
  92.                     $customFields['tds_merware_customer_discount_general'] = $customerStruct->getGeneralDiscount();
  93.                     $customFields['tds_merware_customer_discount_products'] = $this->buildDiscountedProductList($customerStruct);
  94.                     $requests[] = [
  95.                         'id' => $customer->getId(),
  96.                         'customFields' => $customFields
  97.                     ];
  98.                 }
  99.             }
  100.         }
  101.         return $requests;
  102.     }
  103.     /**
  104.      * Helper-function to build a JSON encoded string with a list of product discounts:
  105.      *
  106.      * - list
  107.      * -- artcode
  108.      * -- price
  109.      *
  110.      * @param CustomerStruct $customerStruct
  111.      *
  112.      * @return string
  113.      */
  114.     private function buildDiscountedProductList($customerStruct)
  115.     {
  116.         $list = [];
  117.         $productDiscounts $customerStruct->getProductDiscounts();
  118.         if (!empty($productDiscounts)) {
  119.             foreach ($productDiscounts as $productDiscount) {
  120.                 $list[] = [
  121.                     'artcode' => $productDiscount->getArtcode(),
  122.                     'price' => $productDiscount->getPrice()
  123.                 ];
  124.             }
  125.         }
  126.         return json_encode($list);
  127.     }
  128.     /**
  129.      * Helper-function to load a customer.
  130.      *
  131.      * @param Context $context
  132.      * @param string $email
  133.      *
  134.      * @return CustomerEntity|null
  135.      */
  136.     private function getCustomer($context$email)
  137.     {
  138.         $criteria = new Criteria();
  139.         $criteria->addFilter(
  140.             new EqualsFilter('email'$email)
  141.         );
  142.         $result $this->customerRepository->search($criteria$context)->first();
  143.         return $result;
  144.     }
  145. }