Skip to content

Commit

Permalink
Merge pull request #29 from flownative/task/improve-codebase
Browse files Browse the repository at this point in the history
Improve codebase
  • Loading branch information
kdambekalns authored Sep 7, 2022
2 parents eb01fff + d235084 commit 3a81c7f
Show file tree
Hide file tree
Showing 15 changed files with 305 additions and 286 deletions.
89 changes: 22 additions & 67 deletions Classes/AssetSource/CantoAssetProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@

use Exception;
use Flownative\Canto\Exception\AuthenticationFailedException;
use Flownative\Canto\Exception\MissingClientSecretException;
use Flownative\OAuth2\Client\OAuthClientException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Uri;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Mvc\Routing\Exception\MissingActionNameException;
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 @@ -36,65 +39,18 @@
*/
final class CantoAssetProxy implements AssetProxyInterface, HasRemoteOriginalInterface, SupportsIptcMetadataInterface
{
/**
* @var CantoAssetSource
*/
private $assetSource;

/**
* @var string
*/
private $identifier;

/**
* @var string
*/
private $label;

/**
* @var string
*/
private $filename;

/**
* @var \DateTime
*/
private $lastModified;

/**
* @var int
*/
private $fileSize;

/**
* @var string
*/
private $mediaType;

/**
* @var array
*/
private $iptcProperties = [];

/**
* @var string
*/
private $previewUri;

/**
* @var int
*/
private $widthInPixels;

/**
* @var int
*/
private $heightInPixels;

/**
* @var array
*/
private $tags = [];
private CantoAssetSource $assetSource;
private string $identifier;
private string $label;
private string $filename;
private \DateTime $lastModified;
private int $fileSize;
private string $mediaType;
private array $iptcProperties = [];
private string $previewUri;
private int $widthInPixels;
private int $heightInPixels;
private array $tags = [];

/**
* @Flow\Inject
Expand Down Expand Up @@ -198,47 +154,46 @@ public function getHeightInPixels(): ?int
}

/**
* @return UriInterface
* @throws ThumbnailServiceException
*/
public function getThumbnailUri(): ?UriInterface
{
$thumbnailConfiguration = $this->thumbnailService->getThumbnailConfigurationForPreset('Neos.Media.Browser:Thumbnail');
return new Uri(sprintf(
'%s/%d',
preg_replace('|/[0-9]+$|', '', $this->previewUri),
preg_replace('|/\d+$|', '', $this->previewUri),
max($thumbnailConfiguration->getMaximumWidth(), $thumbnailConfiguration->getMaximumHeight())
));
}

/**
* @return UriInterface
* @throws ThumbnailServiceException
*/
public function getPreviewUri(): ?UriInterface
{
$previewConfiguration = $this->thumbnailService->getThumbnailConfigurationForPreset('Neos.Media.Browser:Preview');
return new Uri(sprintf(
'%s/%d',
preg_replace('|/[0-9]+$|', '', $this->previewUri),
preg_replace('|/\d+$|', '', $this->previewUri),
max($previewConfiguration->getMaximumWidth(), $previewConfiguration->getMaximumHeight())
));
}

/**
* @return resource
* @throws AuthenticationFailedException
* @throws OAuthClientException
* @throws GuzzleException
* @throws OAuthClientException
* @throws MissingClientSecretException
* @throws IdentityProviderException
* @throws \Neos\Flow\Http\Exception
* @throws MissingActionNameException
*/
public function getImportStream()
{
return fopen((string)$this->assetSource->getCantoClient()->directUri($this->identifier), 'rb');
}

/**
* @return string
*/
public function getLocalAssetIdentifier(): ?string
{
$importedAsset = $this->importedAssetRepository->findOneByAssetSourceIdentifierAndRemoteAssetIdentifier($this->assetSource->getIdentifier(), $this->identifier);
Expand Down
120 changes: 50 additions & 70 deletions Classes/AssetSource/CantoAssetProxyQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
*/

use Flownative\Canto\Exception\AuthenticationFailedException;
use Flownative\Canto\Exception\MissingClientSecretException;
use Flownative\OAuth2\Client\OAuthClientException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Response;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use Neos\Cache\Exception as CacheException;
use Neos\Cache\Exception\InvalidDataException;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Http\Exception;
use Neos\Flow\Mvc\Routing\Exception\MissingActionNameException;
use Neos\Media\Domain\Model\AssetCollection;
use Neos\Media\Domain\Model\AssetSource\AssetProxyQueryInterface;
use Neos\Media\Domain\Model\AssetSource\AssetProxyQueryResultInterface;
Expand All @@ -31,61 +35,26 @@
*/
final class CantoAssetProxyQuery implements AssetProxyQueryInterface
{
/**
* @var string
*/
private $searchTerm = '';

/**
* @var Tag
*/
private $activeTag;

/**
* @var string
*/
private $tagQuery = '';

/**
* @var AssetCollection
*/
private $activeAssetCollection;

/**
* @var string
*/
private $assetTypeFilter = 'All';

/**
* @var array
*/
private $orderings = [];

/**
* @var int
*/
private $offset = 0;

/**
* @var int
*/
private $limit = 30;
private string $searchTerm = '';
private ?Tag $activeTag = null;
private string $tagQuery = '';
private ?AssetCollection $activeAssetCollection = null;
private string $assetTypeFilter = 'All';
private array $orderings = [];
private int $offset = 0;
private int $limit = 30;

/**
* @Flow\InjectConfiguration(path="mapping", package="Flownative.Canto")
* @var array
*/
protected $mapping = [];
protected array $mapping = [];

/**
* @Flow\Inject
* @var SystemLoggerInterface
*/
protected $logger;

/**
* @param CantoAssetSource $assetSource
*/
public function __construct(private CantoAssetSource $assetSource)
{
}
Expand Down Expand Up @@ -169,9 +138,14 @@ public function execute(): AssetProxyQueryResultInterface
}

/**
* @throws OAuthClientException
* @throws GuzzleException
* @throws AuthenticationFailedException
* @throws Exception
* @throws GuzzleException
* @throws IdentityProviderException
* @throws MissingActionNameException
* @throws MissingClientSecretException
* @throws OAuthClientException
* @throws \JsonException
*/
public function count(): int
{
Expand All @@ -188,6 +162,8 @@ public function count(): int
* @throws CacheException
* @throws InvalidDataException
* @throws AuthenticationFailedException
* @throws \JsonException
* @throws \Exception
*/
public function getArrayResult(): array
{
Expand All @@ -209,9 +185,14 @@ public function getArrayResult(): array
}

/**
* @throws OAuthClientException
* @throws GuzzleException
* @throws AuthenticationFailedException
* @throws Exception
* @throws GuzzleException
* @throws IdentityProviderException
* @throws MissingActionNameException
* @throws MissingClientSecretException
* @throws OAuthClientException
* @throws \JsonException
*/
private function sendSearchRequest(int $limit, array $orderings): Response
{
Expand All @@ -221,31 +202,25 @@ private function sendSearchRequest(int $limit, array $orderings): Response

$searchTerm = $this->searchTerm;

switch ($this->assetTypeFilter) {
case 'Image':
$formatTypes = ['image'];
break;
case 'Video':
$formatTypes = ['video'];
break;
case 'Audio':
$formatTypes = ['audio'];
break;
case 'Document':
$formatTypes = ['document'];
break;
case 'All':
default:
$formatTypes = ['image', 'video', 'audio', 'document', 'presentation', 'other'];
break;
}
$formatTypes = match ($this->assetTypeFilter) {
'Image' => ['image'],
'Video' => ['video'],
'Audio' => ['audio'],
'Document' => ['document'],
default => ['image', 'video', 'audio', 'document', 'presentation', 'other'],
};
return $this->assetSource->getCantoClient()->search($searchTerm, $formatTypes, $this->tagQuery, $this->offset, $limit, $orderings);
}

/**
* @throws OAuthClientException
* @throws GuzzleException
* @throws AuthenticationFailedException
* @throws Exception
* @throws GuzzleException
* @throws IdentityProviderException
* @throws MissingActionNameException
* @throws MissingClientSecretException
* @throws OAuthClientException
* @throws \JsonException
*/
public function prepareTagQuery(): void
{
Expand Down Expand Up @@ -276,9 +251,14 @@ public function prepareTagQuery(): void
}

/**
* @throws OAuthClientException
* @throws GuzzleException
* @throws AuthenticationFailedException
* @throws GuzzleException
* @throws OAuthClientException
* @throws MissingClientSecretException
* @throws \JsonException
* @throws IdentityProviderException
* @throws Exception
* @throws MissingActionNameException
*/
public function prepareUntaggedQuery(): void
{
Expand Down
Loading

0 comments on commit 3a81c7f

Please sign in to comment.