src/Service/Client/Content/ImagesetListingService.php line 44

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