<?php declare(strict_types=1);
namespace Acris\ShopSwitch\Subscriber;
use Shopware\Core\Framework\Event\BusinessEvent;
use Shopware\Core\Framework\Event\BusinessEventCollectorEvent;
use Shopware\Core\Framework\Event\BusinessEventDefinition;
use Shopware\Core\Framework\Event\BusinessEventInterface;
use Shopware\Core\Framework\Event\MailActionInterface;
use Shopware\Core\Framework\Event\SalesChannelAware;
use Shopware\Core\Framework\Log\LogAwareBusinessEventInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ShopSwitchSubscriber implements EventSubscriberInterface
{
public const CUSTOM_EVENTS_CLASSES = [
'Acris\ShopSwitch\Core\Content\ShopSwitch\Event\ShopSwitchDatabaseUpdateEvent'
];
public static function getSubscribedEvents()
{
return [
BusinessEventCollectorEvent::NAME => [
['onBusinessEventCollect', 200]
]
];
}
public function onBusinessEventCollect(BusinessEventCollectorEvent $event): void
{
$result = $event->getCollection();
foreach (self::CUSTOM_EVENTS_CLASSES as $class) {
$definition = $this->define($class);
if (!$definition) {
continue;
}
$result->set($definition->getName(), $definition);
}
}
private function define(string $class, ?string $name = null): ?BusinessEventDefinition
{
if ($class === BusinessEvent::class) {
return null;
}
$instance = (new \ReflectionClass($class))
->newInstanceWithoutConstructor();
if (!$instance instanceof BusinessEventInterface) {
throw new \RuntimeException(sprintf('Event %s is not a business event', $class));
}
$name = $name ?? $instance->getName();
if (!$name) {
return null;
}
return new BusinessEventDefinition(
$name,
$class,
$instance instanceof MailActionInterface,
$instance instanceof LogAwareBusinessEventInterface,
$instance instanceof SalesChannelAware,
$instance::getAvailableData()->toArray()
);
}
}