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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Newsletter2go;
  3. use Doctrine\DBAL\Connection;
  4. use Newsletter2go\Entity\Newsletter2goConfig;
  5. use Shopware\Core\Framework\Api\Util\AccessKeyHelper;
  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\Plugin;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  13. use Shopware\Core\Framework\Uuid\Uuid;
  14. use Shopware\Core\System\Integration\IntegrationEntity;
  15. use Symfony\Component\Config\FileLocator;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  18. use Symfony\Component\Routing\RouteCollectionBuilder;
  19. class Newsletter2GoSW6 extends Plugin
  20. {
  21.     public function postInstall(InstallContext $context): void
  22.     {
  23.         $this->createIntegration($context->getContext());
  24.     }
  25.     public function uninstall(UninstallContext $context): void
  26.     {
  27.         try {
  28.             $this->deleteNewsletter2goIntegration();
  29.             $this->deleteNewsletter2goConfig();
  30.         } catch (\Exception $exception) {
  31.             //
  32.         }
  33.     }
  34.     private function deleteNewsletter2goIntegration()
  35.     {
  36.         $connection $this->container->get(Connection::class);
  37.         $n2gConfig $connection->executeQuery('SELECT `value` FROM `newsletter2go_config` WHERE `name` = :name', ['name' => 'accessKey'])->fetchAll();
  38.         if (!empty($n2gConfig[0]['value'])) {
  39.             $accessKey$n2gConfig[0]['value'];
  40.             /** @var EntityRepositoryInterface $integrationRepository */
  41.             $integrationRepository $this->container->get('integration.repository');
  42.             $integrationCriteria = new Criteria();
  43.             $integrationCriteria->addFilter(new EqualsFilter('accessKey'$accessKey));
  44.             $integration $integrationRepository->search($integrationCriteriaContext::createDefaultContext());
  45.             if ($integration->first()) {
  46.                 /** @var IntegrationEntity $integrationEntity */
  47.                 $integrationEntity $integration->first();
  48.                 $integrationRepository->delete([
  49.                     ['id' => $integrationEntity->getId()]
  50.                 ], Context::createDefaultContext());
  51.             }
  52.         }
  53.     }
  54.     /**
  55.      * this method drops `newsletter2go_config` table
  56.      */
  57.     private function deleteNewsletter2goConfig()
  58.     {
  59.         try {
  60.             $connection $this->container->get(Connection::class);
  61.             $connection->executeStatement('DROP TABLE IF EXISTS `newsletter2go_config`');
  62.         } catch (\Exception $exception) {
  63.             //
  64.         }
  65.     }
  66.     /**
  67.      * delete integration related to plugin and create a new integration
  68.      * @param Context $context
  69.      */
  70.     private function createIntegration(Context $context)
  71.     {
  72.         try {
  73.             /** @var EntityRepositoryInterface $integrationRepository */
  74.             $integrationRepository $this->container->get('integration.repository');
  75.             $accessKey AccessKeyHelper::generateAccessKey('integration');
  76.             $secretAccessKey AccessKeyHelper::generateSecretAccessKey();
  77.             $integrationLabel 'Newsletter2Go';
  78.             $data = [
  79.                 'label' => $integrationLabel,
  80.                 'accessKey' => $accessKey,
  81.                 'secretAccessKey' => $secretAccessKey,
  82.                 'writeAccess' => true
  83.             ];
  84.             /** @var IntegrationEntity $integrationEntity */
  85.             $integrationEntity $integrationRepository->create([$data], $context);
  86.             $connection $this->container->get(Connection::class);
  87.             $createdAtTimeStampFormat = (new \DateTime())->format('Y-m-d H:i:s');
  88.             $connection->executeStatement("INSERT INTO `newsletter2go_config` VALUES(:id, :name, :value, :createdAt, NULL) ",
  89.                 ['id' => Uuid::randomBytes(), 'name' => Newsletter2goConfig::NAME_VALUE_ACCESS_KEY'value' => $accessKey'createdAt' => $createdAtTimeStampFormat]);
  90.             $connection->executeStatement("INSERT INTO `newsletter2go_config` VALUES(:id, :name, :value, :createdAt, NULL) ",
  91.                 ['id' => Uuid::randomBytes(), 'name' => Newsletter2goConfig::NAME_VALUE_SECRET_ACCESS_KEY'value' => $secretAccessKey'createdAt' => $createdAtTimeStampFormat]);
  92.             $connection->executeStatement("INSERT INTO `newsletter2go_config` VALUES(:id, :name, :value, :createdAt, NULL) ",
  93.                 ['id' => Uuid::randomBytes(), 'name' => Newsletter2goConfig::NAME_VALUE_API_KEY'value' => md5($accessKey $secretAccessKey), 'createdAt' => $createdAtTimeStampFormat]);
  94.             $connection->executeStatement("INSERT INTO `newsletter2go_config` VALUES(:id, :name, :value, :createdAt, NULL) ",
  95.                 ['id' => Uuid::randomBytes(), 'name' => Newsletter2goConfig::NAME_VALUE_CONVERSION_TRACKING'value' => 'false''createdAt' => $createdAtTimeStampFormat]);
  96.         } catch (\Exception $exception) {
  97.             //
  98.         }
  99.     }
  100.     public function build(ContainerBuilder $container): void
  101.     {
  102.         parent::build($container);
  103.         $loader = new XmlFileLoader($container, new FileLocator(__DIR__ '/Resources/'));
  104.         $loader->load(__DIR__ '/Resources/config/services.xml');
  105.     }
  106.     public function getMigrationNamespace(): string
  107.     {
  108.         return 'Newsletter2go\Migration';
  109.     }
  110. }