custom/plugins/SensusCheck24Connect/src/SensusCheck24Connect.php line 15

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Sensus\Check24Connect;
  3. use Sensus\Check24Connect\Utils\Installer;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  10. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  12. class SensusCheck24Connect extends Plugin
  13. {
  14.     public function install(InstallContext $installContext): void
  15.     {
  16.         $installer $this->getInstaller();
  17.         $installer->install($installContext->getContext());
  18.     }
  19.     public function update(UpdateContext $updateContext): void
  20.     {
  21.         parent::update($updateContext);
  22.         $installer $this->getInstaller();
  23.         $installer->update($updateContext->getContext());
  24.     }
  25.     public function uninstall(UninstallContext $uninstallContext): void
  26.     {
  27.         parent::uninstall($uninstallContext);
  28.         if ($uninstallContext->keepUserData()) {
  29.             return;
  30.         }
  31.         $context $uninstallContext->getContext();
  32.         $this->removeCreatedEntities($context);
  33.         $this->removeConfiguration($context);
  34.     }
  35.     private function removeCreatedEntities(Context $context): void
  36.     {
  37.         $systemConfigService $this->container->get('Shopware\Core\System\SystemConfig\SystemConfigService');
  38.         /** @var array|null $entityMap */
  39.         $entityMap $systemConfigService->get(Installer::SYSTEM_CONFIG_KEY);
  40.         if($entityMap) {
  41.             foreach ($entityMap as $entityName => $entityIds) {
  42.                 /** @var EntityRepositoryInterface $repository */
  43.                 $repository $this->container->get($entityName '.repository');
  44.                 $ids array_map(function ($id) {
  45.                     return ['id' => $id];
  46.                 }, $entityIds);
  47.                 $repository->delete($ids$context);
  48.             }
  49.         }
  50.     }
  51.     private function removeConfiguration(Context $context): void
  52.     {
  53.         /** @var EntityRepositoryInterface $systemConfigRepository */
  54.         $systemConfigRepository $this->container->get('system_config.repository');
  55.         $criteria = (new Criteria())->addFilter(new ContainsFilter('configurationKey'$this->getName() . '.config.'));
  56.         $idSearchResult $systemConfigRepository->searchIds($criteria$context);
  57.         $ids array_map(static function ($id) {
  58.             return ['id' => $id];
  59.         }, $idSearchResult->getIds());
  60.         if ($ids === []) {
  61.             return;
  62.         }
  63.         $systemConfigRepository->delete($ids$context);
  64.     }
  65.     /**
  66.      * @return Installer
  67.      */
  68.     protected function getInstaller(): Installer
  69.     {
  70.         $systemConfigService $this->container->get('Shopware\Core\System\SystemConfig\SystemConfigService');
  71.         /** @var EntityRepositoryInterface $salesChannelRepository */
  72.         $salesChannelRepository $this->container->get('sales_channel.repository');
  73.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  74.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  75.         $installer = new Installer($systemConfigService$salesChannelRepository$customFieldSetRepository);
  76.         return $installer;
  77.     }
  78. }