<?php
namespace App\Service\Client\Groups;
use App\Controller\Groups\IndexController;
use App\Service\Client\AbstractClient;
use Knp\Component\Pager\Pagination\PaginationInterface;
use Symfony\Contracts\Cache\ItemInterface;
class GroupListingService extends AbstractClient
{
/**
* @param int $category
* @param int $page
* @param int $perPage
* @return PaginationInterface
* @throws \Psr\Cache\InvalidArgumentException
*/
public function getTopRatedGroups(int $category, int $page, int $perPage = IndexController::ITEMS_PER_PAGE): 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);
$path = 'api/public/group/index/top/' . $category . '/' . $page;
$response = $this->requestJSON('GET', $path, [
'query' => [
'limit' => $perPage,
]
]);
return $this->getDefaultPagination($response, $page, $perPage);
});
}
/**
* @param int $member
* @param int $category
* @param int $page
* @param int $perPage
* @return PaginationInterface
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function getMyGroups(int $member, int $category, int $page, int $perPage = IndexController::ITEMS_PER_PAGE): PaginationInterface
{
$key = $this->getMyGroupsCacheKey($member, $category, $page, $perPage);
return $this->getCacheService()->get($key, function(ItemInterface $item)use ($member, $category, $page, $perPage) {
$item->expiresAfter(600);
$path = 'api/group/member/' . $member . '/' . $category . '/' . $page;
$response = $this->requestJSON('GET', $path, [
'query' => [
'limit' => $perPage,
]
]);
return $this->getDefaultPagination($response, $page, $perPage);
});
}
/**
* @param int $member
* @param int $category
* @param int $page
* @param int $perPage
* @return string
* @throws \ReflectionException
*/
public static function getMyGroupsCacheKey(int $member, int $category, int $page, int $perPage = IndexController::ITEMS_PER_PAGE): string
{
return (new \ReflectionClass(self::class))->getShortName() . '_getMyGroups_' .
'_' . $member . '_' . $category . '_' . $page . '_' . $perPage;
}
/**
* @param int $category
* @param int $page
* @param int $perPage
* @return PaginationInterface
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function getPublicGroups(int $category, int $page, int $perPage = IndexController::ITEMS_PER_PAGE): 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);
$path = 'api/public/group/index/public/' . $category . '/' . $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 getInviteOnlyGroups(int $category, int $page, int $perPage = IndexController::ITEMS_PER_PAGE): 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);
$path = 'api/public/group/index/inviteonly/' . $category . '/' . $page;
$response = $this->requestJSON('GET', $path, [
'query' => [
'limit' => $perPage,
]
]);
return $this->getDefaultPagination($response, $page, $perPage);
});
}
}