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

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