Skip to content

Commit

Permalink
Merge pull request #12 from flownative/feature/support-asset-collections
Browse files Browse the repository at this point in the history
Support asset collections for filtering
  • Loading branch information
kdambekalns authored Feb 16, 2022
2 parents 402c233 + 94237da commit 7dee437
Show file tree
Hide file tree
Showing 11 changed files with 355 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[*.md]
trim_trailing_whitespace = false
16 changes: 9 additions & 7 deletions Classes/AssetSource/PixxioAssetProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
*/

use Behat\Transliterator\Transliterator;
use GuzzleHttp\Psr7\Uri;
use Exception;
use Flownative\Pixxio\Exception\ConnectionException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Uri;
use Neos\Flow\Annotations as Flow;
use Neos\Media\Domain\Model\AssetSource\AssetProxy\AssetProxyInterface;
use Neos\Media\Domain\Model\AssetSource\AssetProxy\HasRemoteOriginalInterface;
use Neos\Media\Domain\Model\AssetSource\AssetProxy\SupportsIptcMetadataInterface;
Expand All @@ -26,7 +28,6 @@
use Neos\Media\Domain\Repository\ImportedAssetRepository;
use Neos\Utility\MediaTypes;
use Psr\Http\Message\UriInterface;
use Neos\Flow\Annotations as Flow;
use stdClass;

/**
Expand Down Expand Up @@ -152,7 +153,7 @@ public static function fromJsonObject(stdClass $jsonObject, PixxioAssetSource $a
if (isset($modifiedImagePaths[2])) {
$assetProxy->originalUri = new Uri($modifiedImagePaths[2]);
}
} else if (is_object($modifiedImagePaths)) {
} elseif (is_object($modifiedImagePaths)) {
if (isset($modifiedImagePaths->{'0'})) {
$assetProxy->thumbnailUri = new Uri($modifiedImagePaths->{'0'});
}
Expand Down Expand Up @@ -285,7 +286,8 @@ public function getPreviewUri(): ?UriInterface
}

/**
* @return resource
* @return bool|resource
* @throws ConnectionException
*/
public function getImportStream()
{
Expand All @@ -294,11 +296,11 @@ public function getImportStream()
$response = $client->request('GET', $this->originalUri);
if ($response->getStatusCode() === 200) {
return $response->getBody()->detach();
} else {
return false;
}

return false;
} catch (GuzzleException $e) {
throw new ConnectionException('Retrieving file failed: ' . $e->getMessage(), 1542808207);
throw new ConnectionException('Retrieving file failed: ' . $e->getMessage(), 1542808207, $e);
}
}

Expand Down
61 changes: 50 additions & 11 deletions Classes/AssetSource/PixxioAssetProxyQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
use Flownative\Pixxio\Exception\MissingClientSecretException;
use GuzzleHttp\Psr7\Response;
use Neos\Flow\Annotations\Inject;
use Psr\Log\LoggerInterface as SystemLoggerInterface;
use Neos\Flow\Log\ThrowableStorageInterface;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Media\Domain\Model\AssetSource\AssetProxyQueryInterface;
use Neos\Media\Domain\Model\AssetSource\AssetProxyQueryResultInterface;
use Psr\Log\LoggerInterface;

/**
*
Expand All @@ -42,6 +44,11 @@ final class PixxioAssetProxyQuery implements AssetProxyQueryInterface
*/
private $assetTypeFilter = 'All';

/**
* @var ?string
*/
private $assetCollectionFilter;

/**
* @var array
*/
Expand All @@ -64,10 +71,16 @@ final class PixxioAssetProxyQuery implements AssetProxyQueryInterface

/**
* @Inject
* @var SystemLoggerInterface
* @var LoggerInterface
*/
protected $logger;

/**
* @Inject
* @var ThrowableStorageInterface
*/
protected $throwableStorage;

/**
* @param PixxioAssetSource $assetSource
*/
Expand All @@ -92,7 +105,6 @@ public function getOffset(): int
return $this->offset;
}


/**
* @param int $limit
*/
Expand All @@ -112,7 +124,7 @@ public function getLimit(): int
/**
* @param string $searchTerm
*/
public function setSearchTerm(string $searchTerm)
public function setSearchTerm(string $searchTerm): void
{
$this->searchTerm = $searchTerm;
}
Expand All @@ -128,7 +140,7 @@ public function getSearchTerm(): string
/**
* @param string $assetTypeFilter
*/
public function setAssetTypeFilter(string $assetTypeFilter)
public function setAssetTypeFilter(string $assetTypeFilter): void
{
$this->assetTypeFilter = $assetTypeFilter;
}
Expand All @@ -141,6 +153,22 @@ public function getAssetTypeFilter(): string
return $this->assetTypeFilter;
}

/**
* @param ?string $assetCollectionFilter
*/
public function setAssetCollectionFilter(?string $assetCollectionFilter): void
{
$this->assetCollectionFilter = $assetCollectionFilter;
}

/**
* @return ?string
*/
public function getAssetCollectionFilter(): ?string
{
return $this->assetCollectionFilter;
}

/**
* @return array
*/
Expand Down Expand Up @@ -192,22 +220,30 @@ public function count(): int

if (!isset($responseObject->quantity)) {
if (isset($responseObject->help)) {
$this->logger->logException(new ConnectionException('Connection to pixx.io failed: ' . $responseObject->help, 1526629493));
$message = $this->throwableStorage->logThrowable(new ConnectionException('Query to pixx.io failed: ' . $responseObject->help, 1526629493));
$this->logger->error($message, LogEnvironment::fromMethodName(__METHOD__));
}
return 0;
}
return $responseObject->quantity;
} catch (AuthenticationFailedException $exception) {
$this->logger->logException(new ConnectionException('Connection to pixx.io failed: ' . $exception->getMessage(), 1526629541));
$message = $this->throwableStorage->logThrowable(new ConnectionException('Connection to pixx.io failed.', 1526629541, $exception));
$this->logger->error($message, LogEnvironment::fromMethodName(__METHOD__));
return 0;
} catch (MissingClientSecretException $exception) {
$this->logger->logException(new ConnectionException('Connection to pixx.io failed: ' . $exception->getMessage(), 1526629547));
$message = $this->throwableStorage->logThrowable(new ConnectionException('Connection to pixx.io failed.', 1526629547, $exception));
$this->logger->error($message, LogEnvironment::fromMethodName(__METHOD__));
return 0;
} catch (ConnectionException $exception) {
$message = $this->throwableStorage->logThrowable(new ConnectionException('Connection to pixx.io failed.', 1643823324, $exception));
$this->logger->error($message, LogEnvironment::fromMethodName(__METHOD__));
return 0;
}
}

/**
* @return PixxioAssetProxy[]
* @throws \Exception
*/
public function getArrayResult(): array
{
Expand All @@ -220,10 +256,12 @@ public function getArrayResult(): array
$assetProxies[] = PixxioAssetProxy::fromJsonObject($rawAsset, $this->assetSource);
}
} catch (AuthenticationFailedException $exception) {
$this->logger->logException(new ConnectionException('Connection to pixx.io failed: ' . $exception->getMessage(), 1526629541));
$message = $this->throwableStorage->logThrowable(new ConnectionException('Connection to pixx.io failed.', 1643822709, $exception));
$this->logger->error($message, LogEnvironment::fromMethodName(__METHOD__));
return [];
} catch (MissingClientSecretException $exception) {
$this->logger->logException(new ConnectionException('Connection to pixx.io failed: ' . $exception->getMessage(), 1526629547));
$message = $this->throwableStorage->logThrowable(new ConnectionException('Connection to pixx.io failed.', 1643822727, $exception));
$this->logger->error($message, LogEnvironment::fromMethodName(__METHOD__));
return [];
}
return $assetProxies;
Expand All @@ -235,6 +273,7 @@ public function getArrayResult(): array
* @return Response
* @throws AuthenticationFailedException
* @throws MissingClientSecretException
* @throws ConnectionException
*/
private function sendSearchRequest(int $limit, array $orderings): Response
{
Expand Down Expand Up @@ -264,6 +303,6 @@ private function sendSearchRequest(int $limit, array $orderings): Response
break;
}

return $this->assetSource->getPixxioClient()->search($searchTerm, $formatTypes, $fileTypes, $this->offset, $limit, $orderings);
return $this->assetSource->getPixxioClient()->search($searchTerm, $formatTypes, $fileTypes, $this->assetCollectionFilter, $this->offset, $limit, $orderings);
}
}
8 changes: 5 additions & 3 deletions Classes/AssetSource/PixxioAssetProxyQueryResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ public function key()
{
$this->initialize();
return $this->assetProxiesIterator->key();

}

public function valid()
Expand Down Expand Up @@ -142,15 +141,18 @@ public function offsetUnset($offset)

/**
* @return int
* @throws \Flownative\Pixxio\Exception\ConnectionException
*/
public function count(): int
{
if ($this->numberOfAssetProxies === null) {
if (is_array($this->assetProxies)) {
return count($this->assetProxies);
$this->numberOfAssetProxies = count($this->assetProxies);
} else {
return $this->query->count();
$this->numberOfAssetProxies = $this->query->count();
}
}

return $this->numberOfAssetProxies;
}
}
29 changes: 22 additions & 7 deletions Classes/AssetSource/PixxioAssetProxyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,32 @@
use Flownative\Pixxio\Exception\ConnectionException;
use Flownative\Pixxio\Exception\MissingClientSecretException;
use Neos\Cache\Frontend\StringFrontend;
use Neos\Cache\Frontend\VariableFrontend;
use Neos\Flow\ObjectManagement\DependencyInjection\DependencyProxy;
use Neos\Media\Domain\Model\AssetCollection;
use Neos\Media\Domain\Model\AssetSource\AssetNotFoundExceptionInterface;
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\AssetSourceConnectionExceptionInterface;
use Neos\Media\Domain\Model\AssetSource\AssetTypeFilter;
use Neos\Media\Domain\Model\AssetSource\SupportsCollectionsInterface;
use Neos\Media\Domain\Model\AssetSource\SupportsSortingInterface;
use Neos\Media\Domain\Model\Tag;

/**
* PixxioAssetProxyRepository
*/
class PixxioAssetProxyRepository implements AssetProxyRepositoryInterface, SupportsSortingInterface
class PixxioAssetProxyRepository implements AssetProxyRepositoryInterface, SupportsSortingInterface, SupportsCollectionsInterface
{
/**
* @var PixxioAssetSource
*/
private $assetSource;

/**
* @param PixxioAssetSource $assetSource
* @var string|null
*/
public function __construct(PixxioAssetSource $assetSource)
{
$this->assetSource = $assetSource;
}
protected $assetCollectionFilter;

/**
* @var string
Expand All @@ -64,6 +62,14 @@ public function __construct(PixxioAssetSource $assetSource)
*/
protected $assetProxyCache;

/**
* @param PixxioAssetSource $assetSource
*/
public function __construct(PixxioAssetSource $assetSource)
{
$this->assetSource = $assetSource;
}

/**
* @param string $identifier
* @return AssetProxyInterface
Expand Down Expand Up @@ -115,13 +121,19 @@ public function filterByType(AssetTypeFilter $assetType = null): void
$this->assetTypeFilter = (string)$assetType ?: 'All';
}

public function filterByCollection(AssetCollection $assetCollection = null): void
{
$this->assetCollectionFilter = $assetCollection ? $assetCollection->getTitle() : null;
}

/**
* @return AssetProxyQueryResultInterface
*/
public function findAll(): AssetProxyQueryResultInterface
{
$query = new PixxioAssetProxyQuery($this->assetSource);
$query->setAssetTypeFilter($this->assetTypeFilter);
$query->setAssetCollectionFilter($this->assetCollectionFilter);
$query->setOrderings($this->orderings);
return new PixxioAssetProxyQueryResult($query);
}
Expand All @@ -135,6 +147,7 @@ public function findBySearchTerm(string $searchTerm): AssetProxyQueryResultInter
$query = new PixxioAssetProxyQuery($this->assetSource);
$query->setSearchTerm($searchTerm);
$query->setAssetTypeFilter($this->assetTypeFilter);
$query->setAssetCollectionFilter($this->assetCollectionFilter);
$query->setOrderings($this->orderings);
return new PixxioAssetProxyQueryResult($query);
}
Expand All @@ -148,6 +161,7 @@ public function findByTag(Tag $tag): AssetProxyQueryResultInterface
$query = new PixxioAssetProxyQuery($this->assetSource);
$query->setSearchTerm($tag->getLabel());
$query->setAssetTypeFilter($this->assetTypeFilter);
$query->setAssetCollectionFilter($this->assetCollectionFilter);
$query->setOrderings($this->orderings);
return new PixxioAssetProxyQueryResult($query);
}
Expand All @@ -159,6 +173,7 @@ public function findUntagged(): AssetProxyQueryResultInterface
{
$query = new PixxioAssetProxyQuery($this->assetSource);
$query->setAssetTypeFilter($this->assetTypeFilter);
$query->setAssetCollectionFilter($this->assetCollectionFilter);
$query->setOrderings($this->orderings);
return new PixxioAssetProxyQueryResult($query);
}
Expand Down
Loading

0 comments on commit 7dee437

Please sign in to comment.