custom/plugins/NewsletterSendinblue/src/NewsletterSendinblue.php line 20

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace NewsletterSendinblue;
  3. use NewsletterSendinblue\Service\ConfigService;
  4. use NewsletterSendinblue\Service\IntegrationService;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  10. use Shopware\Core\Framework\Plugin;
  11. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  12. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  13. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  14. use Shopware\Core\Framework\Uuid\Uuid;
  15. use Shopware\Core\System\SystemConfig\SystemConfigService;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. class NewsletterSendinblue extends Plugin
  18. {
  19.     public const ACL_ROLE_NAME 'Sendinblue';
  20.     public const INTEGRATION_LABEL 'Sendinblue';
  21.     /**
  22.      * @var IntegrationService
  23.      */
  24.     private $integrationService;
  25.     /**
  26.      * @param ContainerBuilder $container
  27.      */
  28.     public function build(ContainerBuilder $container): void
  29.     {
  30.         parent::build($container);
  31.         $container->setParameter('sendinblue.service_worker_path'$this->getPath() . '/Resources/js/service-worker.js');
  32.     }
  33.     /**
  34.      * @param InstallContext $installContext
  35.      */
  36.     public function postInstall(InstallContext $installContext): void
  37.     {
  38.         $this->createAclRole($installContext->getContext());
  39.         $this->createIntegrations($installContext->getContext());
  40.     }
  41.     /**
  42.      * @param DeactivateContext $deactivateContext
  43.      */
  44.     public function deactivate(DeactivateContext $deactivateContext): void
  45.     {
  46.         $this->removeSIBSmtpSettings();
  47.     }
  48.     /**
  49.      * @param UninstallContext $uninstallContext
  50.      */
  51.     public function uninstall(UninstallContext $uninstallContext): void
  52.     {
  53.         if (!$uninstallContext->keepUserData()) {
  54.             $this->deleteIntegrations($uninstallContext->getContext());
  55.             $this->deleteAclRole($uninstallContext->getContext());
  56.             $this->deleteAllSendinblueConfigs();
  57.         }
  58.     }
  59.     /**
  60.      * @param Context $context
  61.      * @return string
  62.      */
  63.     private function createAclRole(Context $context)
  64.     {
  65.         $id md5(self::ACL_ROLE_NAME);
  66.         $roleData = [
  67.             'id' => $id,
  68.             'name' => self::ACL_ROLE_NAME,
  69.             'privileges' => ["customer.editor""customer.viewer""customer:read""customer:update""newsletter_recipient.creator""newsletter_recipient.deleter""newsletter_recipient.editor""newsletter_recipient.viewer""newsletter_recipient:create""newsletter_recipient:delete""newsletter_recipient:read""newsletter_recipient:update"]
  70.         ];
  71.         $aclRoleRepository $this->container->get('acl_role.repository');
  72.         $context->scope(Context::SYSTEM_SCOPE, function (Context $context) use ($aclRoleRepository$roleData): void {
  73.             $aclRoleRepository->upsert([$roleData], $context);
  74.         });
  75.         return $id;
  76.     }
  77.     /**
  78.      * @param Context $context
  79.      */
  80.     private function createIntegrations(Context $context): void
  81.     {
  82.         $this->getIntegrationService()->createIntegration(self::INTEGRATION_LABEL$context);
  83.     }
  84.     /**
  85.      * @param Context $context
  86.      */
  87.     private function deleteIntegrations(Context $context): void
  88.     {
  89.         $this->getIntegrationService()->deleteIntegrations($context);
  90.     }
  91.     /**
  92.      * @param Context $context
  93.      */
  94.     private function deleteAclRole(Context $context)
  95.     {
  96.         $aclRoleRepository $this->container->get('acl_role.repository');
  97.         $context->scope(Context::SYSTEM_SCOPE, function (Context $context) use ($aclRoleRepository): void {
  98.             $aclRoleRepository->delete([['id' => md5(self::ACL_ROLE_NAME)]], $context);
  99.         });
  100.     }
  101.     private function deleteAllSendinblueConfigs(): void
  102.     {
  103.         /** @var EntityRepositoryInterface $systemConfigRepository */
  104.         $systemConfigRepository $this->container->get('system_config.repository');
  105.         $this->removeSIBSmtpSettings();
  106.         $criteria = new Criteria();
  107.         $criteria->addFilter(new ContainsFilter('configurationKey'ConfigService::CONFIG_PREFIX));
  108.         $systemConfigIds $systemConfigRepository->searchIds($criteriaContext::createDefaultContext())->getIds();
  109.         if (empty($systemConfigIds)) {
  110.             return;
  111.         }
  112.         $ids array_map(static function ($id) {
  113.             return ['id' => $id];
  114.         }, $systemConfigIds);
  115.         $systemConfigRepository->delete($idsContext::createDefaultContext());
  116.     }
  117.     /**
  118.      * @return void
  119.      */
  120.     private function removeSIBSmtpSettings(): void
  121.     {
  122.         /** @var EntityRepositoryInterface $systemConfigRepository */
  123.         $systemConfigRepository $this->container->get('system_config.repository');
  124.         $criteria = new Criteria();
  125.         $criteria->addFilter(new EqualsFilter('configurationKey'ConfigService::prepareConfigName(ConfigService::CONFIG_IS_SMTP_ENABLED)));
  126.         $systemConfigs $systemConfigRepository->search($criteriaContext::createDefaultContext())->getElements();
  127.         if (empty($systemConfigs)) {
  128.             return;
  129.         }
  130.         /** @var SystemConfigService $systemConfigService */
  131.         $systemConfigService $this->container->get(SystemConfigService::class);
  132.         $smtpConfigs = [
  133.             ConfigService::CORE_MAILER_AGENT_CONFIG,
  134.             ConfigService::CORE_MAILER_HOST_CONFIG,
  135.             ConfigService::CORE_MAILER_PORT_CONFIG,
  136.             ConfigService::CORE_MAILER_USERNAME_CONFIG,
  137.             ConfigService::CORE_MAILER_PASSWORD_CONFIG,
  138.             ConfigService::CORE_MAILER_SENDER_CONFIG,
  139.             ConfigService::CORE_MAILER_ENCRYPTION_CONFIG,
  140.             ConfigService::CORE_MAILER_AUTHENTICATION_CONFIG
  141.         ];
  142.         foreach ($systemConfigs as $systemConfig) {
  143.             if ($systemConfig->getConfigurationValue()) {
  144.                 $salesChannelId $systemConfig->getSalesChannelId();
  145.                 foreach ($smtpConfigs as $config) {
  146.                     $systemConfigService->delete($config$salesChannelId);
  147.                 }
  148.             }
  149.         }
  150.         foreach ($smtpConfigs as $config) {
  151.             $systemConfigService->delete($config);
  152.         }
  153.     }
  154.     /**
  155.      * @return IntegrationService|null
  156.      */
  157.     private function getIntegrationService(): ?IntegrationService
  158.     {
  159.         if (empty($this->integrationService)) {
  160.             if ($this->container->has(IntegrationService::class)) {
  161.                 /** @var IntegrationService integrationService */
  162.                 $this->integrationService $this->container->get(IntegrationService::class);
  163.             } else {
  164.                 /** @var EntityRepositoryInterface $integrationRepository */
  165.                 $integrationRepository $this->container->get('integration.repository');
  166.                 /** @var EntityRepositoryInterface $aclRoleRepository */
  167.                 $aclRoleRepository $this->container->get('acl_role.repository');
  168.                 /** @var SystemConfigService $systemConfigService */
  169.                 $systemConfigService $this->container->get(SystemConfigService::class);
  170.                 $this->integrationService = new IntegrationService($integrationRepository$aclRoleRepository$systemConfigService);
  171.             }
  172.         }
  173.         return $this->integrationService;
  174.     }
  175. }