diff --git a/Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetProxy.php b/Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetProxy.php old mode 100644 new mode 100755 index 0d367de..9a07fee --- a/Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetProxy.php +++ b/Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetProxy.php @@ -4,7 +4,6 @@ use Imagine\Image\Box; use Imagine\Image\ImagineInterface; use Imagine\Image\Point; -use Neos\Flow\Http\Uri; use Neos\Media\Domain\Model\AssetSource\AssetProxy\AssetProxyInterface; use Neos\Media\Domain\Model\AssetSource\AssetProxy\HasRemoteOriginalInterface; use Neos\Media\Domain\Model\AssetSource\AssetSourceInterface; @@ -12,7 +11,11 @@ use Neos\Media\Domain\Repository\ImportedAssetRepository; use Psr\Http\Message\UriInterface; use Neos\Flow\Annotations as Flow; +use Vette\Shutterstock\Domain\Model\Shutterstock\Image; +/** + * ShutterstockAssetProxy + */ class ShutterstockAssetProxy implements AssetProxyInterface, HasRemoteOriginalInterface { /** @@ -32,20 +35,20 @@ class ShutterstockAssetProxy implements AssetProxyInterface, HasRemoteOriginalIn private $importedAsset; /** - * @var array + * @var Image */ - private $shutterstockData; + private $image; /** * ShutterstockAssetProxy constructor. * - * @param array $shutterstockData + * @param Image $image * @param ShutterstockAssetSource $assetSource */ - public function __construct(array $shutterstockData, ShutterstockAssetSource $assetSource) + public function __construct(Image $image, ShutterstockAssetSource $assetSource) { - $this->shutterstockData = $shutterstockData; + $this->image = $image; $this->assetSource = $assetSource; $this->importedAsset = (new ImportedAssetRepository())->findOneByAssetSourceIdentifierAndRemoteAssetIdentifier($assetSource->getIdentifier(), $this->getIdentifier()); } @@ -57,27 +60,27 @@ public function getAssetSource(): AssetSourceInterface public function getIdentifier(): string { - return $this->shutterstockData['id']; + return $this->image->getId(); } public function getLabel(): string { - return $this->shutterstockData['description']; + return $this->image->getDescription(); } public function getFilename(): string { - return basename($this->shutterstockData['assets']['preview']['url']); + return basename($this->image->getAssetPreview('preview')->getUrl()->getPath()); } public function getLastModified(): \DateTimeInterface { - return new \DateTime($this->shutterstockData['added_date']); + return $this->image->getAddedDate(); } public function getFileSize(): int { - return $this->shutterstockData['assets']['huge_jpg']['file_size']; + return 0; } public function getMediaType(): string @@ -87,22 +90,22 @@ public function getMediaType(): string public function getWidthInPixels(): ?int { - return $this->shutterstockData['assets']['huge_jpg']['width']; + return $this->image->getAssetPreview('preview')->getWidth(); } public function getHeightInPixels(): ?int { - return $this->shutterstockData['assets']['huge_jpg']['height']; + return $this->image->getAssetPreview('preview')->getHeight(); } public function getThumbnailUri(): ?UriInterface { - return new Uri($this->shutterstockData['assets']['huge_thumb']['url']); + return $this->image->getAssetPreview('huge_thumb')->getUrl(); } public function getPreviewUri(): ?UriInterface { - return new Uri($this->shutterstockData['assets']['preview']['url']); + return $this->image->getAssetPreview('preview')->getUrl(); } public function getImportStream() @@ -111,7 +114,7 @@ public function getImportStream() return $this->removeImageId(); } - return fopen($this->shutterstockData['assets']['preview']['url'], 'r'); + return fopen($this->image->getAssetPreview('preview')->getUrl(), 'r'); } /** @@ -121,11 +124,11 @@ public function getImportStream() */ protected function removeImageId() { - $fileHandle = fopen($this->shutterstockData['assets']['preview']['url'], 'r'); + $fileHandle = fopen($this->image->getAssetPreview('preview')->getUrl(), 'r'); $image = $this->imagineService->read($fileHandle); - $width = $this->shutterstockData['assets']['preview']['width']; - $height = $this->shutterstockData['assets']['preview']['height']; + $width = $this->image->getAssetPreview('preview')->getWidth(); + $height = $this->image->getAssetPreview('preview')->getHeight(); $image->crop(new Point(0,0), new Box($width, $height)); $string = $image->get('jpg'); diff --git a/Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetProxyQuery.php b/Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetProxyQuery.php old mode 100644 new mode 100755 index 722fde1..576667f --- a/Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetProxyQuery.php +++ b/Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetProxyQuery.php @@ -1,4 +1,5 @@ getLimit()) + 1; - $this->shutterstockQuery->setPage($page); + $this->shutterstockQuery->setParameter('page', $page); } public function getOffset(): int { - $page = $this->shutterstockQuery->getPage(); + $page = $this->shutterstockQuery->getParameter('page'); return $page * $this->getLimit() + 1; } public function setLimit(int $limit): void { - $this->shutterstockQuery->setPerPage($limit); + $this->shutterstockQuery->setParameter('per_page', $limit); } public function getLimit(): int { - return $this->shutterstockQuery->getPerPage(); + return $this->shutterstockQuery->getParameter('per_page'); } public function setSearchTerm(string $searchTerm) { - $this->shutterstockQuery->setQuery($searchTerm); + $this->shutterstockQuery->setParameter('query', $searchTerm); } public function getSearchTerm() { - return $this->shutterstockQuery->getQuery(); + return $this->shutterstockQuery->getParameter('query'); } public function execute(): AssetProxyQueryResultInterface diff --git a/Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetProxyQueryResult.php b/Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetProxyQueryResult.php old mode 100644 new mode 100755 index 11bea80..00d024b --- a/Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetProxyQueryResult.php +++ b/Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetProxyQueryResult.php @@ -4,6 +4,7 @@ use Neos\Media\Domain\Model\AssetSource\AssetProxy\AssetProxyInterface; use Neos\Media\Domain\Model\AssetSource\AssetProxyQueryInterface; use Neos\Media\Domain\Model\AssetSource\AssetProxyQueryResultInterface; +use Vette\Shutterstock\Domain\Model\Shutterstock\Image; use Vette\Shutterstock\Domain\Service\ShutterstockQueryResult; /** @@ -54,9 +55,9 @@ public function toArray(): array public function current(): ?AssetProxyInterface { - $data = $this->shutterstockQueryResult->current(); - if (is_array($data)) { - return new ShutterstockAssetProxy($data, $this->assetSource); + $image = $this->shutterstockQueryResult->current(); + if ($image instanceof Image) { + return new ShutterstockAssetProxy($image, $this->assetSource); } else { return null; } @@ -106,9 +107,15 @@ public function offsetUnset($offset) throw new \RuntimeException('Unsupported operation: ' . __METHOD__, 1510060444); } + /** + * shutterstock API limits the result to 1000 images + * + * @return int + */ public function count() { - return $this->shutterstockQueryResult->count(); + $count = $this->shutterstockQueryResult->count(); + return $count > 1000 ? 1000 : $count ; } } diff --git a/Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetProxyRepository.php b/Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetProxyRepository.php old mode 100644 new mode 100755 index ee40b07..1b1b238 --- a/Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetProxyRepository.php +++ b/Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetProxyRepository.php @@ -1,12 +1,12 @@ assetSource->getClient()->getById($identifier); - return new ShutterstockAssetProxy($data, $this->assetSource); + $image = $this->assetSource->getClient()->getImageById($identifier); + return new ShutterstockAssetProxy($image, $this->assetSource); } /** @@ -72,7 +72,7 @@ public function findAll(): AssetProxyQueryResultInterface public function findBySearchTerm(string $searchTerm): AssetProxyQueryResultInterface { $query = new ShutterstockQuery(); - $query->setQuery($searchTerm); + $query->setParameter('query', $searchTerm); return new ShutterstockAssetProxyQueryResult($query->execute(), $this->assetSource); } diff --git a/Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetSource.php b/Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetSource.php old mode 100644 new mode 100755 index 9969650..b259c77 --- a/Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetSource.php +++ b/Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetSource.php @@ -63,8 +63,8 @@ public function initializeObject() $this->shutterstockClient->setApiUrl($this->assetSourceOptions['apiUrl']); } - if (isset($this->assetSourceOptions['queryParams'])) { - $this->shutterstockClient->setQueryParams($this->assetSourceOptions['queryParams']); + if (isset($this->assetSourceOptions['imageSearch'])) { + $this->shutterstockClient->setDefaultQueryParameters($this->assetSourceOptions['imageSearch']); } if (isset($this->assetSourceOptions['removeImageIdFromPreview'])) { @@ -146,7 +146,7 @@ public function isRemoveImageIdFromPreview(): bool } /** - * @param bool $removeImageDetailsFromPreview + * @param bool $removeImageIdFromPreview */ public function setRemoveImageIdFromPreview(bool $removeImageIdFromPreview): void { diff --git a/Classes/Domain/Model/Shutterstock/AbstractAsset.php b/Classes/Domain/Model/Shutterstock/AbstractAsset.php new file mode 100644 index 0000000..adb82dd --- /dev/null +++ b/Classes/Domain/Model/Shutterstock/AbstractAsset.php @@ -0,0 +1,94 @@ +id = $id; + $this->height = $height; + $this->width = $width; + } + + /** + * @return string + */ + public function getId(): string + { + return $this->id; + } + + /** + * @param string $id + * @return self + */ + public function setId(string $id): self + { + $this->id = $id; + return $this; + } + + /** + * @return int + */ + public function getHeight(): int + { + return $this->height; + } + + /** + * @param int $height + * @return self + */ + public function setHeight(int $height): self + { + $this->height = $height; + return $this; + } + + /** + * @return int + */ + public function getWidth(): int + { + return $this->width; + } + + /** + * @param int $width + * @return self + */ + public function setWidth(int $width): self + { + $this->width = $width; + return $this; + } +} diff --git a/Classes/Domain/Model/Shutterstock/Asset.php b/Classes/Domain/Model/Shutterstock/Asset.php new file mode 100644 index 0000000..9dc943a --- /dev/null +++ b/Classes/Domain/Model/Shutterstock/Asset.php @@ -0,0 +1,145 @@ +setDpi($assetResponse['dpi']); + $asset->setFormat($assetResponse['format']); + $asset->setFileSize($assetResponse['file_size']); + $asset->setDisplayName($assetResponse['display_name']); + $asset->setIsLicensable($assetResponse['is_licensable']); + + return $asset; + } + + /** + * @return string + */ + public function getDisplayName(): string + { + return $this->displayName; + } + + /** + * @param string $displayName + * @return self + */ + public function setDisplayName(string $displayName): self + { + $this->displayName = $displayName; + return $this; + } + + /** + * @return int + */ + public function getDpi(): int + { + return $this->dpi; + } + + /** + * @param int $dpi + * @return self + */ + public function setDpi(int $dpi): self + { + $this->dpi = $dpi; + return $this; + } + + /** + * @return int + */ + public function getFileSize(): int + { + return $this->fileSize; + } + + /** + * @param int $fileSize + * @return self + */ + public function setFileSize(int $fileSize): self + { + $this->fileSize = $fileSize; + return $this; + } + + /** + * @return string + */ + public function getFormat(): string + { + return $this->format; + } + + /** + * @param string $format + * @return self + */ + public function setFormat(string $format): self + { + $this->format = $format; + return $this; + } + + /** + * @return bool + */ + public function isLicensable(): bool + { + return $this->isLicensable; + } + + /** + * @param bool $isLicensable + * @return self + */ + public function setIsLicensable(bool $isLicensable): self + { + $this->isLicensable = $isLicensable; + return $this; + } +} diff --git a/Classes/Domain/Model/Shutterstock/AssetPreview.php b/Classes/Domain/Model/Shutterstock/AssetPreview.php new file mode 100644 index 0000000..47e9338 --- /dev/null +++ b/Classes/Domain/Model/Shutterstock/AssetPreview.php @@ -0,0 +1,50 @@ +setUrl(new Uri($assetResponse['url'])); + + return $assetPreview; + } + + /** + * @return Uri + */ + public function getUrl(): Uri + { + return $this->url; + } + + /** + * @param Uri $url + * @return self + */ + public function setUrl(Uri $url): self + { + $this->url = $url; + return $this; + } +} diff --git a/Classes/Domain/Model/Shutterstock/Category.php b/Classes/Domain/Model/Shutterstock/Category.php new file mode 100644 index 0000000..97eb63d --- /dev/null +++ b/Classes/Domain/Model/Shutterstock/Category.php @@ -0,0 +1,68 @@ +id = $id; + $this->name = $name; + } + + /** + * @return int + */ + public function getId(): int + { + return $this->id; + } + + /** + * @param int $id + * @return self + */ + public function setId(int $id): self + { + $this->id = $id; + return $this; + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * @param string $name + * @return self + */ + public function setName(string $name): self + { + $this->name = $name; + return $this; + } +} diff --git a/Classes/Domain/Model/Shutterstock/Contributor.php b/Classes/Domain/Model/Shutterstock/Contributor.php new file mode 100644 index 0000000..4bc8a80 --- /dev/null +++ b/Classes/Domain/Model/Shutterstock/Contributor.php @@ -0,0 +1,276 @@ +id = $id; + } + + /** + * @return int + */ + public function getId(): int + { + return $this->id; + } + + /** + * @param int $id + * @return self + */ + public function setId(int $id): self + { + $this->id = $id; + return $this; + } + + /** + * @return string + */ + public function getDisplayName(): string + { + return $this->displayName; + } + + /** + * @param string $displayName + * @return self + */ + public function setDisplayName(string $displayName): self + { + $this->displayName = $displayName; + return $this; + } + + /** + * @return string + */ + public function getAbout(): string + { + return $this->about; + } + + /** + * @param string $about + * @return self + */ + public function setAbout(string $about): self + { + $this->about = $about; + return $this; + } + + /** + * @return array + */ + public function getEquipment(): array + { + return $this->equipment; + } + + /** + * @param array $equipment + * @return self + */ + public function setEquipment(array $equipment): self + { + $this->equipment = $equipment; + return $this; + } + + /** + * @return array + */ + public function getContributorType(): array + { + return $this->contributorType; + } + + /** + * @param array $contributorType + * @return self + */ + public function setContributorType(array $contributorType): self + { + $this->contributorType = $contributorType; + return $this; + } + + /** + * @return array + */ + public function getStyles(): array + { + return $this->styles; + } + + /** + * @param array $styles + * @return self + */ + public function setStyles(array $styles): self + { + $this->styles = $styles; + return $this; + } + + /** + * @return array + */ + public function getSubjects(): array + { + return $this->subjects; + } + + /** + * @param array $subjects + * @return self + */ + public function setSubjects(array $subjects): self + { + $this->subjects = $subjects; + return $this; + } + + /** + * @return string + */ + public function getWebsite(): string + { + return $this->website; + } + + /** + * @param string $website + * @return self + */ + public function setWebsite(string $website): self + { + $this->website = $website; + return $this; + } + + /** + * @return string + */ + public function getLocation(): string + { + return $this->location; + } + + /** + * @param string $location + * @return self + */ + public function setLocation(string $location): self + { + $this->location = $location; + return $this; + } + + /** + * @return Url + */ + public function getPortfolioUrl(): Url + { + return $this->portfolioUrl; + } + + /** + * @param Url $portfolioUrl + * @return self + */ + public function setPortfolioUrl(Url $portfolioUrl): self + { + $this->portfolioUrl = $portfolioUrl; + return $this; + } + + /** + * @return array + */ + public function getSocialMedia(): array + { + return $this->socialMedia; + } + + /** + * @param array $socialMedia + * @return self + */ + public function setSocialMedia(array $socialMedia): self + { + $this->socialMedia = $socialMedia; + return $this; + } +} diff --git a/Classes/Domain/Model/Shutterstock/Image.php b/Classes/Domain/Model/Shutterstock/Image.php new file mode 100644 index 0000000..deb84f7 --- /dev/null +++ b/Classes/Domain/Model/Shutterstock/Image.php @@ -0,0 +1,392 @@ +setId($response['id']); + $image->setImageType($response['image_type']); + $image->setAddedDate(new \DateTime($response['added_date'])); + $image->setAspect($response['aspect']); + $image->setDescription($response['description']); + $image->setIsAdult($response['is_adult']); + + foreach ($response['categories'] as $category) { + $image->addCategory(new Category($category['id'], $category['name'])); + } + + if (isset($data['is_illustration'])) { + $image->setIsIllustration($response['is_illustration']); + } + + foreach ($response['assets'] as $key => $asset) { + $image->addAsset(self::createAssetOrAssetPreviewFromResponse($key, $asset)); + } + + $image->setKeywords($response['keywords']); + $image->setContributor(new Contributor($response['contributor']['id'])); + + return $image; + } + + /** + * @param $key + * @param $assetResponse + * @return AbstractAsset + */ + protected static function createAssetOrAssetPreviewFromResponse($key, $assetResponse): AbstractAsset + { + if (in_array($key, self::ASSET_PREVIEW_KEYS)) { + return AssetPreview::createFromResponse($key, $assetResponse); + } else { + return Asset::createFromResponse($key, $assetResponse); + } + } + + /** + * @return int + */ + public function getId(): int + { + return $this->id; + } + + /** + * @param int $id + * @return self + */ + public function setId(int $id): self + { + $this->id = $id; + return $this; + } + + /** + * @return \DateTime + */ + public function getAddedDate(): \DateTime + { + return $this->addedDate; + } + + /** + * @param \DateTime $addedDate + * @return self + */ + public function setAddedDate(\DateTime $addedDate): self + { + $this->addedDate = $addedDate; + return $this; + } + + /** + * @return mixed + */ + public function getAspect() + { + return $this->aspect; + } + + /** + * @param mixed $aspect + * @return self + */ + public function setAspect($aspect): self + { + $this->aspect = $aspect; + return $this; + } + + /** + * @return string + */ + public function getDescription(): string + { + return $this->description; + } + + /** + * @param string $description + * @return self + */ + public function setDescription(string $description): self + { + $this->description = $description; + return $this; + } + + /** + * @return string + */ + public function getImageType(): string + { + return $this->imageType; + } + + /** + * @param string $imageType + * @return self + */ + public function setImageType(string $imageType): self + { + $this->imageType = $imageType; + return $this; + } + + /** + * @return bool + */ + public function isAdult(): bool + { + return $this->isAdult; + } + + /** + * @param bool $isAdult + * @return self + */ + public function setIsAdult(bool $isAdult): self + { + $this->isAdult = $isAdult; + return $this; + } + + /** + * @return bool + */ + public function isIllustration(): bool + { + return $this->isIllustration; + } + + /** + * @param bool $isIllustration + * @return self + */ + public function setIsIllustration(bool $isIllustration): self + { + $this->isIllustration = $isIllustration; + return $this; + } + + /** + * @return array + */ + public function getKeywords(): array + { + return $this->keywords; + } + + /** + * @param array $keywords + * @return self + */ + public function setKeywords(array $keywords): self + { + $this->keywords = $keywords; + return $this; + } + + /** + * @return string + */ + public function getMediaType(): string + { + return $this->mediaType; + } + + /** + * @param string $mediaType + * @return self + */ + public function setMediaType(string $mediaType): self + { + $this->mediaType = $mediaType; + return $this; + } + + /** + * @return array + */ + public function getCategories(): array + { + return $this->categories; + } + + /** + * @param array $categories + * @return self + */ + public function setCategories(array $categories): self + { + $this->categories = $categories; + return $this; + } + + /** + * @param Category $category + * @return self + */ + public function addCategory(Category $category): self + { + $this->categories[] = $category; + return $this; + } + + /** + * @return Contributor + */ + public function getContributor(): Contributor + { + return $this->contributor; + } + + /** + * @param Contributor $contributor + * @return self + */ + public function setContributor(Contributor $contributor): self + { + $this->contributor = $contributor; + return $this; + } + + /** + * Adds an asset to this image + * + * @param AbstractAsset $asset + * @return self + */ + public function addAsset(AbstractAsset $asset): self + { + $id = $asset->getId(); + + switch (true) { + case $asset instanceof Asset: + $this->assets[$id] = $asset; + break; + case $asset instanceof AssetPreview: + $this->assetPreviews[$id] = $asset; + break; + } + + return $this; + } + + /** + * @return array + */ + public function getAssets(): array + { + return $this->assets; + } + + /** + * @return array + */ + public function getAssetPreviews(): array + { + return $this->assetPreviews; + } + + /** + * @param $key + * @return AssetPreview + */ + public function getAssetPreview($key): AssetPreview + { + return $this->assetPreviews[$key]; + } + + /** + * @param $key + * @return Asset + */ + public function getAsset($key): Asset + { + return $this->assets[$key]; + } +} diff --git a/Classes/Domain/Model/Shutterstock/Result/ImageSearchResult.php b/Classes/Domain/Model/Shutterstock/Result/ImageSearchResult.php new file mode 100644 index 0000000..1c7cea5 --- /dev/null +++ b/Classes/Domain/Model/Shutterstock/Result/ImageSearchResult.php @@ -0,0 +1,38 @@ +setPerPage($response['per_page']); + $result->setPage($response['page']); + $result->setTotalCount($response['total_count']); + $result->setSearchId($response['search_id']); + + $images = array(); + foreach ($response['data'] as $data) { + $images[] = Image::createFromResponse($data); + } + + $result->setData($images); + return $result; + } +} diff --git a/Classes/Domain/Model/Shutterstock/Result/PaginatedResult.php b/Classes/Domain/Model/Shutterstock/Result/PaginatedResult.php new file mode 100644 index 0000000..e12b2bd --- /dev/null +++ b/Classes/Domain/Model/Shutterstock/Result/PaginatedResult.php @@ -0,0 +1,129 @@ +page; + } + + /** + * @param int $page + * @return self + */ + public function setPage(int $page): self + { + $this->page = $page; + return $this; + } + + /** + * @return int + */ + public function getPerPage(): int + { + return $this->perPage; + } + + /** + * @param int $perPage + * @return self + */ + public function setPerPage(int $perPage): self + { + $this->perPage = $perPage; + return $this; + } + + /** + * @return int + */ + public function getTotalCount(): int + { + return $this->totalCount; + } + + /** + * @param int $totalCount + * @return self + */ + public function setTotalCount(int $totalCount): self + { + $this->totalCount = $totalCount; + return $this; + } + + /** + * @return string + */ + public function getSearchId(): string + { + return $this->searchId; + } + + /** + * @param string $searchId + * @return self + */ + public function setSearchId(string $searchId): self + { + $this->searchId = $searchId; + return $this; + } + + /** + * @return array + */ + public function getData(): array + { + return $this->data; + } + + /** + * @param array $data + * @return self + */ + public function setData(array $data): self + { + $this->data = $data; + return $this; + } +} diff --git a/Classes/Domain/Service/ShutterstockClient.php b/Classes/Domain/Service/ShutterstockClient.php old mode 100644 new mode 100755 index 044a726..63d5db5 --- a/Classes/Domain/Service/ShutterstockClient.php +++ b/Classes/Domain/Service/ShutterstockClient.php @@ -1,11 +1,14 @@ client === null) { $this->client = new GuzzleClient([ - 'base_url' => [ $this->apiUrl, []], - 'defaults' => [ - 'exceptions' => false, - 'headers' => [ - 'Authorization' => 'Basic ' . $this->getAuthorizationHash(), - 'Accept' => 'application/json' - ] + 'base_uri' => $this->apiUrl, + 'headers' => [ + 'Authorization' => 'Basic ' . $this->getAuthorizationHash(), + 'Accept' => 'application/json' ] ]); } @@ -71,57 +71,64 @@ protected function getClient() : GuzzleClient * * @return string */ - protected function getAuthorizationHash() + protected function getAuthorizationHash(): string { return base64_encode(join(':', [$this->getClientKey(), $this->getClientSecret()])); } /** - * Query Shutterstock + * Executes a shutterstock image search * * @param ShutterstockQuery $query - * @return array + * @return ImageSearchResult */ - public function query(ShutterstockQuery $query): array + public function searchImages(ShutterstockQuery $query): ImageSearchResult { if (isset($this->queryResponseCache[$query->getCacheEntryIdentifier()])) { return $this->queryResponseCache[$query->getCacheEntryIdentifier()]; } - $queryParams = $this->getQueryParams(); - if (isset($queryParams['imageType'])) { - $query->setImageType($queryParams['imageType']); - } - - if (isset($queryParams['category'])) { - $query->setCategory($queryParams['category']); - } + $query = $this->applyDefaultQueryParameters($query); - if (isset($queryParams['safe'])) { - $query->setSafe($queryParams['safe']); - } - - $response = $this->getClient()->get('images/search', ['query' => $query->getParamsArray()]); + $response = $this->getClient()->get('images/search', ['query' => $query->getParametersArray()]); $response = json_decode($response->getBody(), true); - $this->queryResponseCache[$query->getCacheEntryIdentifier()] = $response; - return $response; + return ImageSearchResult::createFromResponse($response); } /** * Gets an image by id * * @param string $identifier - * @return \GuzzleHttp\Message\FutureResponse|\GuzzleHttp\Message\ResponseInterface|mixed|null + * @return Image */ - public function getById(string $identifier) + public function getImageById(string $identifier): Image { $response = $this->getClient()->get('images/' . $identifier, [ 'query' => ['view' => 'full'] ]); + $response = json_decode($response->getBody(), true); - return $response; + return Image::createFromResponse($response); + } + + + /** + * Applies default query parameters + * + * @param ShutterstockQuery $query + * @return ShutterstockQuery + */ + protected function applyDefaultQueryParameters(ShutterstockQuery $query): ShutterstockQuery + { + $query->setParameter('view', 'full'); + + foreach ($this->defaultQueryParameters as $name => $value) { + $query->setParameter($name, $value); + } + + return $query; } /** @@ -134,10 +141,12 @@ public function getApiUrl(): string /** * @param string $apiUrl + * @return self */ - public function setApiUrl(string $apiUrl): void + public function setApiUrl(string $apiUrl): self { $this->apiUrl = $apiUrl; + return $this; } /** @@ -150,10 +159,12 @@ public function getClientKey(): string /** * @param string $clientKey + * @return self */ - public function setClientKey(string $clientKey): void + public function setClientKey(string $clientKey): self { $this->clientKey = $clientKey; + return $this; } /** @@ -166,25 +177,29 @@ public function getClientSecret(): string /** * @param string $clientSecret + * @return self */ - public function setClientSecret(string $clientSecret): void + public function setClientSecret(string $clientSecret): self { $this->clientSecret = $clientSecret; + return $this; } /** - * @return mixed + * @return array */ - public function getQueryParams() + public function getDefaultQueryParameters() { - return $this->queryParams; + return $this->defaultQueryParameters; } /** - * @param mixed $queryParams + * @param mixed $defaultQueryParameters + * @return self */ - public function setQueryParams($queryParams): void + public function setDefaultQueryParameters($defaultQueryParameters): self { - $this->queryParams = $queryParams; + $this->defaultQueryParameters = $defaultQueryParameters; + return $this; } } diff --git a/Classes/Domain/Service/ShutterstockQuery.php b/Classes/Domain/Service/ShutterstockQuery.php old mode 100644 new mode 100755 index a4fd481..b5d2839 --- a/Classes/Domain/Service/ShutterstockQuery.php +++ b/Classes/Domain/Service/ShutterstockQuery.php @@ -3,6 +3,7 @@ use Neos\Cache\CacheAwareInterface; use Neos\Flow\Annotations as Flow; +use Vette\Shutterstock\Domain\Model\Shutterstock\Result\PaginatedResult; /** * A Shutterstock API Query @@ -11,39 +12,9 @@ class ShutterstockQuery implements CacheAwareInterface { /** - * @var string + * @var array */ - protected $view = 'full'; - - /** - * @var string - */ - protected $category; - - /** - * @var string - */ - protected $imageType; - - /** - * @var bool - */ - protected $safe = true; - - /** - * @var int - */ - protected $page = 1; - - /** - * @var int - */ - protected $perPage = 1; - - /** - * @var string - */ - protected $query; + protected $parameters = array(); /** * @Flow\Inject @@ -61,167 +32,56 @@ public function execute(): ShutterstockQueryResult } /** - * @return array - */ - public function getResult() : array - { - return $this->shutterstockClient->query($this); - } - - /** - * @return int - */ - public function count(): int - { - $result = $this->shutterstockClient->query($this); - $count = $result['total_count']; - - return $count > 1000 ? 1000 : $count; - } - - /** - * @return int + * @return PaginatedResult */ - public function getPage(): int + public function getResult() : PaginatedResult { - return $this->page; + return $this->shutterstockClient->searchImages($this); } /** - * @param int $page - * @return self - */ - public function setPage(int $page): self - { - $this->page = $page; - return $this; - } - - /** - * @return int - */ - public function getPerPage(): int - { - return $this->perPage; - } - - /** - * @param int $perPage - * @return self - */ - public function setPerPage(int $perPage): self - { - $this->perPage = $perPage; - return $this; - } - - /** - * Get search term + * Gets the cache entry identifier for this query * * @return string */ - public function getQuery(): string + public function getCacheEntryIdentifier(): string { - return $this->query ? $this->query : ''; + return md5(json_encode($this->getParametersArray())); } /** - * Set search term + * Sets a query parameter * - * @param string $query - */ - public function setQuery(string $query): void - { - $this->query = $query; - } - - /** - * @return string - */ - public function getView(): string - { - return $this->view; - } - - /** - * @param string $view - */ - public function setView(string $view): void - { - $this->view = $view; - } - - /** - * @return bool - */ - public function isSafe(): bool - { - return $this->safe; - } - - /** - * @param bool $safe - */ - public function setSafe(bool $safe): void - { - $this->safe = $safe; - } - - /** - * @return string - */ - public function getCategory(): string - { - return $this->category; - } - - /** - * @param string $category - */ - public function setCategory(string $category): void - { - $this->category = $category; - } - - /** - * @return string - */ - public function getImageType(): string - { - return $this->imageType; - } - - /** - * @param string $imageType + * @param string $name + * @param $value + * @return self */ - public function setImageType(string $imageType): void + public function setParameter(string $name, $value): self { - $this->imageType = $imageType; + $this->parameters[$name] = $value; + return $this; } /** - * Gets the cache entry identifier for this query + * Gets a query parameter * - * @return string + * @param string $name + * @return mixed */ - public function getCacheEntryIdentifier(): string + public function getParameter(string $name) { - return md5(json_encode($this->getParamsArray())); + if (isset($this->parameters[$name])) { + return $this->parameters[$name]; + } + + return null; } /** * @return array */ - public function getParamsArray() + public function getParametersArray(): array { - return array_filter([ - 'view' => $this->view, - 'page' => $this->page, - 'per_page' => $this->perPage, - 'category' => $this->category, - 'image_type' => $this->imageType, - 'safe' => json_encode($this->safe), - 'query' => $this->query - ]); + return $this->parameters; } } diff --git a/Classes/Domain/Service/ShutterstockQueryResult.php b/Classes/Domain/Service/ShutterstockQueryResult.php old mode 100644 new mode 100755 index eed998d..232405c --- a/Classes/Domain/Service/ShutterstockQueryResult.php +++ b/Classes/Domain/Service/ShutterstockQueryResult.php @@ -1,4 +1,5 @@ assets)) { + if (!is_array($this->data)) { $result = $this->query->getResult(); - $this->assets = $result['data']; - $this->numberOfAssets = $this->query->count(); + $this->data = $result->getData(); + $this->totalCount = $result->getTotalCount(); } } @@ -51,7 +52,6 @@ protected function initialize() * Returns a clone of the query object * * @return ShutterstockQuery - * @api */ public function getQuery() { @@ -61,31 +61,31 @@ public function getQuery() public function current() { $this->initialize(); - return current($this->assets); + return current($this->data); } public function next() { $this->initialize(); - next($this->assets); + next($this->data); } public function key() { $this->initialize(); - key($this->assets); + key($this->data); } public function valid() { $this->initialize(); - return current($this->assets) !== false; + return current($this->data) !== false; } public function rewind() { $this->initialize(); - reset($this->assets); + reset($this->data); } public function offsetExists($offset) @@ -97,24 +97,24 @@ public function offsetExists($offset) public function offsetGet($offset) { $this->initialize(); - return isset($this->queryResult[$offset]) ? $this->assets[$offset] : null; + return isset($this->queryResult[$offset]) ? $this->data[$offset] : null; } public function offsetSet($offset, $value) { $this->initialize(); - $this->assets[$offset] = $value; + $this->data[$offset] = $value; } public function offsetUnset($offset) { $this->initialize(); - unset($this->assets[$offset]); + unset($this->data[$offset]); } public function count() { $this->initialize(); - return $this->numberOfAssets; + return $this->totalCount; } } diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml old mode 100644 new mode 100755 index 085358f..0c066c2 --- a/Configuration/Settings.yaml +++ b/Configuration/Settings.yaml @@ -7,5 +7,5 @@ Neos: label: Shutterstock Preview clientKey: client-key clientSecret: client-secret - queryParams: - imageType: photo + imageSearch: + image_type: photo