src/Service/Client/User/AmateurListingService.php line 176

Open in your IDE?
  1. <?php
  2. namespace App\Service\Client\User;
  3. use App\Dictionary\AdType;
  4. use App\Dto\LivecamStreamsDto;
  5. use App\Service\Client\AbstractClient;
  6. use App\Service\LivecamOnlineService;
  7. use Frivol\Common\JSON;
  8. use Knp\Component\Pager\Pagination\PaginationInterface;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  10. use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
  11. use Symfony\Contracts\Cache\ItemInterface;
  12. class AmateurListingService extends AbstractClient implements CacheWarmerInterface
  13. {
  14.     /**
  15.      * @return bool
  16.      */
  17.     public function isOptional(): bool
  18.     {
  19.         return true;
  20.     }
  21.     /**
  22.      * @param string $cacheDir
  23.      * @throws \Psr\Cache\InvalidArgumentException
  24.      * @throws \ReflectionException
  25.      */
  26.     public function warmUp($cacheDir)
  27.     {
  28.         $page 1;
  29.         $perPage 15;
  30.         // for home page
  31.         $this->getTopRatedAmateurs($page$perPagefalsefalse);
  32.         $this->getRecentAmateurs($page$perPage);
  33.         $this->getOnlineAmateurs($page$perPage);
  34.     }
  35.     /**
  36.      * @param int $webmaster
  37.      * @param int $ad
  38.      * @param int $perPage
  39.      * @param array $specs
  40.      * @return PaginationInterface
  41.      * @throws \Psr\Cache\InvalidArgumentException
  42.      */
  43.     public function getAmateursForWebmasterAmateurAd(int $webmasterint $adint $perPage, array $specs): PaginationInterface
  44.     {
  45.         $key = (new \ReflectionClass($this))->getShortName() . '_' __FUNCTION__;
  46.         $key .= '_' $webmaster '_' $ad;
  47.         return $this->getCacheService()->get($key, function (ItemInterface $item) use ($webmaster$ad$perPage) {
  48.             $item->expiresAfter(60 5);
  49.             $item->tag([
  50.                 'webmaster-' $webmaster,
  51.                 'webmaster-ads-' $webmaster,
  52.                 'ad-type-' AdType::AMATEUR
  53.             ]);
  54.             $response $this->requestJSON('GET''api/public/user/amateur/amateurad/' $webmaster '/' $ad, [
  55.                 'query' => [
  56.                     'limit' => $perPage,
  57.                 ]
  58.             ]);
  59.             return $this->getDefaultPagination($response1$perPage);
  60.         });
  61.     }
  62.     /**
  63.      * @param int $page
  64.      * @param int $perPage
  65.      * @param bool $withImageOnly
  66.      * @param bool $onlineOnly
  67.      * @return PaginationInterface
  68.      * @throws \Psr\Cache\InvalidArgumentException
  69.      * @throws \ReflectionException
  70.      */
  71.     public function getTopRatedAmateurs(int $pageint $perPagebool $withImageOnlybool $onlineOnly): PaginationInterface
  72.     {
  73.         $key = (new \ReflectionClass($this))->getShortName() . '_' __FUNCTION__;
  74.         $key .= '_' $page '_' $perPage '_' . ($withImageOnly 'image' 'noimage') . '_' . ($onlineOnly 'online' 'notonline');
  75.         return $this->getCacheService()->get($key, function (ItemInterface $item) use ($page$perPage$withImageOnly$onlineOnly) {
  76.             $item->expiresAfter(600);
  77.             $item->tag([
  78.                 'livecam-subscription'
  79.             ]);
  80.             $response $this->requestJSON('GET''api/public/user/amateur/top/' $page, [
  81.                 'query' => [
  82.                     'limit' => $perPage,
  83.                     'withImageOnly' => $withImageOnly,
  84.                     'onlineOnly' => $onlineOnly
  85.                 ]
  86.             ]);
  87.             return $this->getDefaultPagination($response$page$perPage);
  88.         });
  89.     }
  90.     /**
  91.      * @param int $perPage
  92.      * @param bool $withImageOnly
  93.      * @return PaginationInterface
  94.      * @throws \Psr\Cache\InvalidArgumentException
  95.      * @throws \ReflectionException
  96.      */
  97.     public function getRandomAmateurs(int $perPagebool $withImageOnly): PaginationInterface
  98.     {
  99.         $key = (new \ReflectionClass($this))->getShortName() . '_' __FUNCTION__;
  100.         $key .= '_' $perPage '_' . ($withImageOnly 'image' 'noimage');
  101.         return $this->getCacheService()->get($key, function (ItemInterface $item) use ($perPage$withImageOnly) {
  102.             $item->expiresAfter(600);
  103.             $item->tag([
  104.                 'livecam-subscription'
  105.             ]);
  106.             $response $this->requestJSON('GET''api/public/user/amateur/random', [
  107.                 'query' => [
  108.                     'limit' => $perPage,
  109.                     'withImageOnly' => $withImageOnly,
  110.                 ]
  111.             ]);
  112.             return $this->getDefaultPagination($response1$perPage);
  113.         });
  114.     }
  115.     /**
  116.      * @param int $page
  117.      * @param int $perPage
  118.      * @return PaginationInterface
  119.      * @throws \Psr\Cache\InvalidArgumentException
  120.      * @throws \ReflectionException
  121.      */
  122.     public function getRecentAmateurs(int $pageint $perPage): PaginationInterface
  123.     {
  124.         $key = (new \ReflectionClass($this))->getShortName() . '_' __FUNCTION__ '_' $page '_' $perPage;
  125.         return $this->getCacheService()->get($key, function (ItemInterface $item) use ($page$perPage) {
  126.             $item->expiresAfter(600);
  127.             $item->tag([
  128.                 'livecam-subscription'
  129.             ]);
  130.             $response $this->requestJSON('GET''api/public/user/amateur/recent/' $page, [
  131.                 'query' => [
  132.                     'limit' => $perPage,
  133.                 ]
  134.             ]);
  135.             return $this->getDefaultPagination($response$page$perPage);
  136.         });
  137.     }
  138.     public function getLivecamAmateurs(int $pageint $perPageLivecamOnlineService $livecamOnlineService): PaginationInterface
  139.     {
  140.         $response['data'] = $livecamOnlineService->getAmateurDetails($page$perPage);
  141.         $response['total'] = $livecamOnlineService->getTotalCount();
  142.         return $this->getDefaultPagination($response$page$perPage);
  143.     }
  144.     /**
  145.      * @param int $page
  146.      * @param int $perPage
  147.      * @param int $maximumAge
  148.      * @return PaginationInterface
  149.      * @throws \Psr\Cache\InvalidArgumentException
  150.      * @throws \ReflectionException
  151.      */
  152.     public function getOnlineAmateurs(int $pageint $perPageint $maximumAge 10): PaginationInterface
  153.     {
  154.         $key = (new \ReflectionClass($this))->getShortName() . '_' __FUNCTION__ .
  155.             '_' $page '_' $perPage '_' $maximumAge;
  156.         return $this->getCacheService()->get($key, function (ItemInterface $item) use ($page$perPage$maximumAge) {
  157.             $item->expiresAfter(60 3);
  158.             $item->tag([
  159.                 'livecam-subscription'
  160.             ]);
  161.             $response $this->requestJSON('GET''api/public/user/amateur/online/' $page, [
  162.                 'query' => [
  163.                     'limit' => $perPage,
  164.                     'maximumLoginAgeMinutes' => $maximumAge,
  165.                 ]
  166.             ]);
  167.             return $this->getDefaultPagination($response$page$perPage);
  168.         });
  169.     }
  170. }