vendor/shopware/core/Profiling/Subscriber/ActiveRulesDataCollectorSubscriber.php line 77

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Profiling\Subscriber;
  3. use Shopware\Core\Content\Rule\RuleEntity;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\Log\Package;
  9. use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
  10. use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\VarDumper\Cloner\Data;
  15. use Symfony\Contracts\Service\ResetInterface;
  16. /**
  17.  * @internal
  18.  */
  19. #[Package('core')]
  20. class ActiveRulesDataCollectorSubscriber extends AbstractDataCollector implements EventSubscriberInterfaceResetInterface
  21. {
  22.     private EntityRepositoryInterface $ruleRepository;
  23.     /**
  24.      * @var array<string>
  25.      */
  26.     private array $ruleIds = [];
  27.     public function __construct(EntityRepositoryInterface $ruleRepository)
  28.     {
  29.         $this->ruleRepository $ruleRepository;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             SalesChannelContextResolvedEvent::class => 'onContextResolved',
  35.         ];
  36.     }
  37.     public function reset(): void
  38.     {
  39.         parent::reset();
  40.         $this->ruleIds = [];
  41.     }
  42.     /**
  43.      * @return array<string, RuleEntity>|Data<string, RuleEntity>
  44.      */
  45.     public function getData()
  46.     {
  47.         return $this->data;
  48.     }
  49.     public function getMatchingRuleCount(): int
  50.     {
  51.         if ($this->data instanceof Data) {
  52.             return $this->data->count();
  53.         }
  54.         return \count($this->data);
  55.     }
  56.     public function collect(Request $requestResponse $response, ?\Throwable $exception null): void
  57.     {
  58.         $this->data $this->getMatchingRules();
  59.     }
  60.     public static function getTemplate(): string
  61.     {
  62.         return '@Profiling/Collector/rules.html.twig';
  63.     }
  64.     public function onContextResolved(SalesChannelContextResolvedEvent $event): void
  65.     {
  66.         $this->ruleIds $event->getContext()->getRuleIds();
  67.     }
  68.     /**
  69.      * @return array<string, Entity>
  70.      */
  71.     private function getMatchingRules(): array
  72.     {
  73.         if (empty($this->ruleIds)) {
  74.             return [];
  75.         }
  76.         $criteria = new Criteria($this->ruleIds);
  77.         return $this->ruleRepository->search($criteriaContext::createDefaultContext())->getElements();
  78.     }
  79. }