vendor/dvdw/events/src/DvdwEvents.php line 19

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Dvdw\Events;
  3. use Doctrine\DBAL\Connection;
  4. use Dvdw\CustomFieldInstaller\System\CustomField\CustomFieldInstaller;
  5. use Dvdw\Events\Content\Media\DvdwMediaService;
  6. use Dvdw\Events\Content\Product\ProductCustomFields;
  7. use Shopware\Core\Content\Media\Aggregate\MediaFolderConfiguration\MediaFolderConfigurationDefinition;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\RepositoryIterator;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Core\Framework\Plugin;
  14. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  15. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  16. class DvdwEvents extends Plugin
  17. {
  18.     private ?CustomFieldInstaller $customFieldInstaller null;
  19.     //Need to use interfaces because of decorators
  20.     private ?EntityRepositoryInterface $mediaRepo null;
  21.     private ?EntityRepositoryInterface $mediaFolderRepo null;
  22.     private ?EntityRepositoryInterface $thumbSizeRepo null;
  23.     public function postInstall(InstallContext $installContext): void
  24.     {
  25.         parent::postInstall($installContext);
  26.         $context $installContext->getContext();
  27.         try {
  28.             $this->createCustomFields($context);
  29.             $this->createMediaFolder($context);
  30.         } catch (\Throwable $e) {
  31.             echo ($e->getMessage()) . PHP_EOL;
  32.         }
  33.     }
  34.     public function uninstall(UninstallContext $uninstallContext): void
  35.     {
  36.         $uninstallContext->setAutoMigrate(false);
  37.         parent::uninstall($uninstallContext);
  38.         if ($uninstallContext->keepUserData()) {
  39.             return;
  40.         }
  41.         $context $uninstallContext->getContext();
  42.         $this->deleteCustomFields($context);
  43.         $this->deleteMediaFolderAndContents($context);
  44.         /** @var Connection $connection */
  45.         $connection $this->container->get('Doctrine\DBAL\Connection');
  46.         foreach ($uninstallContext->getMigrationCollection()->getMigrationSteps() as $migrationStep) {
  47.             $migrationStep->updateDestructive($connection);
  48.         }
  49.         $this->removeMigrations();
  50.     }
  51.     private function createCustomFields(Context $context): void
  52.     {
  53.         $this->setCustomFieldInstaller();
  54.         $this->customFieldInstaller->install(
  55.             ProductCustomFields::SET_NAME,
  56.             ProductCustomFields::FIELDS,
  57.             $context
  58.         );
  59.     }
  60.     private function deleteCustomFields(Context $context): void
  61.     {
  62.         $this->setCustomFieldInstaller();
  63.         $this->customFieldInstaller->uninstall(ProductCustomFields::SET_NAME$context);
  64.     }
  65.     private function setCustomFieldInstaller(): void
  66.     {
  67.         if (isset($this->customFieldInstaller)) {
  68.             return;
  69.         }
  70.         $this->customFieldInstaller $this->container->get(
  71.             'Dvdw\CustomFieldInstaller\System\CustomField\CustomFieldInstaller'
  72.         );
  73.     }
  74.     private function createMediaFolder(Context $context): void
  75.     {
  76.         $this->setMediaRepos();
  77.         if ($this->getParentMediaFolderId($context) !== null) {
  78.             return;
  79.         }
  80.         $thumbIds $this->thumbSizeRepo->searchIds(new Criteria(), $context)->getIds();
  81.         $thumbIds array_map(
  82.             static function(string $id) {
  83.                 return ['id' => $id];
  84.             }
  85.         , $thumbIds);
  86.         $this->mediaFolderRepo->create([
  87.             [
  88.                 'name' => DvdwMediaService::PARENT_FOLDER_NAME,
  89.                 'configuration' => (new MediaFolderConfigurationDefinition())->getDefaults(),
  90.                 'mediaThumbnailSizes' => $thumbIds,
  91.                 'useParentConfiguration' => false
  92.             ]
  93.         ], $context);
  94.     }
  95.     private function deleteMediaFolderAndContents(Context $context): void
  96.     {
  97.         $this->setMediaRepos();
  98.         $criteria = (new Criteria())->addFilter(new EqualsFilter('mediaFolder.name'DvdwMediaService::PARENT_FOLDER_NAME));
  99.         $criteria->setLimit(250);
  100.         $iterator = new RepositoryIterator($this->mediaRepo$context$criteria);
  101.         while (($ids $iterator->fetchIds()) !== null) {
  102.             $toDelete array_map(
  103.                 static function(string $id) {
  104.                     return ['id' => $id];
  105.                 }
  106.             , $ids);
  107.             $this->mediaRepo->delete($toDelete$context);
  108.         }
  109.         $folderId $this->getParentMediaFolderId($context);
  110.         if ($folderId === null) {
  111.             return;
  112.         }
  113.         $this->mediaFolderRepo->delete([['id' => $folderId]], $context);
  114.     }
  115.     private function setMediaRepos(): void
  116.     {
  117.         if (!isset($this->mediaRepo)) {
  118.             $this->mediaRepo $this->container->get('media.repository');
  119.         }
  120.         if (!isset($this->mediaFolderRepo)) {
  121.             $this->mediaFolderRepo $this->container->get('media_folder.repository');
  122.         }
  123.         if (!isset($this->thumbSizeRepo)) {
  124.             $this->thumbSizeRepo $this->container->get('media_thumbnail_size.repository');
  125.         }
  126.     }
  127.     private function getParentMediaFolderId(Context $context): ?string
  128.     {
  129.         $this->setMediaRepos();
  130.         $criteria = (new Criteria())->addFilter(new EqualsFilter('name'DvdwMediaService::PARENT_FOLDER_NAME));
  131.         $criteria->setLimit(1);
  132.         return $this->mediaFolderRepo->searchIds($criteria$context)->firstId();
  133.     }
  134. }