src/Service/Client/Content/ImagesetService.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Service\Client\Content;
  3. use App\Dictionary\ContentPrice;
  4. use App\Event\Content\ContentCreatedEvent;
  5. use App\Event\Content\ContentDeletedEvent;
  6. use App\Service\Client\AbstractClient;
  7. use Symfony\Component\HttpFoundation\File\UploadedFile;
  8. use Symfony\Component\HttpFoundation\ParameterBag;
  9. use Symfony\Contracts\Cache\ItemInterface;
  10. class ImagesetService extends AbstractClient
  11. {
  12.     const ID_CACHE_LIFETIME 10# minutes
  13.     /**
  14.      * @param string $slug
  15.      * @return array|null
  16.      * @throws \Psr\Cache\InvalidArgumentException
  17.      * @throws \ReflectionException
  18.      */
  19.     public function getImagesetBySlug(string $slug): ?array
  20.     {
  21.         $key = (new \ReflectionClass($this))->getShortName() . '_' __FUNCTION__ '_' $slug;
  22.         return $this->getCacheService()->get($key, function(ItemInterface $item) use ($slug) {
  23.             $result $this->requestJSON('GET''api/public/content/imageset/slug/' $slug);
  24.             $item->expiresAfter(60 self::ID_CACHE_LIFETIME);
  25.             $item->tag([
  26.                 'imagesets',
  27.                 'imageset-slug-' $slug,
  28.                 'imageset-' . (int) $result['id']
  29.             ]);
  30.             return $result;
  31.         });
  32.     }
  33.     /**
  34.      * @param int $id
  35.      * @param bool $useCache
  36.      * @return array|null
  37.      * @throws \Psr\Cache\InvalidArgumentException
  38.      * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  39.      * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  40.      * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  41.      * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  42.      */
  43.     public function getImagesetById(int $idbool $useCache true): ?array
  44.     {
  45.         if (!$useCache) {
  46.             return $this->requestJSON('GET''api/public/content/imageset/' $id);
  47.         }
  48.         $key = (new \ReflectionClass($this))->getShortName() . '_' __FUNCTION__ '_' $id;
  49.         return $this->getCacheService()->get($key, function(ItemInterface $item) use ($id) {
  50.             $item->expiresAfter(60 self::ID_CACHE_LIFETIME);
  51.             $item->tag([
  52.                 'imagesets',
  53.                 'imageset-' $id
  54.             ]);
  55.             return $this->requestJSON('GET''api/public/content/imageset/' $id);
  56.         });
  57.     }
  58.     /**
  59.      * @param int $imageset
  60.      * @param ParameterBag $bag
  61.      * @return bool
  62.      * @throws \Psr\Cache\InvalidArgumentException
  63.      * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  64.      * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  65.      * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  66.      * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  67.      */
  68.     public function updateImageset(int $imageset, array $formData): bool
  69.     {
  70.         $fields = [];
  71.         foreach(['title''description'] as $field) {
  72.             if(array_key_exists($field$formData)) {
  73.                 $fields[$field] = $formData[$field];
  74.             }
  75.         }
  76.         if (!isset($formData['price']) || $formData['price'] < ContentPrice::MIN_IMAGE) {
  77.             $formData['price'] = ContentPrice::MIN_IMAGE;
  78.         }
  79.         $fields['cost_per_unit'] = $formData['price'];
  80.         if(!count($fields)) {
  81.             return false;
  82.         }
  83.         $response $this->issuePut('api/content/imageset/' $imageset$fields);
  84.         $result $this->isSuccess($response);
  85.         if($result) {
  86.             $this->cacheService->clearImagesetCache($imageset);
  87.         }
  88.         return $result;
  89.     }
  90.     /**
  91.      * @param int $author
  92.      * @param int $imagesetId
  93.      * @return bool
  94.      * @throws \Psr\Cache\InvalidArgumentException
  95.      * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  96.      * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  97.      * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  98.      * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  99.      */
  100.     public function deleteImageset(int $authorint $imagesetId): bool
  101.     {
  102.         $response $this->issueDelete('api/content/imageset/' $imagesetId);
  103.         $result $this->isSuccess($response);
  104.         if($result) {
  105.             $event = new ContentDeletedEvent($author);
  106.             $this->getEventDispatcher()->dispatch($event);
  107.         }
  108.         return $result;
  109.     }
  110.     /**
  111.      * @param int $author
  112.      * @param int $imageset
  113.      * @param int $id
  114.      * @return bool
  115.      * @throws \Psr\Cache\InvalidArgumentException
  116.      * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  117.      * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  118.      * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  119.      * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  120.      */
  121.     public function deleteImagesetImage(int $authorint $imagesetint $id): bool
  122.     {
  123.         $response $this->issueDelete('api/content/imageset/' $imageset '/images/' $id);
  124.         $result $this->isSuccess($response);
  125.         if($result) {
  126.             $this->cacheService->clearImagesetCache($imageset);
  127.         }
  128.         return $result;
  129.     }
  130.     /**
  131.      * @param int $imagesetId
  132.      * @return bool
  133.      * @throws \Psr\Cache\InvalidArgumentException
  134.      */
  135.     public function clearImagesetCache(int $imagesetId): bool
  136.     {
  137.         return $this->cacheService->clearImagesetCache($imagesetId);
  138.     }
  139.     /**
  140.      * @param int $memberId
  141.      * @param array $specs
  142.      * @return array|null
  143.      * @throws \Psr\Cache\InvalidArgumentException
  144.      * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  145.      * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  146.      * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  147.      * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  148.      */
  149.     public function createImageset(int $memberId, array $specs): ?array
  150.     {
  151.         $path 'api/content/amateur/' $memberId '/imagesets';
  152.         $response $this->issuePost($path$specs);
  153.         if($this->isSuccess($response)) {
  154.             $this->getEventDispatcher()->dispatch(new ContentCreatedEvent($memberId));
  155.         }
  156.         return $this->getResponseJson($response);
  157.     }
  158.     /**
  159.      * @param int $imageset
  160.      * @param UploadedFile $file
  161.      * @return int
  162.      * @throws \Psr\Cache\InvalidArgumentException
  163.      * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  164.      * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  165.      * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  166.      * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  167.      */
  168.     public function addImageToImageset(int $imagesetUploadedFile $file): int
  169.     {
  170.         $path 'api/content/imageset/' $imageset '/images';
  171.         $response $this->issuePostMultipart($path, [], [
  172.             'file' => $file
  173.         ]);
  174.         $result $this->getResponseJson($response);
  175.         if($result && count($result)) {
  176.             $this->cacheService->clearImagesetCache($imageset);
  177.             return $result['id'];
  178.         }
  179.         return 0;
  180.     }
  181.     /**
  182.      * @param int $memberId
  183.      * @param string $imagesetSlug
  184.      * @param int $imagesetId
  185.      * @return array
  186.      * @throws \Psr\Cache\InvalidArgumentException
  187.      */
  188.     public function purchasedImagesByMember(int $memberIdstring $imagesetSlugint $imagesetId): array
  189.     {
  190.         $path sprintf('api/content/mediacenter/%d/purchased/images/%s'$memberId$imagesetSlug);
  191.         $key str_replace('/''-'$path);
  192.         return $this->getCacheService()->get($key, function (ItemInterface $item) use ($path$imagesetId) {
  193.             $response $this->requestJSON('GET'$path);
  194.             $item->tag("imageset-purchase-$imagesetId");
  195.             $item->expiresAfter(120);
  196.             return $response['data'] ?? [];
  197.         });
  198.     }
  199. }