custom/plugins/SwagHoggi/src/Flow/Action/SendMailActionDecorator.php line 42

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace SwagHoggi\Flow\Action;
  4. use Psr\Log\LoggerInterface;
  5. use Shopware\Core\Checkout\Order\Aggregate\OrderCustomer\OrderCustomerEntity;
  6. use Shopware\Core\Content\Flow\Dispatching\Action\SendMailAction;
  7. use Shopware\Core\Content\Flow\Dispatching\StorableFlow;
  8. use Shopware\Core\Framework\Event\FlowEvent;
  9. use Symfony\Contracts\EventDispatcher\Event;
  10. use Shopware\Core\Framework\Context;
  11. class SendMailActionDecorator extends SendMailAction
  12. {
  13.     private SendMailAction $inner;
  14.     private LoggerInterface $logger;
  15.     private const TARGET_CUSTOM_FIELD 'custom_customer_receipients';
  16.     public function __construct(
  17.         SendMailAction $inner,
  18.         LoggerInterface $logger
  19.     )
  20.     {
  21.         $this->inner $inner;
  22.         $this->logger $logger;
  23.     }
  24.     public static function getName(): string
  25.     {
  26.         return SendMailAction::getName();
  27.     }
  28.     public function requirements(): array
  29.     {
  30.         return $this->inner->requirements();
  31.     }
  32.     public function handle (Event $event): void
  33.     {
  34.         $this->inner->handle($event);
  35.         if ($event instanceof FlowEvent) {
  36.             try {
  37.                 $customer $event->getEvent()->getOrder()->getOrderCustomer();
  38.                 $config $event->getConfig();
  39.                 $context $event->getContext();
  40.                 $this->processAdditionalRecipients($config$customer$event$context);
  41.             } catch (\Exception $e) {
  42.                 $this->logger->error('HoggiFlow: Error in legacy handle: ' $e->getMessage());
  43.             }
  44.         }
  45.     }
  46.     public function handleFlow(StorableFlow $flow): void
  47.     {
  48.         $this->inner->handleFlow($flow);
  49.     }
  50.     private function processAdditionalRecipients(array $configOrderCustomerEntity $customer$originatorContext $context): void
  51.     {
  52.         $customFields $customer->getCustomFields() ?? [];
  53.         if (empty($customFields) || empty($customFields[self::TARGET_CUSTOM_FIELD])) {
  54.             $this->logger->info('HoggiFlow: Target field not found. Looking for: ' self::TARGET_CUSTOM_FIELD);
  55.             return;
  56.         }
  57.         if (empty($customFields[self::TARGET_CUSTOM_FIELD])) {
  58.             $this->logger->info('HoggiFlow: Custom fields found, but TARGET key missing.', ['available_fields' => array_keys($customFields)]);
  59.             return;
  60.         }
  61.         $rawEmails $customFields[self::TARGET_CUSTOM_FIELD];
  62.         $emailList $this->parseEmails($rawEmails);
  63.         if (empty($emailList)) {
  64.             return;
  65.         }
  66.         foreach ($emailList as $email) {
  67.             $newConfig $config;
  68.             $newConfig['recipient'] = [
  69.                 'type' => 'custom',
  70.                 'data' => [
  71.                     $email => 'Hoggi - Kopie Empfänger Anfrageformular'
  72.                 ]
  73.             ];
  74.             if ($originator instanceof FlowEvent) {
  75.                 $this->inner->handle(
  76.                     new FlowEvent($originator->getActionName(), $originator->getFlowState(), $newConfig)
  77.                 );
  78.                 $this->logger->info('HoggiFlow: Sent additional email to ' $email);
  79.             }
  80.         }
  81.     }
  82.     private function parseEmails(mixed $raw): array
  83.     {
  84.         if (empty($raw)) {
  85.             return [];
  86.         }
  87.         if (is_array($raw)) {
  88.             $list $raw;
  89.         } elseif (is_string($raw)) {
  90.             if (str_starts_with(trim($raw), '[')) {
  91.                 $decoded json_decode($rawtrue);
  92.                 $list = (json_last_error() === JSON_ERROR_NONE) ? $decoded explode(','$raw);
  93.             } else {
  94.                 $list explode(','$raw);
  95.             }
  96.         } else {
  97.             return [];
  98.         }
  99.         $emails = [];
  100.         foreach ($list as $item) {
  101.             if (is_string($item)) {
  102.                 $email trim($item);
  103.                 if (filter_var($emailFILTER_VALIDATE_EMAIL)) {
  104.                     $emails[] = $email;
  105.                 }
  106.             }
  107.         }
  108.         return array_unique($emails);
  109.     }
  110. }