src/Twig/AmateurExtension.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use App\Service\Client\User\AmateurListingService;
  4. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  5. use Twig\Extension\AbstractExtension;
  6. use Twig\TwigFunction;
  7. use Symfony\Contracts\Cache\ItemInterface;
  8. use Symfony\Contracts\Cache\CacheInterface;
  9. use Frivol\Common\JSON;
  10. class AmateurExtension extends AbstractExtension
  11. {
  12.     public function __construct(
  13.         private readonly AmateurListingService $service,
  14.         private readonly \Redis $redis,
  15.         private readonly ParameterBagInterface $params,
  16.     ) {}
  17.     public function getFunctions(): array
  18.     {
  19.         return [
  20.             new TwigFunction('randomOnlineAmateurs', [$this'getRandomOnlineAmateurs']),
  21.             new TwigFunction('isWebcamOnline', [$this'isWebcamOnline']),
  22.         ];
  23.     }
  24.     public function getRandomOnlineAmateurs(int $limit 6): ?array
  25.     {
  26.         $result $this->service->getOnlineAmateurs(15010)->getItems();
  27.         if (empty($result)) {
  28.             return null;
  29.         }
  30.         shuffle($result);
  31.         return array_slice($result0$limit);
  32.     }
  33.     public function isWebcamOnline(array $amateur): bool
  34.     {
  35.         if (!isset($amateur['id'])) {
  36.             return false;
  37.         }
  38.         try {
  39.             $json $this->redis->get($this->params->get('redis_online_livecams_key'));
  40.             if (!is_string($json)) {
  41.                 return false;
  42.             }
  43.             $data JSON::decode($json);
  44.             if (!isset($data['streams'])) {
  45.                 return false;
  46.             }
  47.             foreach ($data['streams'] as $stream) {
  48.                 if (isset($stream['amateurMemberId']) && $stream['amateurMemberId'] === $amateur['id']) {
  49.                     return true;
  50.                 }
  51.             }
  52.         } catch (\Throwable $e) {}
  53.         return false;
  54.     }
  55. }