Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP FEATURE: Custom graphql adapter #188

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Classes/Domain/Model/Dto/AssetUsageDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ final class AssetUsageDetails implements \JsonSerializable
{

private string $label;
/**
* @var array<string, string>
*/
private array $metadata;
private string $url;

Expand All @@ -39,6 +42,9 @@ public function getLabel(): string
return $this->label;
}

/**
* @return array<string, string>
*/
public function getMetadata(): array
{
return $this->metadata;
Expand Down
99 changes: 32 additions & 67 deletions Classes/GraphQL/Context/AssetSourceContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* source code.
*/

use Neos\Flow\Annotations as Flow;
use Flowpack\Media\Ui\GraphQL\Types as Types;
use Neos\Flow\Persistence\PersistenceManagerInterface;
use Neos\Media\Domain\Model\Asset;
use Neos\Media\Domain\Model\AssetInterface;
Expand All @@ -23,118 +23,83 @@
use Neos\Media\Domain\Repository\AssetRepository;
use Neos\Media\Domain\Service\AssetSourceService;
use Neos\Media\Exception\AssetSourceServiceException;
use t3n\GraphQL\Context as BaseContext;

class AssetSourceContext extends BaseContext
{

/**
* @Flow\Inject
* @var AssetRepository
*/
protected $assetRepository;

/**
* @Flow\Inject
* @var AssetSourceService
*/
protected $assetSourceService;

/**
* @Flow\Inject
* @var PersistenceManagerInterface
*/
protected $persistenceManager;
use function Wwwision\Types\instantiate;

class AssetSourceContext
{
/**
* @var array<AssetSourceInterface>
* @var AssetSourceInterface[]
*/
protected $assetSources;
protected array $assetSources = [];

/**
* @var array<AssetInterface>
* @var AssetInterface[]
*/
protected $localAssetData = [];
protected array $localAssetData = [];

/**
* @return void
*/
public function initializeObject(): void
public function __construct(
protected readonly PersistenceManagerInterface $persistenceManager,
protected readonly AssetSourceService $assetSourceService,
protected readonly AssetRepository $assetRepository,
)
{
$this->assetSources = $this->assetSourceService->getAssetSources();
}

/**
* @return array<AssetSourceInterface>
* @return AssetSourceInterface[]
*/
public function getAssetSources(): array
{
return $this->assetSources;
}

/**
* @param string $id
* @param string $assetSourceIdentifier
* @return AssetProxyInterface|null
*/
public function getAssetProxy(string $id, string $assetSourceIdentifier): ?AssetProxyInterface
public function getAssetProxy(Types\AssetId $id, Types\AssetSourceId $assetSourceIdentifier): ?AssetProxyInterface
{
$activeAssetSource = $this->getAssetSource($assetSourceIdentifier);
if (!$activeAssetSource) {
return null;
}

try {
return $activeAssetSource->getAssetProxyRepository()->getAssetProxy($id);
} catch (\Exception $e) {
// Some assetproxy repositories like the NeosAssetProxyRepository throw exceptions if an asset was not found
return $activeAssetSource->getAssetProxyRepository()->getAssetProxy($id->value);
} catch (\Exception) {
// Some assetProxy repositories like the NeosAssetProxyRepository throw exceptions if an asset was not found
return null;
}
}

/**
* @param AssetProxyInterface $assetProxy
* @return AssetInterface|null
*/
public function getAssetForProxy(AssetProxyInterface $assetProxy): ?AssetInterface
{
$assetIdentifier = $assetProxy->getLocalAssetIdentifier();

if (!$assetIdentifier) {
return null;
}
$localAssetId = Types\LocalAssetId::fromAssetProxy($assetProxy);
return $localAssetId ? $this->getAssetByLocalIdentifier($localAssetId) : null;
}

if (array_key_exists($assetIdentifier, $this->localAssetData)) {
return $this->localAssetData[$assetIdentifier];
public function getAssetByLocalIdentifier(Types\LocalAssetId $localAssetIdentifier): ?AssetInterface
{
if (array_key_exists($localAssetIdentifier->value, $this->localAssetData)) {
return $this->localAssetData[$localAssetIdentifier->value];
}

/** @var Asset $asset */
$asset = $this->assetRepository->findByIdentifier($assetIdentifier);

return $this->localAssetData[$assetIdentifier] = $asset;
$asset = $this->assetRepository->findByIdentifier($localAssetIdentifier->value);
return $this->localAssetData[$localAssetIdentifier->value] = $asset;
}

/**
* @param string $assetSourceName
* @return AssetSourceInterface|null
*/
public function getAssetSource(string $assetSourceName): ?AssetSourceInterface
public function getAssetSource(Types\AssetSourceId $assetSourceId): ?AssetSourceInterface
{
return $this->assetSources[$assetSourceName] ?? null;
return $this->assetSources[$assetSourceId->value] ?? null;
}

/**
* @param string $assetSourceIdentifier
* @param string $assetIdentifier
* @return AssetProxyInterface|null
*/
public function importAsset(string $assetSourceIdentifier, string $assetIdentifier): ?AssetProxyInterface
public function importAsset(Types\AssetSourceId $assetSourceIdentifier, Types\AssetId $assetIdentifier): ?AssetProxyInterface
{
try {
$this->assetSourceService->importAsset($assetSourceIdentifier, $assetIdentifier);
$this->assetSourceService->importAsset($assetSourceIdentifier->value, $assetIdentifier->value);
$this->persistenceManager->persistAll();
return $this->getAssetProxy($assetIdentifier, $assetSourceIdentifier);
} catch (AssetSourceServiceException | \Exception $e) {
} catch (AssetSourceServiceException|\Exception $e) {
}
return null;
}
Expand Down
Loading
Loading