<?php
declare(strict_types=1);
namespace WnsSecurityComplianceSuite\Logging\Subscriber;
use Shopware\Core\Framework\App\Event\AppActivatedEvent;
use Shopware\Core\Framework\App\Event\AppChangedEvent;
use Shopware\Core\Framework\App\Event\AppDeactivatedEvent;
use Shopware\Core\Framework\App\Event\AppDeletedEvent;
use Shopware\Core\Framework\App\Event\AppInstalledEvent;
use Shopware\Core\Framework\App\Event\AppUpdatedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use WnsSecurityComplianceSuite\Logging\DTO\Log;
use WnsSecurityComplianceSuite\Logging\Enum\EventTypes;
use WnsSecurityComplianceSuite\Logging\Provider\SourceInfoProvider;
use WnsSecurityComplianceSuite\Logging\Service\ActionLogger;
final class AppActionSubscriber implements EventSubscriberInterface
{
private ActionLogger $actionLogger;
private SourceInfoProvider $sourceInfoProvider;
private EntityRepository $appRepository;
public function __construct(
ActionLogger $actionLogger,
SourceInfoProvider $sourceInfoProvider,
EntityRepository $appRepository
) {
$this->actionLogger = $actionLogger;
$this->sourceInfoProvider = $sourceInfoProvider;
$this->appRepository = $appRepository;
}
public static function getSubscribedEvents()
{
return [
AppActivatedEvent::class => 'onAppAction',
AppDeactivatedEvent::class => 'onAppAction',
AppInstalledEvent::class => 'onAppAction',
AppUpdatedEvent::class => 'onAppAction',
AppDeletedEvent::class => 'onAppDeleted',
];
}
/**
* @param AppActivatedEvent|AppDeactivatedEvent|AppInstalledEvent|AppUpdatedEvent $event
*/
public function onAppAction(AppChangedEvent $event): void
{
$app = $event->getApp();
$detail = [
'app' => $app->getName(),
'version' => $app->getVersion(),
];
$log = new Log(
$this->getEventType(\get_class($event)),
$this->sourceInfoProvider->getSourceDTO($event->getContext()->getSource()),
$detail,
new \DateTimeImmutable(),
);
$this->actionLogger->log($log, null);
}
public function onAppDeleted(AppDeletedEvent $event): void
{
$app = $this->appRepository->search(new Criteria([$event->getAppId()]), $event->getContext())->first();
if ($app === null) {
$detail = [
'appId' => $event->getAppId(),
];
} else {
$detail = [
'app' => $app->getName(),
'version' => $app->getVersion(),
];
}
$log = new Log(
$this->getEventType(\get_class($event)),
$this->sourceInfoProvider->getSourceDTO($event->getContext()->getSource()),
$detail,
new \DateTimeImmutable(),
);
$this->actionLogger->log($log, null);
}
private function getEventType(string $event): string
{
switch ($event) {
case AppActivatedEvent::class:
return EventTypes::APP_ACTIVATED;
case AppDeactivatedEvent::class:
return EventTypes::APP_DEACTIVATED;
case AppDeletedEvent::class:
return EventTypes::APP_DELETED;
case AppInstalledEvent::class:
return EventTypes::APP_INSTALLED;
case AppUpdatedEvent::class:
return EventTypes::APP_UPDATED;
default:
return $event;
}
}
}