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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\CmsPreview;
  3. use Acris\CmsPreview\DependencyInjection\CompilerPass\CmsPreviewCacheCompilerPass;
  4. use Doctrine\DBAL\Connection;
  5. use Shopware\Core\Defaults;
  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\MultiFilter;
  11. use Shopware\Core\Framework\Plugin;
  12. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  13. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. class AcrisCmsPreviewCS extends Plugin
  16. {
  17.     public function activate(ActivateContext $context): void
  18.     {
  19.         $this->insertDefaultPreviewProfiles($context->getContext());
  20.     }
  21.     public function build(ContainerBuilder $container): void
  22.     {
  23.         parent::build($container);
  24.         $container->addCompilerPass(new CmsPreviewCacheCompilerPass());
  25.     }
  26.     private function insertDefaultPreviewProfiles(Context $context): void
  27.     {
  28.         /** @var EntityRepositoryInterface $previewRepository */
  29.         $previewRepository $this->container->get('acris_cms_preview_profile.repository');
  30.         $salesChannelRepository $this->container->get('sales_channel.repository');
  31.         $criteria = new Criteria();
  32.         $criteria->addAssociation('domains');
  33.         $salesChannelEntities $salesChannelRepository->search($criteria$context)->getEntities();
  34.         $profileData = [];
  35.         foreach ($salesChannelEntities as $channel) {
  36.             if ($channel->getTypeId() !== Defaults::SALES_CHANNEL_TYPE_API) {
  37.                 if (!empty($channel->getDomains()->getElements())) {
  38.                     $profileData[] = [
  39.                         'internalName' => $channel->getName(),
  40.                         'active' => true,
  41.                         'position' => 10,
  42.                         'salesChannelId' => $channel->getId(),
  43.                         'salesChannelDomainId' => $channel->getDomains()->first()->getId(),
  44.                         'currencyId' => $channel->getCurrencyId(),
  45.                         'customerGroupId' => $channel->getCustomerGroupId()
  46.                     ];
  47.                 }
  48.             }
  49.         }
  50.         foreach ($profileData as $profile) {
  51.             $this->createIfNotExists($previewRepository, [['name' => 'internalName''value' => $profile['internalName']]], $profile$context);
  52.         }
  53.     }
  54.     private function createIfNotExists(EntityRepositoryInterface $repository, array $equalFields, array $dataContext $context): void
  55.     {
  56.         $filters = [];
  57.         foreach ($equalFields as $equalField) {
  58.             $filters[] = new EqualsFilter($equalField['name'], $equalField['value']);
  59.         }
  60.         if (sizeof($filters) > 1) {
  61.             $filter = new MultiFilter(MultiFilter::CONNECTION_OR$filters);
  62.         } else {
  63.             $filter array_shift($filters);
  64.         }
  65.         $searchResult $repository->search((new Criteria())->addFilter($filter), $context);
  66.         if (empty($searchResult->first())) {
  67.             $repository->upsert([$data], $context);
  68.         }
  69.     }
  70.     public function uninstall(UninstallContext $context): void
  71.     {
  72.         if ($context->keepUserData()) {
  73.             return;
  74.         }
  75.         $this->cleanupDatabase();
  76.     }
  77.     private function cleanupDatabase(): void
  78.     {
  79.         $connection $this->container->get(Connection::class);
  80.         $connection->executeStatement('DROP TABLE IF EXISTS acris_cms_preview_profile');
  81.     }
  82. }