vendor/wns/security-compliance-suite/src/Logging/Subscriber/PluginActionSubscriber.php line 48

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WnsSecurityComplianceSuite\Logging\Subscriber;
  4. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  5. use Shopware\Core\Framework\Plugin\Event\PluginLifecycleEvent;
  6. use Shopware\Core\Framework\Plugin\Event\PluginPostActivateEvent;
  7. use Shopware\Core\Framework\Plugin\Event\PluginPostDeactivateEvent;
  8. use Shopware\Core\Framework\Plugin\Event\PluginPostInstallEvent;
  9. use Shopware\Core\Framework\Plugin\Event\PluginPostUninstallEvent;
  10. use Shopware\Core\Framework\Plugin\Event\PluginPostUpdateEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use WnsSecurityComplianceSuite\Logging\DTO\Log;
  13. use WnsSecurityComplianceSuite\Logging\Enum\EventTypes;
  14. use WnsSecurityComplianceSuite\Logging\Provider\SourceInfoProvider;
  15. use WnsSecurityComplianceSuite\Logging\Service\ActionLogger;
  16. final class PluginActionSubscriber implements EventSubscriberInterface
  17. {
  18. private ActionLogger $actionLogger;
  19. private SourceInfoProvider $sourceInfoProvider;
  20. public function __construct(
  21. ActionLogger $actionLogger,
  22. SourceInfoProvider $sourceInfoProvider
  23. ) {
  24. $this->actionLogger = $actionLogger;
  25. $this->sourceInfoProvider = $sourceInfoProvider;
  26. }
  27. public static function getSubscribedEvents()
  28. {
  29. return [
  30. PluginPostInstallEvent::class => 'onPluginAction',
  31. PluginPostActivateEvent::class => 'onPluginAction',
  32. PluginPostDeactivateEvent::class => 'onPluginAction',
  33. PluginPostUninstallEvent::class => 'onPluginAction',
  34. PluginPostUpdateEvent::class => 'onPluginAction',
  35. ];
  36. }
  37. /**
  38. * @param PluginPostInstallEvent|PluginPostActivateEvent|PluginPostDeactivateEvent|PluginPostUninstallEvent|PluginPostUpdateEvent $event
  39. */
  40. public function onPluginAction(PluginLifecycleEvent $event): void
  41. {
  42. /** @var InstallContext $context */
  43. $context = $event->getContext();
  44. $plugin = $context->getPlugin();
  45. $detail = [
  46. 'plugin' => $plugin->getName(),
  47. 'version' => $context->getCurrentPluginVersion(),
  48. ];
  49. $log = new Log(
  50. $this->getEventType(\get_class($event)),
  51. $this->sourceInfoProvider->getSourceDTO($event->getContext()->getContext()->getSource()),
  52. $detail,
  53. new \DateTimeImmutable(),
  54. );
  55. $this->actionLogger->log($log, null);
  56. }
  57. private function getEventType(string $event): string
  58. {
  59. switch ($event) {
  60. case PluginPostInstallEvent::class:
  61. return EventTypes::PLUGIN_INSTALL;
  62. case PluginPostActivateEvent::class:
  63. return EventTypes::PLUGIN_ACTIVATE;
  64. case PluginPostDeactivateEvent::class:
  65. return EventTypes::PLUGIN_DEACTIVATE;
  66. case PluginPostUninstallEvent::class:
  67. return EventTypes::PLUGIN_UNINSTALL;
  68. case PluginPostUpdateEvent::class:
  69. return EventTypes::PLUGIN_UPDATE;
  70. default:
  71. return $event;
  72. }
  73. }
  74. }