custom/plugins/CompraEsiSW6/src/Export/Subscriber/CustomerSubscriber.php line 73

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Compra\EsiSW6\Export\Subscriber;
  3. use Compra\EsiSW6\Core\System\Service\ApiService;
  4. use Shopware\Core\Checkout\Customer\CustomerEvents;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class CustomerSubscriber implements EventSubscriberInterface
  9. {
  10.     protected EntityRepository $customerRepo;
  11.     protected EntityRepository $customerAddressRepo;
  12.     protected ApiService $apiService;
  13.     public const RELEVANT_CUSTOMER_FIELDS = [
  14.         'salutationId',
  15.         'firstName',
  16.         'lastName',
  17.         'company',
  18.         'email',
  19.         'birthday'
  20.     ];
  21.     /** @var array  */
  22.     public const RELEVANT_ADDRESS_FIELDS = [
  23.         'countryId',
  24.         'countryStateId',
  25.         'company',
  26.         'department',
  27.         'salutationId',
  28.         'title',
  29.         'firstName',
  30.         'lastName',
  31.         'street',
  32.         'zipcode',
  33.         'city',
  34.         'vatId',
  35.         'phoneNumber',
  36.         'additionalAddressLine1',
  37.         'additionalAddressLine2',
  38.     ];
  39.     public function __construct(
  40.         EntityRepository $customerRepo,
  41.         EntityRepository $customerAddressRepo,
  42.         ApiService $apiService
  43.     ) {
  44.         $this->customerRepo $customerRepo;
  45.         $this->customerAddressRepo $customerAddressRepo;
  46.         $this->apiService $apiService;
  47.     }
  48.     /**
  49.      * @return string[]
  50.      */
  51.     public static function getSubscribedEvents(): array
  52.     {
  53.         return [
  54.             CustomerEvents::CUSTOMER_WRITTEN_EVENT => 'onCustomerWritten',
  55.             CustomerEvents::CUSTOMER_ADDRESS_WRITTEN_EVENT => 'onCustomerAddressWritten',
  56.         ];
  57.     }
  58.     /**
  59.      * mark the customer as to be exported when there are relevant changes
  60.      *
  61.      * @param EntityWrittenEvent $event
  62.      */
  63.     public function onCustomerWritten(EntityWrittenEvent $event): void
  64.     {
  65.         $payloads $event->getPayloads();
  66.         $admin $this->apiService->getAdminUser($event->getContext());
  67.         foreach ($payloads as $payload) {
  68.             if ($admin !== ApiService::ESI_API_USER && $this->isRelevantCustomerChange($payload)) {
  69.                 $data = [
  70.                     'id' => $payload['id'],
  71.                     'customFields' => [
  72.                         'compra_esi_xml_export' => true,
  73.                     ],
  74.                 ];
  75.                 $this->customerRepo->update([$data], $event->getContext());
  76.             }
  77.         }
  78.     }
  79.     /**
  80.      * mark the customer address as to be exported when there are relevant changes
  81.      *
  82.      * @param EntityWrittenEvent $event
  83.      */
  84.     public function onCustomerAddressWritten(EntityWrittenEvent $event): void
  85.     {
  86.         $payloads $event->getPayloads();
  87.         $admin $this->apiService->getAdminUser($event->getContext());
  88.         foreach ($payloads as $payload) {
  89.             if ($admin !== ApiService::ESI_API_USER && $this->isRelevantAddressChange($payload)) {
  90.                 $extend = [
  91.                     'id' => $payload['id'],
  92.                     'customerId' => $payload['customerId'], // added customer number to fix error thrown by B2B suite due to missing test
  93.                     'customFields' => ['compra_esi_xml_export' => true],
  94.                 ];
  95.                 $updateData $this->customerAddressRepo->update([$extend], $event->getContext());
  96.                 $extend = [
  97.                     'id' => $payload['customerId'],
  98.                     'customFields' => [
  99.                         'compra_esi_xml_export_address' => true,
  100.                     ],
  101.                 ];
  102.                 $updateData $this->customerRepo->update([$extend], $event->getContext());
  103.             }
  104.         }
  105.     }
  106.     /**
  107.      * Only some changes are interesting for the export.
  108.      * For example a change on the last login is not interesting. Especially we need to avoid an endless loop when saving the export custom field marker.
  109.      *
  110.      * @param $payload
  111.      * @return bool
  112.      */
  113.     private function isRelevantCustomerChange($payload): bool
  114.     {
  115.         if (!isset($payload['id'])
  116.             || empty($payload['id'])
  117.             || empty(\array_intersect(self::RELEVANT_CUSTOMER_FIELDS, \array_keys($payload)))) {
  118.             return false;
  119.         }
  120.         return true;
  121.     }
  122.     /**
  123.      * Only some changes are interesting for the export.
  124.      * For example a change on the last login is not interesting. Especially we need to avoid an endless loop when saving the export custom field marker.
  125.      *
  126.      * @param $payload
  127.      * @return bool
  128.      */
  129.     private function isRelevantAddressChange($payload): bool
  130.     {
  131.         if (!isset($payload['id'])
  132.             || empty($payload['id'])
  133.             || empty(\array_intersect(self::RELEVANT_ADDRESS_FIELDS, \array_keys($payload)))) {
  134.             return false;
  135.         }
  136.         return true;
  137.     }
  138. }