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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * (c) shopware AG <info@shopware.com>
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Swag\AmazonPay;
  9. use Doctrine\DBAL\Connection;
  10. use Shopware\Core\Checkout\Payment\PaymentMethodDefinition;
  11. use Shopware\Core\Content\Flow\Aggregate\FlowSequence\FlowSequenceDefinition;
  12. use Shopware\Core\Content\Flow\Aggregate\FlowTemplate\FlowTemplateDefinition;
  13. use Shopware\Core\Content\Flow\FlowDefinition;
  14. use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeDefinition;
  15. use Shopware\Core\Content\MailTemplate\MailTemplateDefinition;
  16. use Shopware\Core\Content\Rule\RuleDefinition;
  17. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  18. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  19. use Shopware\Core\Framework\Plugin;
  20. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  21. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  22. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  23. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  24. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  25. use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
  26. use Shopware\Core\System\Currency\CurrencyDefinition;
  27. use Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetDefinition;
  28. use Shopware\Core\System\Salutation\SalutationDefinition;
  29. use Swag\AmazonPay\Installer\CustomFieldsInstaller;
  30. use Swag\AmazonPay\Installer\DatabaseInstaller;
  31. use Swag\AmazonPay\Installer\DefaultSalutationInstaller;
  32. use Swag\AmazonPay\Installer\FlowInstaller;
  33. use Swag\AmazonPay\Installer\FlowTemplateInstaller;
  34. use Swag\AmazonPay\Installer\MailTemplateInstaller;
  35. use Swag\AmazonPay\Installer\PaymentMethodInstaller;
  36. use Swag\AmazonPay\Installer\RuleInstaller;
  37. use Swag\AmazonPay\Uninstaller\ConfigurationUninstaller;
  38. use Swag\AmazonPay\Uninstaller\LogUninstaller;
  39. use Swag\AmazonPay\Util\SwagAmazonPayClassLoader;
  40. use Swag\AmazonPay\Util\Util;
  41. use Symfony\Component\DependencyInjection\ContainerInterface;
  42. use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
  43. if (\file_exists(__DIR__ '/../vendor/autoload.php')) {
  44.     (new SwagAmazonPayClassLoader())->register();
  45. }
  46. class SwagAmazonPay extends Plugin
  47. {
  48.     public function install(InstallContext $installContext): void
  49.     {
  50.         /** @var PluginIdProvider $pluginIdProvider */
  51.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  52.         $paymentMethodRepository $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME);
  53.         $customFieldSetRepository $this->getRepository($this->containerCustomFieldSetDefinition::ENTITY_NAME);
  54.         $salutationRepository $this->getRepository($this->containerSalutationDefinition::ENTITY_NAME);
  55.         $ruleRepository $this->getRepository($this->containerRuleDefinition::ENTITY_NAME);
  56.         $currencyRepository $this->getRepository($this->containerCurrencyDefinition::ENTITY_NAME);
  57.         $mailTemplateRepository $this->getRepository($this->containerMailTemplateDefinition::ENTITY_NAME);
  58.         $mailTemplateTypeRepository $this->getRepository($this->containerMailTemplateTypeDefinition::ENTITY_NAME);
  59.         if(class_exists(FlowDefinition::class)) {
  60.             $flowRepository $this->getRepository($this->containerFlowDefinition::ENTITY_NAME);
  61.             $flowSequenceRepository $this->getRepository($this->containerFlowSequenceDefinition::ENTITY_NAME);
  62.         }
  63.         if(class_exists(FlowTemplateDefinition::class)) { //SW65
  64.             $flowTemplateRepository $this->getRepository($this->containerFlowTemplateDefinition::ENTITY_NAME);
  65.         }
  66.         (new PaymentMethodInstaller($paymentMethodRepository$pluginIdProvider))->install($installContext);
  67.         (new CustomFieldsInstaller($customFieldSetRepository))->install($installContext);
  68.         (new DefaultSalutationInstaller($salutationRepository))->install($installContext);
  69.         (new RuleInstaller($ruleRepository$currencyRepository$paymentMethodRepository))->install($installContext);
  70.         (new MailTemplateInstaller($mailTemplateRepository$mailTemplateTypeRepository))->install($installContext);
  71.         if(class_exists(FlowDefinition::class)) {
  72.             (new FlowInstaller($flowRepository$flowSequenceRepository$mailTemplateRepository))->install($installContext);
  73.         }
  74.         if(class_exists(FlowTemplateDefinition::class)) { //SW65
  75.             (new FlowTemplateInstaller($flowTemplateRepository$mailTemplateRepository))->install($installContext);
  76.         }
  77.     }
  78.     public function update(UpdateContext $updateContext): void
  79.     {
  80.         $ruleRepository $this->getRepository($this->containerRuleDefinition::ENTITY_NAME);
  81.         $currencyRepository $this->getRepository($this->containerCurrencyDefinition::ENTITY_NAME);
  82.         $paymentMethodRepository $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME);
  83.         (new RuleInstaller($ruleRepository$currencyRepository$paymentMethodRepository))->update($updateContext);
  84.     }
  85.     public function deactivate(DeactivateContext $deactivateContext): void
  86.     {
  87.         /** @var PluginIdProvider $pluginIdProvider */
  88.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  89.         $paymentMethodRepository $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME);
  90.         $customFieldSetRepository $this->getRepository($this->containerCustomFieldSetDefinition::ENTITY_NAME);
  91.         (new PaymentMethodInstaller($paymentMethodRepository$pluginIdProvider))->deactivate($deactivateContext);
  92.         (new CustomFieldsInstaller($customFieldSetRepository))->deactivate($deactivateContext);
  93.     }
  94.     public function uninstall(UninstallContext $uninstallContext): void
  95.     {
  96.         /** @var Connection $connection */
  97.         $connection $this->container->get(Connection::class);
  98.         /** @var PluginIdProvider $pluginIdProvider */
  99.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  100.         $paymentMethodRepository $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME);
  101.         $customFieldSetRepository $this->getRepository($this->containerCustomFieldSetDefinition::ENTITY_NAME);
  102.         $ruleRepository $this->getRepository($this->containerRuleDefinition::ENTITY_NAME);
  103.         $currencyRepository $this->getRepository($this->containerCurrencyDefinition::ENTITY_NAME);
  104.         $mailTemplateRepository $this->getRepository($this->containerMailTemplateDefinition::ENTITY_NAME);
  105.         $mailTemplateTypeRepository $this->getRepository($this->containerMailTemplateTypeDefinition::ENTITY_NAME);
  106.         if(class_exists(FlowDefinition::class)) {
  107.             $flowRepository $this->getRepository($this->containerFlowDefinition::ENTITY_NAME);
  108.             $flowSequenceRepository $this->getRepository($this->containerFlowSequenceDefinition::ENTITY_NAME);
  109.         }
  110.         if(class_exists(FlowTemplateDefinition::class)) { //SW65
  111.             $flowTemplateRepository $this->getRepository($this->containerFlowTemplateDefinition::ENTITY_NAME);
  112.         }
  113.         (new PaymentMethodInstaller($paymentMethodRepository$pluginIdProvider))->uninstall($uninstallContext);
  114.         (new CustomFieldsInstaller($customFieldSetRepository))->uninstall($uninstallContext);
  115.         (new DatabaseInstaller($connection))->uninstall($uninstallContext);
  116.         (new RuleInstaller($ruleRepository$currencyRepository$paymentMethodRepository))->uninstall($uninstallContext);
  117.         (new MailTemplateInstaller($mailTemplateRepository$mailTemplateTypeRepository))->uninstall($uninstallContext);
  118.         (new ConfigurationUninstaller($connection))->uninstall($uninstallContext);
  119.         if(class_exists(FlowDefinition::class)) {
  120.             (new FlowInstaller($flowRepository$flowSequenceRepository$mailTemplateRepository))->uninstall($uninstallContext);
  121.         }
  122.         if(class_exists(FlowTemplateDefinition::class)) { //SW65
  123.             (new FlowTemplateInstaller($flowTemplateRepository$mailTemplateRepository))->uninstall($uninstallContext);
  124.         }
  125.         $logsDir $this->container->getParameter('kernel.logs_dir');
  126.         (new LogUninstaller($logsDir))->uninstall($uninstallContext);
  127.     }
  128.     public function activate(ActivateContext $activateContext): void
  129.     {
  130.         /** @var PluginIdProvider $pluginIdProvider */
  131.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  132.         $paymentRepository $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME);
  133.         $customFieldSetRepository $this->getRepository($this->containerCustomFieldSetDefinition::ENTITY_NAME);
  134.         (new PaymentMethodInstaller($paymentRepository$pluginIdProvider))->activate($activateContext);
  135.         (new CustomFieldsInstaller($customFieldSetRepository))->activate($activateContext);
  136.     }
  137.     public function enrichPrivileges(): array
  138.     {
  139.         return [
  140.             'order.viewer' => [
  141.                 'swag_amazon_pay_payment_notification:read',
  142.             ],
  143.         ];
  144.     }
  145.     public function executeComposerCommands(): bool
  146.     {
  147.         return true;
  148.     }
  149.     private function getRepository(ContainerInterface $containerstring $entityName//SW65 add return type : EntityRepository
  150.     {
  151.         $repository $container->get(\sprintf('%s.repository'$entityName), ContainerInterface::NULL_ON_INVALID_REFERENCE);
  152.         if (interface_exists(Util::ENTITY_REPOSITORY_INTERFACE)) {
  153.             //SW65 may remove this if statement
  154.             if (!$repository instanceof EntityRepositoryInterface) {
  155.                 throw new ServiceNotFoundException(\sprintf('%s.repository'$entityName));
  156.             }
  157.         } else {
  158.             if (!$repository instanceof EntityRepository) {
  159.                 throw new ServiceNotFoundException(\sprintf('%s.repository'$entityName));
  160.             }
  161.         }
  162.         return $repository;
  163.     }
  164. }