<?php
declare(strict_types=1);
namespace NetInventors\NetiNextStoreLocator\Subscriber;
use NetInventors\NetiNextStoreLocator\Components\Event\ContactEvent;
use NetInventors\NetiNextStoreLocator\Constants\BusinessEventsConstants;
use NetInventors\NetiNextStoreLocator\Constants\FlowConstants;
use Shopware\Core\Content\Flow\Events\FlowSendMailActionEvent;
use Shopware\Core\Content\MailTemplate\Event\MailSendSubscriberBridgeEvent;
use Shopware\Core\Framework\Event\BusinessEventCollector;
use Shopware\Core\Framework\Event\BusinessEventCollectorEvent;
use Shopware\Core\Framework\Event\BusinessEventDefinition;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class BusinessEventSubscriber implements EventSubscriberInterface
{
/**
* @var BusinessEventCollector
*/
private $businessEventCollector;
private string $version;
public function __construct(
BusinessEventCollector $businessEventCollector,
string $version
) {
$this->businessEventCollector = $businessEventCollector;
$this->version = $version;
}
public static function getSubscribedEvents(): array
{
/**
* The MailSendSubscriberBridgeEvent class is deprecated but required to make the feature work.
*
* @psalm-suppress DeprecatedClass
*/
return [
BusinessEventCollectorEvent::NAME => 'onCollectBusinessEvents',
MailSendSubscriberBridgeEvent::class => 'onMailSend',
FlowSendMailActionEvent::class => 'onSendMailAction',
];
}
public function onCollectBusinessEvents(BusinessEventCollectorEvent $event): void
{
/** @psalm-suppress DeprecatedClass */
$definitionClasses = BusinessEventsConstants::EVENT_CLASSES;
if (\version_compare($this->version, '6.4.6.0', '>=')) {
$definitionClasses = FlowConstants::EVENT_CLASSES;
}
foreach ($definitionClasses as $class) {
$eventDefinition = $this->businessEventCollector->define($class);
if ($eventDefinition instanceof BusinessEventDefinition) {
$event->getCollection()->set($class, $eventDefinition);
}
}
}
/**
* The MailSendSubscriberBridgeEvent class is deprecated but required to make the feature work.
*
* @psalm-suppress DeprecatedClass
*/
public function onMailSend(MailSendSubscriberBridgeEvent $event): void
{
/**
* The returned BusinessEvent class is deprecated but required to make the feature work.
*
* @psalm-suppress DeprecatedClass
*/
$businessEvent = $event->getBusinessEvent()->getEvent();
if (!$businessEvent instanceof ContactEvent) {
return;
}
$event->getDataBag()->set('binAttachments', $this->getAttachments($businessEvent));
}
public function onSendMailAction(FlowSendMailActionEvent $event): void
{
/**
* @psalm-suppress DeprecatedClass The FlowEvent class will be deprecated with 6.5.x
* @psalm-suppress DeprecatedMethod The getFlowEvent method will be deprecated with 6.5.x
*/
$flowEvent = $event->getFlowEvent()->getEvent();
if (!$flowEvent instanceof \NetInventors\NetiNextStoreLocator\Components\FlowEvent\ContactEvent) {
return;
}
$event->getDataBag()->set('binAttachments', $this->getAttachments($flowEvent));
}
private function getAttachments(ContactEvent $event): array
{
$attachments = [];
/** @var array<string, mixed> $file */
foreach ($event->getFiles() as $file) {
/** @var UploadedFile $uploadedFile */
$uploadedFile = $file['file'];
/** @var string $name */
$name = $file['name'];
$attachments[] = [
'content' => $uploadedFile->getContent(),
'fileName' => $name,
'mimeType' => $uploadedFile->getMimeType(),
];
}
return $attachments;
}
}