vendor/store.shopware.com/odsnews/src/odsNews.php line 18

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Onedrop\News;
  4. use Doctrine\DBAL\Connection;
  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\MultiFilter;
  10. use Shopware\Core\Framework\Plugin;
  11. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  12. /**
  13.  * @SuppressWarnings(PHPMD.CamelCaseClassName)
  14.  */
  15. class odsNews extends Plugin
  16. {
  17.     public function uninstall(UninstallContext $uninstallContext): void
  18.     {
  19.         parent::uninstall($uninstallContext);
  20.         if ($uninstallContext->keepUserData()) {
  21.             return;
  22.         }
  23.         /* Delete all categories with the type "news" */
  24.         $this->deleteNewsCategory();
  25.         /* Delete cms block with type news-full-listing & news-short-listing */
  26.         $this->deleteCmsBlocks();
  27.         /* Delete cms slots with type news-full-listing & news-short-listing */
  28.         $this->deleteCmsSlots();
  29.         /* Drop the database tables */
  30.         $this->dropDatabaseTable();
  31.     }
  32.     protected function dropDatabaseTable(): void
  33.     {
  34.         /** @var Connection $connection */
  35.         $connection $this->container->get(Connection::class);
  36.         $connection->executeStatement('SET FOREIGN_KEY_CHECKS=0;');
  37.         $connection->executeStatement('DROP TABLE IF EXISTS `ods_news_manufacturer`');
  38.         $connection->executeStatement('DROP TABLE IF EXISTS `ods_news`');
  39.         $connection->executeStatement('DROP TABLE IF EXISTS `ods_news_translation`');
  40.         $connection->executeStatement('SET FOREIGN_KEY_CHECKS=1;');
  41.     }
  42.     private function deleteNewsCategory(): void
  43.     {
  44.         /** @var EntityRepositoryInterface $categoryRepository */
  45.         $categoryRepository $this->container->get('category.repository');
  46.         $criteria = (new Criteria())
  47.             ->addFilter(new EqualsFilter('category.type''news'));
  48.         $idSearchResult $categoryRepository->searchIds($criteriaContext::createDefaultContext());
  49.         if (empty($idSearchResult->getIds())) {
  50.             return;
  51.         }
  52.         $categoryIds \array_map(static function ($categoryId) {
  53.             return ['id' => $categoryId];
  54.         }, $idSearchResult->getIds());
  55.         $categoryRepository->delete($categoryIdsContext::createDefaultContext());
  56.     }
  57.     private function deleteCmsBlocks(): void
  58.     {
  59.         $cmsBlockRepository $this->container->get('cms_block.repository');
  60.         $criteria = (new Criteria())
  61.             ->addFilter(
  62.                 new MultiFilter(
  63.                     MultiFilter::CONNECTION_OR,
  64.                     [
  65.                         new EqualsFilter('cms_block.type''news-full-listing'),
  66.                         new EqualsFilter('cms_block.type''news-short-listing'),
  67.                     ]
  68.                 )
  69.             );
  70.         $idSearchResult $cmsBlockRepository->searchIds($criteriaContext::createDefaultContext());
  71.         if (empty($idSearchResult->getIds())) {
  72.             return;
  73.         }
  74.         $blockIds \array_map(static function ($blockId) {
  75.             return ['id' => $blockId];
  76.         }, $idSearchResult->getIds());
  77.         $cmsBlockRepository->delete($blockIdsContext::createDefaultContext());
  78.     }
  79.     private function deleteCmsSlots(): void
  80.     {
  81.         $cmsSlotRepository $this->container->get('cms_slot.repository');
  82.         $criteria = (new Criteria())
  83.             ->addFilter(
  84.                 new MultiFilter(
  85.                     MultiFilter::CONNECTION_OR,
  86.                     [
  87.                         new EqualsFilter('cms_slot.type''short-listing-element'),
  88.                         new EqualsFilter('cms_slot.type''full-listing-element'),
  89.                     ]
  90.                 )
  91.             );
  92.         $idSearchResult $cmsSlotRepository->searchIds($criteriaContext::createDefaultContext());
  93.         if (empty($idSearchResult->getIds())) {
  94.             return;
  95.         }
  96.         $slotIds \array_map(static function ($slotId) {
  97.             return ['id' => $slotId];
  98.         }, $idSearchResult->getIds());
  99.         $cmsSlotRepository->delete($slotIdsContext::createDefaultContext());
  100.     }
  101. }