<?php
namespace App\Service\Client\Content;
use App\Service\Client\AbstractClient;
use Knp\Component\Pager\Pagination\PaginationInterface;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Symfony\Contracts\Cache\ItemInterface;
class VideoListingService 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->getNewestVideos($page, $perPage);
$this->getBestOfTheMonthVideos($page, $perPage);
$this->getBestOfTheWeekVideos($page, $perPage);
}
}
/**
* @param int $page
* @param int $perPage
* @return PaginationInterface
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function getNewestVideos(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);
$response = $this->requestJSON('GET', 'api/public/content/videos/' . $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 getBestOfTheWeekVideos(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);
$response = $this->requestJSON('GET', 'api/public/content/videos/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 getBestOfTheMonthVideos(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);
$response = $this->requestJSON('GET', 'api/public/content/videos/bestofthemonth/' . $page, [
'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 getVideosByCategory(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);
$response = $this->requestJSON('GET', 'api/public/content/videos/category/' . $category . '/' . $page, [
'query' => [
'limit' => $perPage
]
]);
return $this->getDefaultPagination($response, $page, $perPage);
});
}
/**
* @param int $member
* @param int $page
* @param int $perPage
* @param array $statuses
* @return PaginationInterface
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function getRecentVideosForMember(int $member, int $page, int $perPage, array $statuses): PaginationInterface
{
$key = (new \ReflectionClass($this))->getShortName() . '_' . __FUNCTION__ . '_' . $member .
'_' . $page . '_' . $perPage;
if(count($statuses)) {
$key .= '_' . implode('_', $statuses);
}
return $this->getCacheService()->get($key, function(ItemInterface $item) use ($member, $page, $perPage, $statuses) {
$item->expiresAfter(60 * 30);
$item->tag([
'member-' . $member,
'videos',
'videos-member-' . $member
]);
$query = [
'limit' => $perPage,
];
if(count($statuses)) {
$query['statuses'] = implode(',', $statuses);
}
$path = 'api/public/content/amateur/' . $member . '/videos/' . $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 $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 getNonCachedRecentVideosForMember(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 . '/videos/' . $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 getMemberVideoCountPerStatuses(int $member, array $statuses): int
{
$perPage = 15; # this then uses the same cache key as the listing itself
return $this->getRecentVideosForMember($member, 1, $perPage, $statuses)->getTotalItemCount();
}
}