<?php
declare(strict_types=1);
namespace SwagHoggi\Flow\Action;
use Psr\Log\LoggerInterface;
use Shopware\Core\Checkout\Order\Aggregate\OrderCustomer\OrderCustomerEntity;
use Shopware\Core\Content\Flow\Dispatching\Action\SendMailAction;
use Shopware\Core\Content\Flow\Dispatching\StorableFlow;
use Shopware\Core\Framework\Event\FlowEvent;
use Symfony\Contracts\EventDispatcher\Event;
use Shopware\Core\Framework\Context;
class SendMailActionDecorator extends SendMailAction
{
private SendMailAction $inner;
private LoggerInterface $logger;
private const TARGET_CUSTOM_FIELD = 'custom_customer_receipients';
public function __construct(
SendMailAction $inner,
LoggerInterface $logger
)
{
$this->inner = $inner;
$this->logger = $logger;
}
public static function getName(): string
{
return SendMailAction::getName();
}
public function requirements(): array
{
return $this->inner->requirements();
}
public function handle (Event $event): void
{
$this->inner->handle($event);
if ($event instanceof FlowEvent) {
try {
$customer = $event->getEvent()->getOrder()->getOrderCustomer();
$config = $event->getConfig();
$context = $event->getContext();
$this->processAdditionalRecipients($config, $customer, $event, $context);
} catch (\Exception $e) {
$this->logger->error('HoggiFlow: Error in legacy handle: ' . $e->getMessage());
}
}
}
public function handleFlow(StorableFlow $flow): void
{
$this->inner->handleFlow($flow);
}
private function processAdditionalRecipients(array $config, OrderCustomerEntity $customer, $originator, Context $context): void
{
$customFields = $customer->getCustomFields() ?? [];
if (empty($customFields) || empty($customFields[self::TARGET_CUSTOM_FIELD])) {
$this->logger->info('HoggiFlow: Target field not found. Looking for: ' . self::TARGET_CUSTOM_FIELD);
return;
}
if (empty($customFields[self::TARGET_CUSTOM_FIELD])) {
$this->logger->info('HoggiFlow: Custom fields found, but TARGET key missing.', ['available_fields' => array_keys($customFields)]);
return;
}
$rawEmails = $customFields[self::TARGET_CUSTOM_FIELD];
$emailList = $this->parseEmails($rawEmails);
if (empty($emailList)) {
return;
}
foreach ($emailList as $email) {
$newConfig = $config;
$newConfig['recipient'] = [
'type' => 'custom',
'data' => [
$email => 'Hoggi - Kopie Empfänger Anfrageformular'
]
];
if ($originator instanceof FlowEvent) {
$this->inner->handle(
new FlowEvent($originator->getActionName(), $originator->getFlowState(), $newConfig)
);
$this->logger->info('HoggiFlow: Sent additional email to ' . $email);
}
}
}
private function parseEmails(mixed $raw): array
{
if (empty($raw)) {
return [];
}
if (is_array($raw)) {
$list = $raw;
} elseif (is_string($raw)) {
if (str_starts_with(trim($raw), '[')) {
$decoded = json_decode($raw, true);
$list = (json_last_error() === JSON_ERROR_NONE) ? $decoded : explode(',', $raw);
} else {
$list = explode(',', $raw);
}
} else {
return [];
}
$emails = [];
foreach ($list as $item) {
if (is_string($item)) {
$email = trim($item);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emails[] = $email;
}
}
}
return array_unique($emails);
}
}