src/Service/Client/Content/VideoListingService.php line 90

Open in your IDE?
  1. <?php
  2. namespace App\Service\Client\Content;
  3. use App\Service\Client\AbstractClient;
  4. use Knp\Component\Pager\Pagination\PaginationInterface;
  5. use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
  6. use Symfony\Contracts\Cache\ItemInterface;
  7. class VideoListingService extends AbstractClient implements CacheWarmerInterface
  8. {
  9.     /**
  10.      * @return bool
  11.      */
  12.     public function isOptional(): bool
  13.     {
  14.         return true;
  15.     }
  16.     /**
  17.      * @param string $cacheDir
  18.      * @throws \Psr\Cache\InvalidArgumentException
  19.      * @throws \ReflectionException
  20.      */
  21.     public function warmUp($cacheDir)
  22.     {
  23.         $perPage 12;
  24.         foreach([123] as $page) {
  25.             $this->getNewestVideos($page$perPage);
  26.             $this->getBestOfTheMonthVideos($page$perPage);
  27.             $this->getBestOfTheWeekVideos($page$perPage);
  28.         }
  29.     }
  30.     /**
  31.      * @param int $page
  32.      * @param int $perPage
  33.      * @return PaginationInterface
  34.      * @throws \Psr\Cache\InvalidArgumentException
  35.      * @throws \ReflectionException
  36.      */
  37.     public function getNewestVideos(int $pageint $perPage): PaginationInterface
  38.     {
  39.         $key = (new \ReflectionClass($this))->getShortName() . '_' __FUNCTION__ '_'  $page '_' $perPage;
  40.         return $this->getCacheService()->get($key, function(ItemInterface $item) use ($page$perPage) {
  41.             $item->expiresAfter(600);
  42.             $response $this->requestJSON('GET''api/public/content/videos/' $page, [
  43.                 'query' => [
  44.                     'limit' => $perPage
  45.                 ]
  46.             ]);
  47.             return $this->getDefaultPagination($response$page$perPage);
  48.         });
  49.     }
  50.     /**
  51.      * @param int $page
  52.      * @param int $perPage
  53.      * @return PaginationInterface
  54.      * @throws \Psr\Cache\InvalidArgumentException
  55.      * @throws \ReflectionException
  56.      */
  57.     public function getBestOfTheWeekVideos(int $pageint $perPage): PaginationInterface
  58.     {
  59.         $key = (new \ReflectionClass($this))->getShortName() . '_' __FUNCTION__ '_'  $page '_' $perPage;
  60.         return $this->getCacheService()->get($key, function(ItemInterface $item) use ($page$perPage) {
  61.             $item->expiresAfter(60 60);
  62.             $response $this->requestJSON('GET''api/public/content/videos/bestoftheweek/' $page, [
  63.                 'query' => [
  64.                     'limit' => $perPage
  65.                 ]
  66.             ]);
  67.             return $this->getDefaultPagination($response$page$perPage);
  68.         });
  69.     }
  70.     /**
  71.      * @param int $page
  72.      * @param int $perPage
  73.      * @return PaginationInterface
  74.      * @throws \Psr\Cache\InvalidArgumentException
  75.      * @throws \ReflectionException
  76.      */
  77.     public function getBestOfTheMonthVideos(int $pageint $perPage): PaginationInterface
  78.     {
  79.         $key = (new \ReflectionClass($this))->getShortName() . '_' __FUNCTION__ '_'  $page '_' $perPage;
  80.         return $this->getCacheService()->get($key, function(ItemInterface $item) use ($page$perPage) {
  81.             $item->expiresAfter(60 60);
  82.             $response $this->requestJSON('GET''api/public/content/videos/bestofthemonth/' $page, [
  83.                 'query' => [
  84.                     'limit' => $perPage
  85.                 ]
  86.             ]);
  87.             return $this->getDefaultPagination($response$page$perPage);
  88.         });
  89.     }
  90.     /**
  91.      * @param int $category
  92.      * @param int $page
  93.      * @param int $perPage
  94.      * @return PaginationInterface
  95.      * @throws \Psr\Cache\InvalidArgumentException
  96.      * @throws \ReflectionException
  97.      */
  98.     public function getVideosByCategory(int $categoryint $pageint $perPage): PaginationInterface
  99.     {
  100.         $key = (new \ReflectionClass($this))->getShortName() . '_' __FUNCTION__ '_'  $category '_' $page '_' $perPage;
  101.         return $this->getCacheService()->get($key, function(ItemInterface $item) use ($category$page$perPage) {
  102.             $item->expiresAfter(600);
  103.             $response $this->requestJSON('GET''api/public/content/videos/category/' $category '/' $page, [
  104.                 'query' => [
  105.                     'limit' => $perPage
  106.                 ]
  107.             ]);
  108.             return $this->getDefaultPagination($response$page$perPage);
  109.         });
  110.     }
  111.     /**
  112.      * @param int $member
  113.      * @param int $page
  114.      * @param int $perPage
  115.      * @param array $statuses
  116.      * @return PaginationInterface
  117.      * @throws \Psr\Cache\InvalidArgumentException
  118.      * @throws \ReflectionException
  119.      */
  120.     public function getRecentVideosForMember(int $memberint $pageint $perPage, array $statuses): PaginationInterface
  121.     {
  122.         $key = (new \ReflectionClass($this))->getShortName() . '_' __FUNCTION__ '_' $member .
  123.             '_' $page '_' $perPage;
  124.         if(count($statuses)) {
  125.             $key .= '_' implode('_'$statuses);
  126.         }
  127.         return $this->getCacheService()->get($key, function(ItemInterface $item) use ($member$page$perPage$statuses) {
  128.             $item->expiresAfter(60 30);
  129.             $item->tag([
  130.                 'member-' $member,
  131.                 'videos',
  132.                 'videos-member-' $member
  133.             ]);
  134.             $query = [
  135.                 'limit' => $perPage,
  136.             ];
  137.             if(count($statuses)) {
  138.                 $query['statuses'] = implode(','$statuses);
  139.             }
  140.             $path 'api/public/content/amateur/' $member '/videos/' $page;
  141.             $response $this->requestJSON('GET'$path, [
  142.                 'query' => $query
  143.             ]);
  144.             return $this->getDefaultPagination($response$page$perPage);
  145.         });
  146.     }
  147.     /**
  148.      * @param int $member
  149.      * @param int $page
  150.      * @param int $perPage
  151.      * @param array $statuses
  152.      * @return PaginationInterface
  153.      * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  154.      * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  155.      * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  156.      * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  157.      */
  158.     public function getNonCachedRecentVideosForMember(int $memberint $pageint $perPage, array $statuses): PaginationInterface
  159.     {
  160.         $query = [
  161.             'limit' => $perPage,
  162.         ];
  163.         if(count($statuses)) {
  164.             $query['statuses'] = implode(','$statuses);
  165.         }
  166.         $path 'api/public/content/amateur/' $member '/videos/' $page;
  167.         $response $this->requestJSON('GET'$path, [
  168.             'query' => $query
  169.         ]);
  170.         return $this->getDefaultPagination($response$page$perPage);
  171.     }
  172.     /**
  173.      * @param int $member
  174.      * @param array $statuses
  175.      * @return int
  176.      * @throws \Psr\Cache\InvalidArgumentException
  177.      * @throws \ReflectionException
  178.      */
  179.     public function getMemberVideoCountPerStatuses(int $member, array $statuses): int
  180.     {
  181.         $perPage 15# this then uses the same cache key as the listing itself
  182.         return $this->getRecentVideosForMember($member1$perPage$statuses)->getTotalItemCount();
  183.     }
  184. }