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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace NewsletterSendinblue;
  3. use Doctrine\DBAL\Connection;
  4. use NewsletterSendinblue\Service\ConfigService;
  5. use NewsletterSendinblue\Service\IntegrationService;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  11. use Shopware\Core\Framework\Plugin;
  12. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  13. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  14. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  15. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  16. use Shopware\Core\Framework\Uuid\Uuid;
  17. use Shopware\Core\System\SystemConfig\SystemConfigService;
  18. use Symfony\Component\DependencyInjection\ContainerBuilder;
  19. class NewsletterSendinblue extends Plugin
  20. {
  21.     public const ACL_ROLE_NAME 'Brevo';
  22.     public const OLD_ACL_ROLE_NAME 'Sendinblue';
  23.     public const INTEGRATION_LABEL 'Brevo';
  24.     public const OLD_INTEGRATION_LABEL 'Sendinblue';
  25.     public const PLUGIN_LABEL 'Sendinblue';
  26.     /**
  27.      * @var IntegrationService
  28.      */
  29.     private $integrationService;
  30.     /**
  31.      * @param ContainerBuilder $container
  32.      */
  33.     public function build(ContainerBuilder $container): void
  34.     {
  35.         parent::build($container);
  36.         $container->setParameter('sendinblue.service_worker_path'$this->getPath() . '/Resources/js/service-worker.js');
  37.     }
  38.     /**
  39.      * @param InstallContext $installContext
  40.      */
  41.     public function postInstall(InstallContext $installContext): void
  42.     {
  43.         $this->createAclRole($installContext->getContext());
  44.         $this->createIntegrations($installContext->getContext());
  45.     }
  46.     /**
  47.      * @param UpdateContext $updateContext
  48.      * @return void
  49.      */
  50.     public function update(UpdateContext $updateContext): void
  51.     {
  52.         if (version_compare($updateContext->getCurrentPluginVersion(), '3.0.11''<=')) {
  53.             try {
  54.                 $this->changeIntegrationAndRoleName($updateContext->getContext());
  55.             } catch (\Throwable $exception) {
  56.             }
  57.         }
  58.     }
  59.     /**
  60.      * @param DeactivateContext $deactivateContext
  61.      */
  62.     public function deactivate(DeactivateContext $deactivateContext): void
  63.     {
  64.         $this->removeSIBSmtpSettings($deactivateContext->getContext());
  65.     }
  66.     /**
  67.      * @param UninstallContext $uninstallContext
  68.      */
  69.     public function uninstall(UninstallContext $uninstallContext): void
  70.     {
  71.         if (!$uninstallContext->keepUserData()) {
  72.             $this->deleteIntegrations($uninstallContext->getContext());
  73.             $this->deleteAclRole($uninstallContext->getContext());
  74.             $this->deleteAllSendinblueConfigs($uninstallContext->getContext());
  75.         }
  76.     }
  77.     /**
  78.      * @param Context $context
  79.      * @return string
  80.      */
  81.     private function createAclRole(Context $context)
  82.     {
  83.         $id md5(self::ACL_ROLE_NAME);
  84.         $roleData = [
  85.             'id' => $id,
  86.             'name' => self::ACL_ROLE_NAME,
  87.             '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"]
  88.         ];
  89.         $aclRoleRepository $this->container->get('acl_role.repository');
  90.         $context->scope(Context::SYSTEM_SCOPE, function (Context $context) use ($aclRoleRepository$roleData): void {
  91.             $aclRoleRepository->upsert([$roleData], $context);
  92.         });
  93.         return $id;
  94.     }
  95.     /**
  96.      * @param Context $context
  97.      */
  98.     private function createIntegrations(Context $context): void
  99.     {
  100.         $this->getIntegrationService()->createIntegration(self::INTEGRATION_LABEL$context);
  101.     }
  102.     /**
  103.      * @param Context $context
  104.      */
  105.     private function deleteIntegrations(Context $context): void
  106.     {
  107.         $this->getIntegrationService()->deleteIntegrations($context);
  108.     }
  109.     /**
  110.      * @param Context $context
  111.      */
  112.     private function deleteAclRole(Context $context)
  113.     {
  114.         $aclRoleRepository $this->container->get('acl_role.repository');
  115.         $context->scope(Context::SYSTEM_SCOPE, function (Context $context) use ($aclRoleRepository): void {
  116.             $aclRoleRepository->delete([['id' => md5(self::ACL_ROLE_NAME)]], $context);
  117.             $aclRoleRepository->delete([['id' => md5(self::OLD_ACL_ROLE_NAME)]], $context);
  118.         });
  119.     }
  120.     private function deleteAllSendinblueConfigs(Context $context): void
  121.     {
  122.         /** @var EntityRepositoryInterface $systemConfigRepository */
  123.         $systemConfigRepository $this->container->get('system_config.repository');
  124.         $this->removeSIBSmtpSettings($context);
  125.         $criteria = new Criteria();
  126.         $criteria->addFilter(new ContainsFilter('configurationKey'ConfigService::CONFIG_PREFIX));
  127.         $systemConfigIds $systemConfigRepository->searchIds($criteria$context)->getIds();
  128.         if (empty($systemConfigIds)) {
  129.             return;
  130.         }
  131.         $ids array_map(static function ($id) {
  132.             return ['id' => $id];
  133.         }, $systemConfigIds);
  134.         $systemConfigRepository->delete($ids$context);
  135.     }
  136.     /**
  137.      * @param Context $context
  138.      * @return void
  139.      */
  140.     private function removeSIBSmtpSettings(Context $context): void
  141.     {
  142.         /** @var EntityRepositoryInterface $systemConfigRepository */
  143.         $systemConfigRepository $this->container->get('system_config.repository');
  144.         $criteria = new Criteria();
  145.         $criteria->addFilter(new EqualsFilter('configurationKey'ConfigService::prepareConfigName(ConfigService::CONFIG_IS_SMTP_ENABLED)));
  146.         $systemConfigs $systemConfigRepository->search($criteria$context)->getElements();
  147.         if (empty($systemConfigs)) {
  148.             return;
  149.         }
  150.         /** @var SystemConfigService $systemConfigService */
  151.         $systemConfigService $this->container->get(SystemConfigService::class);
  152.         $smtpConfigs = [
  153.             ConfigService::CORE_MAILER_AGENT_CONFIG,
  154.             ConfigService::CORE_MAILER_HOST_CONFIG,
  155.             ConfigService::CORE_MAILER_PORT_CONFIG,
  156.             ConfigService::CORE_MAILER_USERNAME_CONFIG,
  157.             ConfigService::CORE_MAILER_PASSWORD_CONFIG,
  158.             ConfigService::CORE_MAILER_SENDER_CONFIG,
  159.             ConfigService::CORE_MAILER_ENCRYPTION_CONFIG,
  160.             ConfigService::CORE_MAILER_AUTHENTICATION_CONFIG
  161.         ];
  162.         foreach ($systemConfigs as $systemConfig) {
  163.             if ($systemConfig->getConfigurationValue()) {
  164.                 $salesChannelId $systemConfig->getSalesChannelId();
  165.                 foreach ($smtpConfigs as $config) {
  166.                     $systemConfigService->delete($config$salesChannelId);
  167.                 }
  168.             }
  169.         }
  170.         foreach ($smtpConfigs as $config) {
  171.             $systemConfigService->delete($config);
  172.         }
  173.     }
  174.     /**
  175.      * @return IntegrationService|null
  176.      */
  177.     private function getIntegrationService(): ?IntegrationService
  178.     {
  179.         if (empty($this->integrationService)) {
  180.             if ($this->container->has(IntegrationService::class)) {
  181.                 /** @var IntegrationService integrationService */
  182.                 $this->integrationService $this->container->get(IntegrationService::class);
  183.             } else {
  184.                 /** @var EntityRepositoryInterface $integrationRepository */
  185.                 $integrationRepository $this->container->get('integration.repository');
  186.                 /** @var EntityRepositoryInterface $aclRoleRepository */
  187.                 $aclRoleRepository $this->container->get('acl_role.repository');
  188.                 /** @var SystemConfigService $systemConfigService */
  189.                 $systemConfigService $this->container->get(SystemConfigService::class);
  190.                 $this->integrationService = new IntegrationService($integrationRepository$aclRoleRepository$systemConfigService);
  191.             }
  192.         }
  193.         return $this->integrationService;
  194.     }
  195.     /**
  196.      * @param Context $context
  197.      * @return void
  198.      */
  199.     private function changeIntegrationAndRoleName(Context $context): void
  200.     {
  201.         $connection $this->container->get(Connection::class);
  202.         // change acl_role name
  203.         try {
  204.             $connection->update('acl_role',
  205.                 ['name' => self::ACL_ROLE_NAME],
  206.                 ['name' => self::OLD_ACL_ROLE_NAME]
  207.             );
  208.         } catch (\Throwable $e) {
  209.         }
  210.         // change integration name
  211.         try {
  212.             $integrationRepository $this->container->get('integration.repository');
  213.             $criteria = new Criteria();
  214.             $criteria->addFilter(new EqualsFilter('customFields.sendinblue'1));
  215.             $integrations $integrationRepository->search($criteria$context);
  216.             $data = [];
  217.             foreach ($integrations as $integration) {
  218.                 $data[] = [
  219.                     'id' => $integration->getId(),
  220.                     'label' => str_replace(self::OLD_INTEGRATION_LABELself::INTEGRATION_LABEL$integration->getLabel())
  221.                 ];
  222.             }
  223.             if (!empty($data)) {
  224.                 $integrationRepository->update($data$context);
  225.             }
  226.         } catch (\Throwable $e) {
  227.         }
  228.     }
  229. }