<?php
namespace App\Service\Client\Content;
use App\Event\Content\CategoryAssignmentChangeEvent;
use App\Service\Client\AbstractClient;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Symfony\Contracts\Cache\ItemInterface;
class CategoryService 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)
{
$this->getTree();
}
/**
* @return array
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function getTree(): array
{
$key = (new \ReflectionClass($this))->getShortName() . '_' . __FUNCTION__;
return $this->getCacheService()->get($key, function(ItemInterface $item) {
$item->expiresAfter(600);
$response = $this->requestJSON('GET', 'api/public/content/category/tree');
if(is_array($response)) {
return $response['data'];
}
return [];
});
}
/**
* @param int $categoryId
* @return array
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function getCategoryGroupByCategoryId(int $categoryId): ?array
{
$tree = $this->getTree();
foreach($tree as $id => $group) {
foreach($group['categories'] as $category) {
if($category['id'] == $categoryId) {
return $group;
}
}
}
return null;
}
/**
* @param int $categoryId
* @return array|null
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function getCategoryById(int $categoryId): ?array
{
$tree = $this->getTree();
foreach($tree as $id => $group) {
foreach($group['categories'] as $category) {
if($category['id'] == $categoryId) {
return $category;
}
}
}
return null;
}
/**
* @return array
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function getKeyValueStore(): array
{
$key = (new \ReflectionClass($this))->getShortName() . '_' . __FUNCTION__;
return $this->getCacheService()->get($key, function(ItemInterface $item) {
$item->expiresAfter(600);
$result = [];
foreach ($this->getTree() as $node) {
foreach ($node['categories'] as $category) {
$slugs = [];
foreach (['slug', 'video_slug', 'imageset_slug'] as $possibleSlug) {
if (isset($category[$possibleSlug])) {
$slugs[$possibleSlug] = $category[$possibleSlug];
}
}
$result[$category['id']] = [
'name' => $category['name'],
'slugs' => $slugs
];
}
}
return $result;
});
}
/**
* @param string $slug
* @return array|null
* @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 getCategoryBySlug(string $slug): ?array
{
return $this->requestJSON('GET', 'api/public/content/category/category/' . $slug);
}
/**
* @param string $slug
* @return array|null
* @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 getGroupBySlug(string $slug): ?array
{
return $this->requestJSON('GET', 'api/public/content/category/group/' . $slug);
}
/**
* @param int $content
* @return bool
* @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 removeAssignedContentCategories(int $content): bool
{
$response = $this->issueDelete('api/content/category/removeassignments/' . $content);
$result = $this->isSuccess($response);
if($result) {
$this->getEventDispatcher()->dispatch(new CategoryAssignmentChangeEvent(0, $content));
}
return $result;
}
/**
* @param array $content
* @return int[]
*/
public function extractAssignedCategoryIds(array $content): array
{
$ids = [];
if(isset($content['categories'])) {
foreach($content['categories'] as $relations) {
$ids[] = (int) $relations['category']['id'];
}
}
return $ids;
}
/**
* @param int $category
* @param int $content
* @return bool
* @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 assignCategoryToContent(int $category, int $content): bool
{
$path = 'api/content/category/addassignment/' . $content;
$response = $this->issuePost($path, [
'category' => $category
]);
$result = $this->isSuccess($response);
if($result) {
$this->getEventDispatcher()->dispatch(new CategoryAssignmentChangeEvent($category, $content));
}
return $result;
}
}