<?php
namespace App\Service\Client\User;
use App\Dictionary\AdType;
use App\Dto\LivecamStreamsDto;
use App\Service\Client\AbstractClient;
use App\Service\LivecamOnlineService;
use Frivol\Common\JSON;
use Knp\Component\Pager\Pagination\PaginationInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Symfony\Contracts\Cache\ItemInterface;
class AmateurListingService 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)
{
$page = 1;
$perPage = 15;
// for home page
$this->getTopRatedAmateurs($page, $perPage, false, false);
$this->getRecentAmateurs($page, $perPage);
$this->getOnlineAmateurs($page, $perPage);
}
/**
* @param int $webmaster
* @param int $ad
* @param int $perPage
* @param array $specs
* @return PaginationInterface
* @throws \Psr\Cache\InvalidArgumentException
*/
public function getAmateursForWebmasterAmateurAd(int $webmaster, int $ad, int $perPage, array $specs): PaginationInterface
{
$key = (new \ReflectionClass($this))->getShortName() . '_' . __FUNCTION__;
$key .= '_' . $webmaster . '_' . $ad;
return $this->getCacheService()->get($key, function (ItemInterface $item) use ($webmaster, $ad, $perPage) {
$item->expiresAfter(60 * 5);
$item->tag([
'webmaster-' . $webmaster,
'webmaster-ads-' . $webmaster,
'ad-type-' . AdType::AMATEUR
]);
$response = $this->requestJSON('GET', 'api/public/user/amateur/amateurad/' . $webmaster . '/' . $ad, [
'query' => [
'limit' => $perPage,
]
]);
return $this->getDefaultPagination($response, 1, $perPage);
});
}
/**
* @param int $page
* @param int $perPage
* @param bool $withImageOnly
* @param bool $onlineOnly
* @return PaginationInterface
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function getTopRatedAmateurs(int $page, int $perPage, bool $withImageOnly, bool $onlineOnly): PaginationInterface
{
$key = (new \ReflectionClass($this))->getShortName() . '_' . __FUNCTION__;
$key .= '_' . $page . '_' . $perPage . '_' . ($withImageOnly ? 'image' : 'noimage') . '_' . ($onlineOnly ? 'online' : 'notonline');
return $this->getCacheService()->get($key, function (ItemInterface $item) use ($page, $perPage, $withImageOnly, $onlineOnly) {
$item->expiresAfter(600);
$item->tag([
'livecam-subscription'
]);
$response = $this->requestJSON('GET', 'api/public/user/amateur/top/' . $page, [
'query' => [
'limit' => $perPage,
'withImageOnly' => $withImageOnly,
'onlineOnly' => $onlineOnly
]
]);
return $this->getDefaultPagination($response, $page, $perPage);
});
}
/**
* @param int $perPage
* @param bool $withImageOnly
* @return PaginationInterface
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function getRandomAmateurs(int $perPage, bool $withImageOnly): PaginationInterface
{
$key = (new \ReflectionClass($this))->getShortName() . '_' . __FUNCTION__;
$key .= '_' . $perPage . '_' . ($withImageOnly ? 'image' : 'noimage');
return $this->getCacheService()->get($key, function (ItemInterface $item) use ($perPage, $withImageOnly) {
$item->expiresAfter(600);
$item->tag([
'livecam-subscription'
]);
$response = $this->requestJSON('GET', 'api/public/user/amateur/random', [
'query' => [
'limit' => $perPage,
'withImageOnly' => $withImageOnly,
]
]);
return $this->getDefaultPagination($response, 1, $perPage);
});
}
/**
* @param int $page
* @param int $perPage
* @return PaginationInterface
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function getRecentAmateurs(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([
'livecam-subscription'
]);
$response = $this->requestJSON('GET', 'api/public/user/amateur/recent/' . $page, [
'query' => [
'limit' => $perPage,
]
]);
return $this->getDefaultPagination($response, $page, $perPage);
});
}
public function getLivecamAmateurs(int $page, int $perPage, LivecamOnlineService $livecamOnlineService): PaginationInterface
{
$response['data'] = $livecamOnlineService->getAmateurDetails($page, $perPage);
$response['total'] = $livecamOnlineService->getTotalCount();
return $this->getDefaultPagination($response, $page, $perPage);
}
/**
* @param int $page
* @param int $perPage
* @param int $maximumAge
* @return PaginationInterface
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function getOnlineAmateurs(int $page, int $perPage, int $maximumAge = 10): PaginationInterface
{
$key = (new \ReflectionClass($this))->getShortName() . '_' . __FUNCTION__ .
'_' . $page . '_' . $perPage . '_' . $maximumAge;
return $this->getCacheService()->get($key, function (ItemInterface $item) use ($page, $perPage, $maximumAge) {
$item->expiresAfter(60 * 3);
$item->tag([
'livecam-subscription'
]);
$response = $this->requestJSON('GET', 'api/public/user/amateur/online/' . $page, [
'query' => [
'limit' => $perPage,
'maximumLoginAgeMinutes' => $maximumAge,
]
]);
return $this->getDefaultPagination($response, $page, $perPage);
});
}
}