<?php
namespace App\Service\Client\Content;
use App\Dictionary\ContentStatus;
use App\Service\Client\AbstractClient;
use Knp\Component\Pager\Pagination\PaginationInterface;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Symfony\Contracts\Cache\ItemInterface;
class ImagesetListingService extends AbstractClient implements CacheWarmerInterface
{
/**
* @return bool
*/
public function isOptional(): bool
{
return true;
}
/**
* @param string $cacheDir
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function warmUp($cacheDir)
{
$perPage = 12;
foreach([1, 2, 3] as $page) {
$this->getNewestImagesets($page, $perPage);
$this->getBestOfTheMonthImagesets($page, $perPage);
$this->getBestOfTheWeekImagesets($page, $perPage);
}
}
/**
* @param int $page
* @param int $perPage
* @return PaginationInterface
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function getNewestImagesets(int $page, int $perPage): PaginationInterface
{
$key = (new \ReflectionClass($this))->getShortName() . '_' . __FUNCTION__ . '_' . $page . '_' . $perPage;
return $this->getCacheService()->get($key, function(ItemInterface $item) use ($page, $perPage) {
$item->expiresAfter(600);
$item->tag([
'imagesets',
'imagesets-new'
]);
$response = $this->requestJSON('GET', 'api/public/content/imagesets/' . $page, [
'query' => [
'limit' => $perPage
]
]);
return $this->getDefaultPagination($response, $page, $perPage);
});
}
/**
* @param int $page
* @param int $perPage
* @return PaginationInterface
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function getBestOfTheWeekImagesets(int $page, int $perPage): PaginationInterface
{
$key = (new \ReflectionClass($this))->getShortName() . '_' . __FUNCTION__ . '_' . $page . '_' . $perPage;
return $this->getCacheService()->get($key, function(ItemInterface $item) use ($page, $perPage) {
$item->expiresAfter(60 * 60);
$item->tag([
'imagesets',
'imagesets-week'
]);
$response = $this->requestJSON('GET', 'api/public/content/imagesets/bestoftheweek/' . $page, [
'query' => [
'limit' => $perPage
]
]);
return $this->getDefaultPagination($response, $page, $perPage);
});
}
/**
* @param int $page
* @param int $perPage
* @return PaginationInterface
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function getBestOfTheMonthImagesets(int $page, int $perPage): PaginationInterface
{
$key = (new \ReflectionClass($this))->getShortName() . '_' . __FUNCTION__ . '_' . $page . '_' . $perPage;
return $this->getCacheService()->get($key, function(ItemInterface $item) use ($page, $perPage) {
$item->expiresAfter(60 * 60);
$item->tag([
'imagesets',
'imagesets-month'
]);
$path = 'api/public/content/imagesets/bestofthemonth/' . $page;
$response = $this->requestJSON('GET', $path, [
'query' => [
'limit' => $perPage
]
]);
return $this->getDefaultPagination($response, $page, $perPage);
});
}
/**
* @param int $category
* @param int $page
* @param int $perPage
* @return PaginationInterface
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function getImagesetsByCategory(int $category, int $page, int $perPage): PaginationInterface
{
$key = (new \ReflectionClass($this))->getShortName() .
'_' . __FUNCTION__ . '_' . $category . '_' . $page . '_' . $perPage;
return $this->getCacheService()->get($key, function(ItemInterface $item) use ($category, $page, $perPage) {
$item->expiresAfter(600);
$item->tag([
'imagesets',
'imagesets-category-' . $category
]);
$path = 'api/public/content/imagesets/category/' . $category . '/' . $page;
$response = $this->requestJSON('GET', $path, [
'query' => [
'limit' => $perPage
]
]);
return $this->getDefaultPagination($response, $page, $perPage);
});
}
/**
* @param int $member
* @param int $page
* @param int $perPage
* @param array $statuses OPTIONAL
* @return PaginationInterface
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function getRecentImagesetsForMember(int $member, int $page, int $perPage, ?array $statuses = []): PaginationInterface
{
$key = (new \ReflectionClass($this))->getShortName() . '_'
. __FUNCTION__ . '_' . $member . '_' . $page . '_' . $perPage;
if(count($statuses)) {
$key .= '_' . md5(implode('_', $statuses));
}
return $this->getCacheService()->get($key, function(ItemInterface $item) use ($member, $page, $perPage, $statuses) {
$item->expiresAfter(60 * 30);
$item->tag([
'member-' . $member,
'imagesets',
'imagesets-member-' . $member
]);
$query = [
'limit' => $perPage,
];
if(count($statuses)) {
$query['statuses'] = implode(',', $statuses);
}
$path = 'api/public/content/amateur/' . $member . '/imagesets/' . $page;
$response = $this->requestJSON('GET', $path, [
'query' => $query
]);
return $this->getDefaultPagination($response, $page, $perPage);
});
}
/**
* @param int $member
* @param int $page
* @param int $perPage
* @param array|null $statuses
* @return PaginationInterface
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
*/
public function getNonCachedRecentImagesetsForMember(int $member, int $page, int $perPage, ?array $statuses = []): PaginationInterface
{
$query = [
'limit' => $perPage,
];
if(count($statuses)) {
$query['statuses'] = implode(',', $statuses);
}
$path = 'api/public/content/amateur/' . $member . '/imagesets/' . $page;
$response = $this->requestJSON('GET', $path, [
'query' => $query
]);
return $this->getDefaultPagination($response, $page, $perPage);
}
/**
* @param int $member
* @param array $statuses
* @return int
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function getMemberImagesetCountPerStatuses(int $member, array $statuses): int
{
$perPage = 15; # this then uses the same cache key as the listing itself
$paginator = $this->getRecentImagesetsForMember($member, 1, $perPage, $statuses);
return $paginator->getTotalItemCount();
}
}