src/Twig/Components/YandexMetrika.php line 12

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Twig\Components;
  4. use App\Service\Container\ContactInfoProvider;
  5. use Symfony\Component\Security\Core\Security;
  6. use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
  7. #[AsTwigComponent('YandexMetrika')]
  8. class YandexMetrika
  9. {
  10.     public function __construct(
  11.         private readonly Security $security,
  12.         private readonly ContactInfoProvider $contactInfoProvider,
  13.     )
  14.     {
  15.     }
  16.     /**
  17.      * Получить необходимый для конкретного места ID счетчика Яндекс Метрики
  18.      */
  19.     public function getNeededHereCounterId(): string
  20.     {
  21.         // На публичных страницах один счетчик, на внутренних другой
  22.         return !$this->security->getUser()
  23.             ? (string)$this->contactInfoProvider->getInfo()->getYmPublicCounterId()
  24.             : (string)$this->contactInfoProvider->getInfo()->getYmCounterId();
  25.     }
  26. }