vendor/store.shopware.com/tonurpackstation6/src/Subscriber/SystemConfigSubscriber.php line 34

Open in your IDE?
  1. <?php
  2. namespace Tonur\Packstation\Subscriber;
  3. use Exception;
  4. use Psr\Log\LoggerAwareTrait;
  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\System\CustomField\CustomFieldEntity;
  10. use Shopware\Core\System\SystemConfig\Event\SystemConfigChangedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Tonur\Packstation\TonurPackstation6;
  13. class SystemConfigSubscriber implements EventSubscriberInterface
  14. {
  15.     private EntityRepositoryInterface $customFieldRepository;
  16.     use LoggerAwareTrait;
  17.     public function __construct(EntityRepositoryInterface $customFieldRepository)
  18.     {
  19.         $this->customFieldRepository $customFieldRepository;
  20.     }
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             SystemConfigChangedEvent::class => 'onSystemConfigChanged'
  25.         ];
  26.     }
  27.     public function onSystemConfigChanged(SystemConfigChangedEvent $event)
  28.     {
  29.         $key TonurPackstation6::BUNDLE_NAME '.config.invertDeliveryEnabled';
  30.         if ($key !== $event->getKey()) {
  31.             return;
  32.         }
  33.         if ($event->getSalesChannelId() != null) {
  34.             return;
  35.         }
  36.         try {
  37.             $criteria = new Criteria();
  38.             $criteria->addFilter(new EqualsFilter('name'TonurPackstation6::CUSTOMFIELD_PACKSTATION_DELIVERY_ENABLED));
  39.             $context Context::createDefaultContext();
  40.             /** @var CustomFieldEntity $repertusPackstationDeliveryEnabled */
  41.             $repertusPackstationDeliveryEnabled $this->customFieldRepository->search($criteria$context)->first();
  42.             if (!$repertusPackstationDeliveryEnabled || !($config $repertusPackstationDeliveryEnabled->getConfig())) {
  43.                 return;
  44.             }
  45.             if (!$event->getValue()) {
  46.                 $config['label'] = [
  47.                     "de-DE" => "Lieferung an Packstation möglich",
  48.                     "en-GB" => "Delivery to packstation is possible"
  49.                 ];
  50.             } else {
  51.                 $config['label'] = [
  52.                     "de-DE" => "Lieferung an Packstation nicht möglich",
  53.                     "en-GB" => "delivery to packstation is not possible"
  54.                 ];
  55.             }
  56.             $this->customFieldRepository->update([[
  57.                 'id' => $repertusPackstationDeliveryEnabled->getId(),
  58.                 'config' => $config
  59.             ]], $context);
  60.         } catch (Exception $ex) {
  61.             $this->logger->error(__METHOD__ ': an error occurred.', ['message' => $ex->getMessage(), 'exception' => $ex]);
  62.         }
  63.     }
  64. }