custom/plugins/SwagAmazonPay/src/SwagAmazonPay.php line 51

  1. <?php declare(strict_types=1);
  2. namespace Swag\AmazonPay;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Checkout\Payment\PaymentMethodDefinition;
  5. use Shopware\Core\Content\Rule\RuleDefinition;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  10. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  13. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  14. use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
  15. use Shopware\Core\System\Currency\CurrencyDefinition;
  16. use Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetDefinition;
  17. use Shopware\Core\System\Salutation\SalutationDefinition;
  18. use Swag\AmazonPay\Installer\CustomFieldsInstaller;
  19. use Swag\AmazonPay\Installer\DatabaseInstaller;
  20. use Swag\AmazonPay\Installer\DefaultSalutationInstaller;
  21. use Swag\AmazonPay\Installer\PaymentMethodInstaller;
  22. use Swag\AmazonPay\Installer\RuleInstaller;
  23. use Swag\AmazonPay\Uninstaller\ConfigurationUninstaller;
  24. use Swag\AmazonPay\Uninstaller\LogUninstaller;
  25. use Swag\AmazonPay\Util\SwagAmazonPayClassLoader;
  26. use Swag\AmazonPay\Util\Util;
  27. use Symfony\Component\DependencyInjection\ContainerInterface;
  28. use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
  29. if (\file_exists(__DIR__ . '/../vendor/autoload.php')) {
  30. (new SwagAmazonPayClassLoader())->register();
  31. }
  32. class SwagAmazonPay extends Plugin
  33. {
  34. public const CHECKOUT_SESSION_KEY = 'swag-amazon-pay.checkout-session-id';
  35. public function install(InstallContext $installContext): void
  36. {
  37. /** @var PluginIdProvider $pluginIdProvider */
  38. $pluginIdProvider = $this->container->get(PluginIdProvider::class);
  39. $paymentMethodRepository = $this->getRepository($this->container, PaymentMethodDefinition::ENTITY_NAME);
  40. $customFieldSetRepository = $this->getRepository($this->container, CustomFieldSetDefinition::ENTITY_NAME);
  41. $salutationRepository = $this->getRepository($this->container, SalutationDefinition::ENTITY_NAME);
  42. $ruleRepository = $this->getRepository($this->container, RuleDefinition::ENTITY_NAME);
  43. $currencyRepository = $this->getRepository($this->container, CurrencyDefinition::ENTITY_NAME);
  44. (new PaymentMethodInstaller($paymentMethodRepository, $pluginIdProvider))->install($installContext);
  45. (new CustomFieldsInstaller($customFieldSetRepository))->install($installContext);
  46. (new DefaultSalutationInstaller($salutationRepository))->install($installContext);
  47. (new RuleInstaller($ruleRepository, $currencyRepository, $paymentMethodRepository))->install($installContext);
  48. }
  49. public function update(UpdateContext $updateContext): void
  50. {
  51. $ruleRepository = $this->getRepository($this->container, RuleDefinition::ENTITY_NAME);
  52. $currencyRepository = $this->getRepository($this->container, CurrencyDefinition::ENTITY_NAME);
  53. $paymentMethodRepository = $this->getRepository($this->container, PaymentMethodDefinition::ENTITY_NAME);
  54. (new RuleInstaller($ruleRepository, $currencyRepository, $paymentMethodRepository))->update($updateContext);
  55. }
  56. public function deactivate(DeactivateContext $deactivateContext): void
  57. {
  58. /** @var PluginIdProvider $pluginIdProvider */
  59. $pluginIdProvider = $this->container->get(PluginIdProvider::class);
  60. $paymentMethodRepository = $this->getRepository($this->container, PaymentMethodDefinition::ENTITY_NAME);
  61. $customFieldSetRepository = $this->getRepository($this->container, CustomFieldSetDefinition::ENTITY_NAME);
  62. (new PaymentMethodInstaller($paymentMethodRepository, $pluginIdProvider))->deactivate($deactivateContext);
  63. (new CustomFieldsInstaller($customFieldSetRepository))->deactivate($deactivateContext);
  64. }
  65. public function uninstall(UninstallContext $uninstallContext): void
  66. {
  67. /** @var Connection $connection */
  68. $connection = $this->container->get(Connection::class);
  69. /** @var PluginIdProvider $pluginIdProvider */
  70. $pluginIdProvider = $this->container->get(PluginIdProvider::class);
  71. $paymentMethodRepository = $this->getRepository($this->container, PaymentMethodDefinition::ENTITY_NAME);
  72. $customFieldSetRepository = $this->getRepository($this->container, CustomFieldSetDefinition::ENTITY_NAME);
  73. $ruleRepository = $this->getRepository($this->container, RuleDefinition::ENTITY_NAME);
  74. $currencyRepository = $this->getRepository($this->container, CurrencyDefinition::ENTITY_NAME);
  75. (new PaymentMethodInstaller($paymentMethodRepository, $pluginIdProvider))->uninstall($uninstallContext);
  76. (new CustomFieldsInstaller($customFieldSetRepository))->uninstall($uninstallContext);
  77. (new DatabaseInstaller($connection))->uninstall($uninstallContext);
  78. (new RuleInstaller($ruleRepository, $currencyRepository, $paymentMethodRepository))->uninstall($uninstallContext);
  79. (new ConfigurationUninstaller($connection))->uninstall($uninstallContext);
  80. $logsDir = $this->container->getParameter('kernel.logs_dir');
  81. (new LogUninstaller($logsDir))->uninstall($uninstallContext);
  82. }
  83. public function activate(ActivateContext $activateContext): void
  84. {
  85. /** @var PluginIdProvider $pluginIdProvider */
  86. $pluginIdProvider = $this->container->get(PluginIdProvider::class);
  87. $paymentRepository = $this->getRepository($this->container, PaymentMethodDefinition::ENTITY_NAME);
  88. $customFieldSetRepository = $this->getRepository($this->container, CustomFieldSetDefinition::ENTITY_NAME);
  89. (new PaymentMethodInstaller($paymentRepository, $pluginIdProvider))->activate($activateContext);
  90. (new CustomFieldsInstaller($customFieldSetRepository))->activate($activateContext);
  91. }
  92. public function enrichPrivileges(): array
  93. {
  94. return [
  95. 'order.viewer' => [
  96. 'swag_amazon_pay_payment_notification:read',
  97. ],
  98. ];
  99. }
  100. private function getRepository(ContainerInterface $container, string $entityName) //SW65 add return type : EntityRepository
  101. {
  102. $repository = $container->get(\sprintf('%s.repository', $entityName), ContainerInterface::NULL_ON_INVALID_REFERENCE);
  103. if (interface_exists(Util::ENTITY_REPOSITORY_INTERFACE)) {
  104. //SW65 may remove this if statement
  105. if (!$repository instanceof EntityRepositoryInterface) {
  106. throw new ServiceNotFoundException(\sprintf('%s.repository', $entityName));
  107. }
  108. } else {
  109. if (!$repository instanceof EntityRepository) {
  110. throw new ServiceNotFoundException(\sprintf('%s.repository', $entityName));
  111. }
  112. }
  113. return $repository;
  114. }
  115. }