Skip to content

Commit

Permalink
TASK: Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
cvette committed May 19, 2018
1 parent 3bdd3cd commit 27d1eb1
Show file tree
Hide file tree
Showing 17 changed files with 1,340 additions and 262 deletions.
41 changes: 22 additions & 19 deletions Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetProxy.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
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;
use Neos\Media\Domain\Model\ImportedAsset;
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
{
/**
Expand All @@ -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());
}
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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');
}

/**
Expand All @@ -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');
Expand Down
13 changes: 7 additions & 6 deletions Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetProxyQuery.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Vette\Shutterstock\Domain\Model\AssetSource\Shutterstock;

use Neos\Media\Domain\Model\AssetSource\AssetProxyQueryInterface;
Expand Down Expand Up @@ -37,33 +38,33 @@ public function __construct(ShutterstockQuery $shutterstockQuery, ShutterstockAs
public function setOffset(int $offset): void
{
$page = floor($offset / $this->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
Expand Down
15 changes: 11 additions & 4 deletions Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetProxyQueryResult.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 ;
}

}
8 changes: 4 additions & 4 deletions Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetProxyRepository.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
namespace Vette\Shutterstock\Domain\Model\AssetSource\Shutterstock;

use Vette\Shutterstock\Domain\Service\ShutterstockQuery;
use Neos\Media\Domain\Model\AssetSource\AssetProxy\AssetProxyInterface;
use Neos\Media\Domain\Model\AssetSource\AssetProxyQueryResultInterface;
use Neos\Media\Domain\Model\AssetSource\AssetProxyRepositoryInterface;
use Neos\Media\Domain\Model\AssetSource\AssetTypeFilter;
use Neos\Media\Domain\Model\Tag;
use Vette\Shutterstock\Domain\Service\ShutterstockQuery;

/**
* Shutterstock AssetProxy Repository
Expand Down Expand Up @@ -38,8 +38,8 @@ public function __construct(ShutterstockAssetSource $assetSource)
*/
public function getAssetProxy(string $identifier): AssetProxyInterface
{
$data = $this->assetSource->getClient()->getById($identifier);
return new ShutterstockAssetProxy($data, $this->assetSource);
$image = $this->assetSource->getClient()->getImageById($identifier);
return new ShutterstockAssetProxy($image, $this->assetSource);
}

/**
Expand Down Expand Up @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions Classes/Domain/Model/AssetSource/Shutterstock/ShutterstockAssetSource.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down Expand Up @@ -146,7 +146,7 @@ public function isRemoveImageIdFromPreview(): bool
}

/**
* @param bool $removeImageDetailsFromPreview
* @param bool $removeImageIdFromPreview
*/
public function setRemoveImageIdFromPreview(bool $removeImageIdFromPreview): void
{
Expand Down
94 changes: 94 additions & 0 deletions Classes/Domain/Model/Shutterstock/AbstractAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Vette\Shutterstock\Domain\Model\Shutterstock;

/**
* Abstract Asset
*/
class AbstractAsset
{

/**
* @var string
*/
protected $id;

/**
* @var int
*/
protected $height;

/**
* @var int
*/
protected $width;


/**
* AbstractAsset constructor.
*
* @param string $id
* @param int $height
* @param int $width
*/
public function __construct(string $id, int $height, int $width)
{
$this->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;
}
}
Loading

0 comments on commit 27d1eb1

Please sign in to comment.