<?php
declare(strict_types=1);
namespace VioB2BLogin\Core\Framework\Subscriber;
use ReflectionClass;
use ReflectionException;
use RuntimeException;
use Shopware\Core\Framework\Event\BusinessEventCollectorEvent;
use Shopware\Core\Framework\Event\BusinessEventDefinition;
use Shopware\Core\Framework\Event\BusinessEventInterface;
use Shopware\Core\Framework\Event\FlowEventAware;
use Shopware\Core\Framework\Event\MailActionInterface;
use Shopware\Core\Framework\Event\SalesChannelAware;
use Shopware\Core\Framework\Feature;
use Shopware\Core\Framework\Log\LogAwareBusinessEventInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use VioB2BLogin\Core\Checkout\Cart\Event\CheckoutEmployeeOrderPlacedEvent;
use VioB2BLogin\Core\Checkout\Customer\Event\EmployeeAccountRecoverRequestEvent;
use VioB2BLogin\Core\Checkout\Customer\Event\EmployeeBeforeLoginEvent;
use VioB2BLogin\Core\Checkout\Customer\Event\EmployeeCreateEvent;
use VioB2BLogin\Core\Checkout\Customer\Event\EmployeeDeletedEvent;
use VioB2BLogin\Core\Checkout\Customer\Event\EmployeeLoginEvent;
use VioB2BLogin\Core\Checkout\Customer\Event\EmployeeUpdateEvent;
class BusinessSubscriber implements EventSubscriberInterface
{
public const EVENTS = [
CheckoutEmployeeOrderPlacedEvent::class,
EmployeeCreateEvent::class,
EmployeeUpdateEvent::class,
EmployeeDeletedEvent::class,
EmployeeBeforeLoginEvent::class,
EmployeeLoginEvent::class,
EmployeeAccountRecoverRequestEvent::class
];
/**
* @inheritDoc
*/
public static function getSubscribedEvents(): array
{
return [
BusinessEventCollectorEvent::NAME => 'onCollectBusinessEvents'
];
}
/**
* @throws ReflectionException
*/
public function onCollectBusinessEvents(BusinessEventCollectorEvent $event): void
{
// add BusinessEvents
foreach (self::EVENTS as $eventClassName) {
$definition = $this->createDefinition($eventClassName);
$event->getCollection()->set($definition->getName(), $definition);
}
}
/**
* @throws ReflectionException
*/
private function createDefinition(string $eventClassName): BusinessEventDefinition
{
$reflectionClass = new ReflectionClass($eventClassName);
if (Feature::isActive('FEATURE_NEXT_17858')) {
if (!$reflectionClass->implementsInterface(FlowEventAware::class)) {
throw new RuntimeException(sprintf('Event %s is not a business event', $eventClassName));
}
} else if (!$reflectionClass->implementsInterface(BusinessEventInterface::class)) {
throw new RuntimeException(sprintf('Event %s is not a business event', $eventClassName));
}
$instance = (new ReflectionClass($eventClassName))->newInstanceWithoutConstructor();//NOSONAR
$name = $instance->getName();
$aware = array_keys(array_filter($reflectionClass->getInterfaces(), static function (ReflectionClass $interface) {
return $interface->isSubclassOf(FlowEventAware::class);
})
);
return new BusinessEventDefinition(
$name,
$eventClassName,
$instance instanceof MailActionInterface,
$instance instanceof LogAwareBusinessEventInterface,
$instance instanceof SalesChannelAware,
$instance::getAvailableData()->toArray(),
$aware
);
}
}