vendor/store.shopware.com/swagamazonpay/src/SwagAmazonPay.php line 36

Open in your IDE?
  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 function install(InstallContext $installContext): void
  35.     {
  36.         /** @var PluginIdProvider $pluginIdProvider */
  37.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  38.         $paymentMethodRepository $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME);
  39.         $customFieldSetRepository $this->getRepository($this->containerCustomFieldSetDefinition::ENTITY_NAME);
  40.         $salutationRepository $this->getRepository($this->containerSalutationDefinition::ENTITY_NAME);
  41.         $ruleRepository $this->getRepository($this->containerRuleDefinition::ENTITY_NAME);
  42.         $currencyRepository $this->getRepository($this->containerCurrencyDefinition::ENTITY_NAME);
  43.         (new PaymentMethodInstaller($paymentMethodRepository$pluginIdProvider))->install($installContext);
  44.         (new CustomFieldsInstaller($customFieldSetRepository))->install($installContext);
  45.         (new DefaultSalutationInstaller($salutationRepository))->install($installContext);
  46.         (new RuleInstaller($ruleRepository$currencyRepository$paymentMethodRepository))->install($installContext);
  47.     }
  48.     public function update(UpdateContext $updateContext): void
  49.     {
  50.         $ruleRepository $this->getRepository($this->containerRuleDefinition::ENTITY_NAME);
  51.         $currencyRepository $this->getRepository($this->containerCurrencyDefinition::ENTITY_NAME);
  52.         $paymentMethodRepository $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME);
  53.         (new RuleInstaller($ruleRepository$currencyRepository$paymentMethodRepository))->update($updateContext);
  54.     }
  55.     public function deactivate(DeactivateContext $deactivateContext): void
  56.     {
  57.         /** @var PluginIdProvider $pluginIdProvider */
  58.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  59.         $paymentMethodRepository $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME);
  60.         $customFieldSetRepository $this->getRepository($this->containerCustomFieldSetDefinition::ENTITY_NAME);
  61.         (new PaymentMethodInstaller($paymentMethodRepository$pluginIdProvider))->deactivate($deactivateContext);
  62.         (new CustomFieldsInstaller($customFieldSetRepository))->deactivate($deactivateContext);
  63.     }
  64.     public function uninstall(UninstallContext $uninstallContext): void
  65.     {
  66.         /** @var Connection $connection */
  67.         $connection $this->container->get(Connection::class);
  68.         /** @var PluginIdProvider $pluginIdProvider */
  69.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  70.         $paymentMethodRepository $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME);
  71.         $customFieldSetRepository $this->getRepository($this->containerCustomFieldSetDefinition::ENTITY_NAME);
  72.         $ruleRepository $this->getRepository($this->containerRuleDefinition::ENTITY_NAME);
  73.         $currencyRepository $this->getRepository($this->containerCurrencyDefinition::ENTITY_NAME);
  74.         (new PaymentMethodInstaller($paymentMethodRepository$pluginIdProvider))->uninstall($uninstallContext);
  75.         (new CustomFieldsInstaller($customFieldSetRepository))->uninstall($uninstallContext);
  76.         (new DatabaseInstaller($connection))->uninstall($uninstallContext);
  77.         (new RuleInstaller($ruleRepository$currencyRepository$paymentMethodRepository))->uninstall($uninstallContext);
  78.         (new ConfigurationUninstaller($connection))->uninstall($uninstallContext);
  79.         $logsDir $this->container->getParameter('kernel.logs_dir');
  80.         (new LogUninstaller($logsDir))->uninstall($uninstallContext);
  81.     }
  82.     public function activate(ActivateContext $activateContext): void
  83.     {
  84.         /** @var PluginIdProvider $pluginIdProvider */
  85.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  86.         $paymentRepository $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME);
  87.         $customFieldSetRepository $this->getRepository($this->containerCustomFieldSetDefinition::ENTITY_NAME);
  88.         (new PaymentMethodInstaller($paymentRepository$pluginIdProvider))->activate($activateContext);
  89.         (new CustomFieldsInstaller($customFieldSetRepository))->activate($activateContext);
  90.     }
  91.     public function enrichPrivileges(): array
  92.     {
  93.         return [
  94.             'order.viewer' => [
  95.                 'swag_amazon_pay_payment_notification:read',
  96.             ],
  97.         ];
  98.     }
  99.     private function getRepository(ContainerInterface $containerstring $entityName//SW65 add return type : EntityRepository
  100.     {
  101.         $repository $container->get(\sprintf('%s.repository'$entityName), ContainerInterface::NULL_ON_INVALID_REFERENCE);
  102.         if (interface_exists(Util::ENTITY_REPOSITORY_INTERFACE)) {
  103.             //SW65 may remove this if statement
  104.             if (!$repository instanceof EntityRepositoryInterface) {
  105.                 throw new ServiceNotFoundException(\sprintf('%s.repository'$entityName));
  106.             }
  107.         } else {
  108.             if (!$repository instanceof EntityRepository) {
  109.                 throw new ServiceNotFoundException(\sprintf('%s.repository'$entityName));
  110.             }
  111.         }
  112.         return $repository;
  113.     }
  114. }