custom/plugins/WbfkExtensions/src/Subscriber/LogOrderChanges.php line 39

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WbfkExtensions\Subscriber;
  4. use Doctrine\DBAL\Connection;
  5. use Doctrine\DBAL\Exception;
  6. use Psr\Log\LoggerInterface;
  7. use Shopware\Core\Checkout\Order\OrderCollection;
  8. use Shopware\Core\Checkout\Order\OrderDefinition;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\BeforeDeleteEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class LogOrderChanges implements EventSubscriberInterface
  17. {
  18.     public function __construct(
  19.         private readonly SystemConfigService $systemConfigService,
  20.         private readonly LoggerInterface $logger,
  21.         private readonly EntityRepositoryInterface $orderRepository,
  22.         private readonly Connection $connection,
  23.     ) {
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             // BeforeDeleteEvent::class => 'beforeDelete',
  29.             // OrderEvents::ORDER_WRITTEN_EVENT => 'onOrderWritten',
  30.             PreWriteValidationEvent::class => 'preWriteValidation',
  31.         ];
  32.     }
  33.     public function preWriteValidation(PreWriteValidationEvent $event): void
  34.     {
  35.         if (!$this->systemConfigService->get('WbfkExtensions.config.loggingOrder')) {
  36.             return;
  37.         }
  38.         $ctxExt $event->getContext()->getExtension(self::class);
  39.         if (isset($ctxExt['allowToOpen']) && $ctxExt['allowToOpen']) {
  40.             return;
  41.         }
  42.         foreach ($event->getCommands() as $command) {
  43.             if ($command->getEntityName() === 'order') {
  44.                 $payloads $command->getPayload();
  45.                 $exist $command->getEntityExistence();
  46.                 if ($exist->exists() && isset($payloads['state_id']) && $payloads['state_id'] === hex2bin('d50aa676dd7049a39bbef1dd9ac2fdcb')) {
  47.                     if ($this->getOrderState($command->getPrimaryKey()) === 'd50aa676dd7049a39bbef1dd9ac2fdcb') {
  48.                         // Check old state. If it is also "open" than it is OK
  49.                         return;
  50.                     }
  51.                     $this->logger->error('Prevent Order State to be set to OPEN', [
  52.                         'payloads' => $payloads,
  53.                         '$_SERVER' => $_SERVER,
  54.                         'apache_request_headers' => apache_request_headers(),
  55.                         'stack_trace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),
  56.                     ]);
  57.                     $event->getExceptions()->add(new \Exception('Prevent Order State to be set to OPEN'));
  58.                 }
  59.             }
  60.         }
  61.     }
  62.     private function getOrderState($primaryKeys): ?string
  63.     {
  64.         try {
  65.             $res $this->connection->executeQuery('SELECT state_id FROM `order` WHERE id = :id and version_id = :version_id'$primaryKeys);
  66.             $retArray $res->fetchAssociative();
  67.             return bin2hex($retArray['state_id']);
  68.         } catch (Exception|\Doctrine\DBAL\Driver\Exception) {
  69.         }
  70.         return null;
  71.     }
  72.     public function onOrderWritten(EntityWrittenEvent $event): void
  73.     {
  74.         if (!$this->systemConfigService->get('WbfkExtensions.config.loggingOrder')) {
  75.             return;
  76.         }
  77.         $payloads $event->getPayloads();
  78.         $ids implode(', '$event->getIds());
  79.         $this->logger->info('Written: '.$ids, [
  80.             'payloads' => $payloads,
  81.             '$_SERVER' => $_SERVER,
  82.             'apache_request_headers' => apache_request_headers(),
  83.         ]);
  84.     }
  85.     public function beforeDelete(BeforeDeleteEvent $event): void
  86.     {
  87.         if (!$this->systemConfigService->get('WbfkExtensions.config.loggingOrder')) {
  88.             return;
  89.         }
  90.         $context $event->getContext();
  91.         $ids $event->getIds(OrderDefinition::ENTITY_NAME);
  92.         if (empty($ids)) {
  93.             return;
  94.         }
  95.         $orderCriteria = new Criteria($ids);
  96.         $orderCriteria->addAssociation('orderCustomer');
  97.         $orderCriteria->addAssociation('billingAddress');
  98.         $orderCriteria->addAssociation('deliveries');
  99.         $orderCriteria->addAssociation('lineItems');
  100.         $orderCriteria->addAssociation('transactions');
  101.         $orderCriteria->addAssociation('tags');
  102.         /** @var OrderCollection $orders */
  103.         $orders $this->orderRepository->search($orderCriteria$context);
  104.         $event->addSuccess(function () use ($orders): void {
  105.             $this->logger->alert('Deleted: '.implode(', '$orders->getIds()), [
  106.                 'orders' => $orders,
  107.                 '$_SERVER' => $_SERVER,
  108.                 'apache_request_headers' => apache_request_headers(),
  109.             ]);
  110.         });
  111.     }
  112. }