custom/plugins/ProxaPaymentReminder/src/ProxaPaymentReminder.php line 21

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ProxaPaymentReminder;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  7. use Shopware\Core\Framework\Plugin;
  8. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  9. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  10. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  11. use Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use ProxaPaymentReminder\Manager\CustomFieldManager;
  13. use ProxaPaymentReminder\Manager\MailTemplateManager;
  14. /**
  15.  * Class ProxaPaymentReminder
  16.  * @package ProxaPaymentReminder
  17.  */
  18. class ProxaPaymentReminder extends Plugin
  19. {
  20.     public const REMAINDER_MAILS_COUNT 2;
  21.     /**
  22.      * {@inheritdoc}
  23.      */
  24.     public function build(ContainerBuilder $container): void
  25.     {
  26.         parent::build($container);
  27.     }
  28.     /**
  29.      * @param InstallContext $installContext
  30.      */
  31.     public function install(InstallContext $installContext): void
  32.     {
  33.         parent::install($installContext);
  34.         ($this->getCustomFieldManager($installContext->getContext()))->install();
  35.         ($this->getMailTemplateManager($installContext->getContext()))->install();
  36.     }
  37.     /**
  38.      * @param UpdateContext $updateContext
  39.      */
  40.     public function update(UpdateContext $updateContext): void
  41.     {
  42.         parent::update($updateContext);
  43.         ($this->getCustomFieldManager($updateContext->getContext()))->update();
  44.         ($this->getMailTemplateManager($updateContext->getContext()))->update();
  45.     }
  46.     /**
  47.      * @param UninstallContext $uninstallContext
  48.      */
  49.     public function uninstall(UninstallContext $uninstallContext): void
  50.     {
  51.         parent::uninstall($uninstallContext);
  52.         if ($uninstallContext->keepUserData()) {
  53.             return;
  54.         }
  55.         $this->removeConfiguration($uninstallContext->getContext());
  56.         ($this->getCustomFieldManager($uninstallContext->getContext()))->uninstall();
  57.         ($this->getMailTemplateManager($uninstallContext->getContext()))->uninstall();
  58.     }
  59.     /**
  60.      * @param Context $context
  61.      */
  62.     private function removeConfiguration(Context $context): void
  63.     {
  64.         /** @var EntityRepositoryInterface $systemConfigRepository */
  65.         $systemConfigRepository $this->container->get('system_config.repository');
  66.         $criteria = (new Criteria())->addFilter(new ContainsFilter('configurationKey'$this->getName() . '.config.'));
  67.         $idSearchResult $systemConfigRepository->searchIds($criteria$context);
  68.         $ids array_map(static function ($id) {
  69.             return ['id' => $id];
  70.         }, $idSearchResult->getIds());
  71.         if ($ids === []) {
  72.             return;
  73.         }
  74.         $systemConfigRepository->delete($ids$context);
  75.     }
  76.     /**
  77.      * @param Context $context
  78.      * @return CustomFieldManager
  79.      */
  80.     private function getCustomFieldManager(Context $context): CustomFieldManager
  81.     {
  82.         return new CustomFieldManager(
  83.             $context,
  84.             $this->container->get('custom_field_set.repository'),
  85.             $this->container->get('snippet.repository')
  86.         );
  87.     }
  88.     /**
  89.      * @param Context $context
  90.      * @return MailTemplateManager
  91.      */
  92.     private function getMailTemplateManager(Context $context): MailTemplateManager
  93.     {
  94.         return new MailTemplateManager(
  95.             $this->container,
  96.             $context,
  97.             $this->container->get('mail_template.repository'),
  98.             $this->container->get('mail_template_type.repository'),
  99.             $this->container->get('event_action.repository')
  100.         );
  101.     }
  102. }