-
-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathAssetUsageSyncService.php
70 lines (61 loc) · 2.43 KB
/
AssetUsageSyncService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
declare(strict_types=1);
namespace Neos\Neos\AssetUsage\Service;
use Neos\ContentRepository\Core\ContentRepository;
use Neos\ContentRepository\Core\Factory\ContentRepositoryServiceInterface;
use Neos\ContentRepository\Core\Projection\ContentGraph\VisibilityConstraints;
use Neos\Media\Domain\Model\AssetInterface;
use Neos\Media\Domain\Repository\AssetRepository;
use Neos\Neos\AssetUsage\Dto\AssetUsage;
use Neos\Neos\AssetUsage\Dto\AssetUsageFilter;
use Neos\Neos\AssetUsage\Dto\AssetUsages;
use Neos\Neos\AssetUsage\Projection\AssetUsageFinder;
use Neos\Neos\AssetUsage\Projection\AssetUsageRepository;
/**
* @internal
*/
class AssetUsageSyncService implements ContentRepositoryServiceInterface
{
/**
* @var array<string, bool>
*/
private array $existingAssetsById = [];
public function __construct(
private readonly ContentRepository $contentRepository,
private readonly AssetUsageFinder $assetUsageFinder,
private readonly AssetRepository $assetRepository,
private readonly AssetUsageRepository $assetUsageRepository,
) {
}
public function findAllUsages(): AssetUsages
{
return $this->assetUsageFinder->findByFilter(AssetUsageFilter::create());
}
public function removeAssetUsage(AssetUsage $assetUsage): void
{
$this->assetUsageRepository->remove($assetUsage);
}
public function isAssetUsageStillValid(AssetUsage $usage): bool
{
if (!isset($this->existingAssetsById[$usage->assetId])) {
/** @var AssetInterface|null $asset */
$asset = $this->assetRepository->findByIdentifier($usage->assetId);
$this->existingAssetsById[$usage->assetId] = $asset !== null;
}
if ($this->existingAssetsById[$usage->assetId] === false) {
return false;
}
$dimensionSpacePoint = $usage->originDimensionSpacePoint->toDimensionSpacePoint();
// FIXME: AssetUsage->workspaceName ?
$workspace = $this->contentRepository->getWorkspaceFinder()->findOneByCurrentContentStreamId($usage->contentStreamId);
if (is_null($workspace)) {
return false;
}
$subGraph = $this->contentRepository->getContentGraph($workspace->workspaceName)->getSubgraph(
$dimensionSpacePoint,
VisibilityConstraints::withoutRestrictions()
);
$node = $subGraph->findNodeById($usage->nodeAggregateId);
return $node !== null;
}
}