custom/static-plugins/K3nFindConsultant/src/Storefront/Subscriber/ValidateSubscriber.php line 60

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace K3n\K3nFindConsultant\Storefront\Subscriber;
  4. use GuzzleHttp\Client;
  5. use GuzzleHttp\Psr7\Request;
  6. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  7. use Shopware\Core\Checkout\Order\Aggregate\OrderAddress\OrderAddressCollection;
  8. use Shopware\Core\Checkout\Order\Aggregate\OrderAddress\OrderAddressEntity;
  9. use Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent;
  10. use Shopware\Core\Checkout\Order\OrderEntity;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\Event\EventData\MailRecipientStruct;
  14. use Shopware\Core\System\StateMachine\Event\StateMachineStateChangeEvent;
  15. use Shopware\Core\System\SystemConfig\SystemConfigService;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  18. use VioRepresentativeLogin\Entity\Agent\AgentEntity;
  19. use VioRepresentativeLogin\VioRepresentativeLogin;
  20. class ValidateSubscriber implements EventSubscriberInterface
  21. {
  22.     /** @var EntityRepositoryInterface */
  23.     private $orderRepository;
  24.     /** @var SystemConfigService */
  25.     private $systemConfigService;
  26.     /**
  27.      * @var Client
  28.      */
  29.     private $client;
  30.     private EntityRepositoryInterface $agentRepository;
  31.     public function __construct(
  32.         EntityRepositoryInterface $orderRepository,
  33.         SystemConfigService $systemConfigService,
  34.         EntityRepositoryInterface $agentRepository
  35.     ) {
  36.         $this->orderRepository     $orderRepository;
  37.         $this->systemConfigService $systemConfigService;
  38.         $this->client              = new Client();
  39.         $this->agentRepository $agentRepository;
  40.     }
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return [
  44.             CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlaced',
  45.         ];
  46.     }
  47.     /**
  48.      * @param CheckoutOrderPlacedEvent $event
  49.      */
  50.     public function onCheckoutOrderPlaced(CheckoutOrderPlacedEvent $event): void
  51.     {
  52.         // skip if order already has a agent, because a mail to the
  53.         // rep will be send in this case from K3nExtendVioRepresentativeLogin.
  54.         // This function here a just needed for orders from farmers.
  55.         if($this->orderHasAgent($event->getOrder())) {
  56.             return;
  57.         }
  58.         $isDebugMode $this->systemConfigService->get('K3nFindConsultant.config.debugMode');
  59.         if ($isDebugMode) {
  60.             $consultantEmail $this->systemConfigService->get('K3nFindConsultant.config.debugModeEmail');
  61.             if(!$consultantEmail) {
  62.                 return;
  63.             }
  64.             $consultantName  'Test';
  65.         } else {
  66.             /** @var OrderEntity $order */
  67.             $order $event->getOrder();
  68.             /** @var OrderAddressCollection $adresses */
  69.             $adresses $order->getAddresses();
  70.             $zipcode '';
  71.             /** @var OrderAddressEntity $address */
  72.             foreach ($adresses as $address) {
  73.                 if ($address->getZipcode()) {
  74.                     $zipcode $address->getZipcode();
  75.                     break;
  76.                 }
  77.             }
  78.             if (!$zipcode) {
  79.                 return;
  80.             }
  81.             $host 'www.lgseeds.pl/storage-salesrep/api?search='.$zipcode;
  82.             $request = new Request(
  83.                 'GET',
  84.                 $host,
  85.                 [
  86.                     'Content-Type' => 'application/json',
  87.                     'Accept: application/json'
  88.                 ]
  89.             );
  90.             $response $this->client->send($request);
  91.             $response $response->getBody()->getContents();
  92.             $resultArray json_decode($responsetrue);
  93.             if (!$resultArray['success'] or empty($resultArray['data'])) {
  94.                 return;
  95.             }
  96.             $consultantEmail $resultArray['data'][0]['contact']['email'];
  97.             $consultantName  $resultArray['data'][0]['name'];
  98.         }
  99.         if(!$consultantEmail) {
  100.             return;
  101.         }
  102.         $this->assignAgentToOrder($consultantEmail$event);
  103.         /** @var MailRecipientStruct  $mailStruct */
  104.         $mailStruct $event->getMailStruct();
  105.         $recipientArray $mailStruct->getRecipients();
  106.         $recipientArray[$consultantEmail] = $consultantName;
  107.         $mailStruct->setRecipients($recipientArray);
  108.     }
  109.     public function assignAgentToOrder(string $agentEmailCheckoutOrderPlacedEvent $event)
  110.     {
  111.         // skip if order already has a agent
  112.         if($this->orderHasAgent($event->getOrder())) {
  113.             return;
  114.         }
  115.         $criteria = new Criteria();
  116.         $criteria->addFilter(new EqualsFilter('email'$agentEmail));
  117.         /** @var AgentEntity|null $agent */
  118.         $agent $this->agentRepository
  119.             ->search($criteria$event->getContext())
  120.             ->first();
  121.         if ($agent && $agent instanceof AgentEntity) {
  122.             $this->orderRepository->update([[
  123.                 'id' => $event->getOrderId(),
  124.                 'customFields' => [VioRepresentativeLogin::AGENT_FIELD => $agent->getId()]
  125.             ]], $event->getContext());
  126.         }
  127.     }
  128.     public function orderHasAgent(OrderEntity $order): bool
  129.     {
  130.         $customFields $order->getCustomFields();
  131.         return is_array($customFields) && array_key_exists(VioRepresentativeLogin::AGENT_FIELD$customFields) && $customFields[VioRepresentativeLogin::AGENT_FIELD];
  132.     }
  133. }