vendor/crehler/arrangements/src/CrehlerArrangements.php line 32

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
  4.  *
  5.  * @author    Jakub MedyƄski <jme@crehler.com>
  6.  * @support   Crehler <support@crehler.com>
  7.  * @created   27 lip 2020
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Crehler\Arrangements;
  13. use Crehler\Arrangements\Lifecycle\Install;
  14. use Shopware\Core\Framework\Context;
  15. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
  19. use Shopware\Core\Framework\Plugin;
  20. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  21. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  22. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  23. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  24. use Shopware\Core\System\CustomField\CustomFieldTypes;
  25. use Symfony\Component\Config\FileLocator;
  26. use Symfony\Component\DependencyInjection\ContainerBuilder;
  27. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  28. class CrehlerArrangements extends Plugin
  29. {
  30.     public const CATEGORY_CUSTOM_FIELDS = [
  31.         'name' => 'crehler_category_is_arrangements',
  32.         'type' => CustomFieldTypes::BOOL,
  33.     ];
  34.     public function build(ContainerBuilder $container): void
  35.     {
  36.         parent::build($container);
  37.         $loader = new XmlFileLoader($container, new FileLocator(__DIR__ '/DependencyInjection/'));
  38.         $loader->load('logger.xml');
  39.         $loader->load('entity.xml');
  40.         $loader->load('subscriber.xml');
  41.         $loader->load('service.xml');
  42.         $loader->load('snippets.xml');
  43.         $loader->load('message.xml');
  44.     }
  45.     public function install(InstallContext $installContext): void
  46.     {
  47.         $install = new Install($this->container);
  48.         $install->run($installContext->getContext());
  49.         parent::install($installContext);
  50.     }
  51.     public function update(UpdateContext $updateContext): void
  52.     {
  53.         $install = new Install($this->container);
  54.         $install->run($updateContext->getContext());
  55.         parent::update($updateContext);
  56.     }
  57.     public function activate(ActivateContext $activateContext): void
  58.     {
  59.         $this->activateCustomField(self::CATEGORY_CUSTOM_FIELDS$activateContext->getContext(), $this->container->get('custom_field.repository'));
  60.     }
  61.     public function deactivate(DeactivateContext $deactivateContext): void
  62.     {
  63.     }
  64.     private function activateCustomField(array $customFieldContext $contextEntityRepositoryInterface $customFieldRepository): void
  65.     {
  66.         $customFieldIds $this->getCustomFieldIds($customField$context$customFieldRepository);
  67.         if ($customFieldIds->getTotal() !== 0) {
  68.             return;
  69.         }
  70.         $customFieldRepository->upsert([$customField], $context);
  71.     }
  72.     private function getCustomFieldIds(array $customFieldContext $contextEntityRepositoryInterface $customFieldRepository): IdSearchResult
  73.     {
  74.         $criteria = new Criteria();
  75.         $criteria->addFilter(new EqualsFilter('name'$customField['name']));
  76.         return $customFieldRepository->searchIds($criteria$context);
  77.     }
  78. }