<?php
namespace App\Service\Client\Content;
use App\Dictionary\ContentPrice;
use App\Event\Content\ContentCreatedEvent;
use App\Event\Content\ContentDeletedEvent;
use App\Service\Client\AbstractClient;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Contracts\Cache\ItemInterface;
class ImagesetService extends AbstractClient
{
const ID_CACHE_LIFETIME = 10; # minutes
/**
* @param string $slug
* @return array|null
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function getImagesetBySlug(string $slug): ?array
{
$key = (new \ReflectionClass($this))->getShortName() . '_' . __FUNCTION__ . '_' . $slug;
return $this->getCacheService()->get($key, function(ItemInterface $item) use ($slug) {
$result = $this->requestJSON('GET', 'api/public/content/imageset/slug/' . $slug);
$item->expiresAfter(60 * self::ID_CACHE_LIFETIME);
$item->tag([
'imagesets',
'imageset-slug-' . $slug,
'imageset-' . (int) $result['id']
]);
return $result;
});
}
/**
* @param int $id
* @param bool $useCache
* @return array|null
* @throws \Psr\Cache\InvalidArgumentException
* @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 getImagesetById(int $id, bool $useCache = true): ?array
{
if (!$useCache) {
return $this->requestJSON('GET', 'api/public/content/imageset/' . $id);
}
$key = (new \ReflectionClass($this))->getShortName() . '_' . __FUNCTION__ . '_' . $id;
return $this->getCacheService()->get($key, function(ItemInterface $item) use ($id) {
$item->expiresAfter(60 * self::ID_CACHE_LIFETIME);
$item->tag([
'imagesets',
'imageset-' . $id
]);
return $this->requestJSON('GET', 'api/public/content/imageset/' . $id);
});
}
/**
* @param int $imageset
* @param ParameterBag $bag
* @return bool
* @throws \Psr\Cache\InvalidArgumentException
* @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 updateImageset(int $imageset, array $formData): bool
{
$fields = [];
foreach(['title', 'description'] as $field) {
if(array_key_exists($field, $formData)) {
$fields[$field] = $formData[$field];
}
}
if (!isset($formData['price']) || $formData['price'] < ContentPrice::MIN_IMAGE) {
$formData['price'] = ContentPrice::MIN_IMAGE;
}
$fields['cost_per_unit'] = $formData['price'];
if(!count($fields)) {
return false;
}
$response = $this->issuePut('api/content/imageset/' . $imageset, $fields);
$result = $this->isSuccess($response);
if($result) {
$this->cacheService->clearImagesetCache($imageset);
}
return $result;
}
/**
* @param int $author
* @param int $imagesetId
* @return bool
* @throws \Psr\Cache\InvalidArgumentException
* @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 deleteImageset(int $author, int $imagesetId): bool
{
$response = $this->issueDelete('api/content/imageset/' . $imagesetId);
$result = $this->isSuccess($response);
if($result) {
$event = new ContentDeletedEvent($author);
$this->getEventDispatcher()->dispatch($event);
}
return $result;
}
/**
* @param int $author
* @param int $imageset
* @param int $id
* @return bool
* @throws \Psr\Cache\InvalidArgumentException
* @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 deleteImagesetImage(int $author, int $imageset, int $id): bool
{
$response = $this->issueDelete('api/content/imageset/' . $imageset . '/images/' . $id);
$result = $this->isSuccess($response);
if($result) {
$this->cacheService->clearImagesetCache($imageset);
}
return $result;
}
/**
* @param int $imagesetId
* @return bool
* @throws \Psr\Cache\InvalidArgumentException
*/
public function clearImagesetCache(int $imagesetId): bool
{
return $this->cacheService->clearImagesetCache($imagesetId);
}
/**
* @param int $memberId
* @param array $specs
* @return array|null
* @throws \Psr\Cache\InvalidArgumentException
* @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 createImageset(int $memberId, array $specs): ?array
{
$path = 'api/content/amateur/' . $memberId . '/imagesets';
$response = $this->issuePost($path, $specs);
if($this->isSuccess($response)) {
$this->getEventDispatcher()->dispatch(new ContentCreatedEvent($memberId));
}
return $this->getResponseJson($response);
}
/**
* @param int $imageset
* @param UploadedFile $file
* @return int
* @throws \Psr\Cache\InvalidArgumentException
* @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 addImageToImageset(int $imageset, UploadedFile $file): int
{
$path = 'api/content/imageset/' . $imageset . '/images';
$response = $this->issuePostMultipart($path, [], [
'file' => $file
]);
$result = $this->getResponseJson($response);
if($result && count($result)) {
$this->cacheService->clearImagesetCache($imageset);
return $result['id'];
}
return 0;
}
/**
* @param int $memberId
* @param string $imagesetSlug
* @param int $imagesetId
* @return array
* @throws \Psr\Cache\InvalidArgumentException
*/
public function purchasedImagesByMember(int $memberId, string $imagesetSlug, int $imagesetId): array
{
$path = sprintf('api/content/mediacenter/%d/purchased/images/%s', $memberId, $imagesetSlug);
$key = str_replace('/', '-', $path);
return $this->getCacheService()->get($key, function (ItemInterface $item) use ($path, $imagesetId) {
$response = $this->requestJSON('GET', $path);
$item->tag("imageset-purchase-$imagesetId");
$item->expiresAfter(120);
return $response['data'] ?? [];
});
}
}