src/Service/Client/Content/CategoryService.php line 78

Open in your IDE?
  1. <?php
  2. namespace App\Service\Client\Content;
  3. use App\Event\Content\CategoryAssignmentChangeEvent;
  4. use App\Service\Client\AbstractClient;
  5. use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
  6. use Symfony\Contracts\Cache\ItemInterface;
  7. class CategoryService extends AbstractClient implements CacheWarmerInterface
  8. {
  9.     /**
  10.      * @return bool
  11.      */
  12.     public function isOptional(): bool
  13.     {
  14.         return true;
  15.     }
  16.     /**
  17.      * @param string $cacheDir
  18.      * @throws \Psr\Cache\InvalidArgumentException
  19.      * @throws \ReflectionException
  20.      */
  21.     public function warmUp($cacheDir)
  22.     {
  23.         $this->getTree();
  24.     }
  25.     /**
  26.      * @return array
  27.      * @throws \Psr\Cache\InvalidArgumentException
  28.      * @throws \ReflectionException
  29.      */
  30.     public function getTree(): array
  31.     {
  32.         $key = (new \ReflectionClass($this))->getShortName() . '_' __FUNCTION__;
  33.         return $this->getCacheService()->get($key, function(ItemInterface $item) {
  34.             $item->expiresAfter(600);
  35.             $response $this->requestJSON('GET''api/public/content/category/tree');
  36.             if(is_array($response)) {
  37.                 return $response['data'];
  38.             }
  39.             return [];
  40.         });
  41.     }
  42.     /**
  43.      * @param int $categoryId
  44.      * @return array
  45.      * @throws \Psr\Cache\InvalidArgumentException
  46.      * @throws \ReflectionException
  47.      */
  48.     public function getCategoryGroupByCategoryId(int $categoryId): ?array
  49.     {
  50.         $tree $this->getTree();
  51.         foreach($tree as $id => $group) {
  52.             foreach($group['categories'] as $category) {
  53.                 if($category['id'] == $categoryId) {
  54.                     return $group;
  55.                 }
  56.             }
  57.         }
  58.         return null;
  59.     }
  60.     /**
  61.      * @param int $categoryId
  62.      * @return array|null
  63.      * @throws \Psr\Cache\InvalidArgumentException
  64.      * @throws \ReflectionException
  65.      */
  66.     public function getCategoryById(int $categoryId): ?array
  67.     {
  68.         $tree $this->getTree();
  69.         foreach($tree as $id => $group) {
  70.             foreach($group['categories'] as $category) {
  71.                 if($category['id'] == $categoryId) {
  72.                     return $category;
  73.                 }
  74.             }
  75.         }
  76.         return null;
  77.     }
  78.     /**
  79.      * @return array
  80.      * @throws \Psr\Cache\InvalidArgumentException
  81.      * @throws \ReflectionException
  82.      */
  83.     public function getKeyValueStore(): array
  84.     {
  85.         $key = (new \ReflectionClass($this))->getShortName() . '_' __FUNCTION__;
  86.         return $this->getCacheService()->get($key, function(ItemInterface $item) {
  87.             $item->expiresAfter(600);
  88.             $result = [];
  89.             foreach ($this->getTree() as $node) {
  90.                 foreach ($node['categories'] as $category) {
  91.                     $slugs = [];
  92.                     foreach (['slug''video_slug''imageset_slug'] as $possibleSlug) {
  93.                         if (isset($category[$possibleSlug])) {
  94.                             $slugs[$possibleSlug] = $category[$possibleSlug];
  95.                         }
  96.                     }
  97.                     $result[$category['id']] = [
  98.                         'name' => $category['name'],
  99.                         'slugs' => $slugs
  100.                     ];
  101.                 }
  102.             }
  103.             return $result;
  104.         });
  105.     }
  106.     /**
  107.      * @param string $slug
  108.      * @return array|null
  109.      * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  110.      * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  111.      * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  112.      * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  113.      */
  114.     public function getCategoryBySlug(string $slug): ?array
  115.     {
  116.         return $this->requestJSON('GET''api/public/content/category/category/' $slug);
  117.     }
  118.     /**
  119.      * @param string $slug
  120.      * @return array|null
  121.      * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  122.      * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  123.      * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  124.      * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  125.      */
  126.     public function getGroupBySlug(string $slug): ?array
  127.     {
  128.         return $this->requestJSON('GET''api/public/content/category/group/' $slug);
  129.     }
  130.     /**
  131.      * @param int $content
  132.      * @return bool
  133.      * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  134.      * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  135.      * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  136.      * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  137.      */
  138.     public function removeAssignedContentCategories(int $content): bool
  139.     {
  140.         $response $this->issueDelete('api/content/category/removeassignments/' $content);
  141.         $result $this->isSuccess($response);
  142.         if($result) {
  143.             $this->getEventDispatcher()->dispatch(new CategoryAssignmentChangeEvent(0$content));
  144.         }
  145.         return $result;
  146.     }
  147.     /**
  148.      * @param array $content
  149.      * @return int[]
  150.      */
  151.     public function extractAssignedCategoryIds(array $content): array
  152.     {
  153.         $ids = [];
  154.         if(isset($content['categories'])) {
  155.             foreach($content['categories'] as $relations) {
  156.                 $ids[] = (int) $relations['category']['id'];
  157.             }
  158.         }
  159.         return $ids;
  160.     }
  161.     /**
  162.      * @param int $category
  163.      * @param int $content
  164.      * @return bool
  165.      * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  166.      * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  167.      * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  168.      * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  169.      */
  170.     public function assignCategoryToContent(int $categoryint $content): bool
  171.     {
  172.         $path 'api/content/category/addassignment/' $content;
  173.         $response $this->issuePost($path, [
  174.             'category' => $category
  175.         ]);
  176.         $result $this->isSuccess($response);
  177.         if($result) {
  178.             $this->getEventDispatcher()->dispatch(new CategoryAssignmentChangeEvent($category$content));
  179.         }
  180.         return $result;
  181.     }
  182. }