1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\Metadata\Factory; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
17
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; |
18
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
19
|
|
|
use Doctrine\ORM\EntityManager; |
20
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
21
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\ComponentInterface; |
22
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\PageDataInterface; |
23
|
|
|
use Silverback\ApiComponentsBundle\Metadata\ComponentUsageMetadata; |
24
|
|
|
use Silverback\ApiComponentsBundle\Metadata\PageDataPropertyMetadata; |
25
|
|
|
use Silverback\ApiComponentsBundle\Repository\Core\ComponentPositionRepository; |
26
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
27
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @author Daniel West <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class ComponentUsageMetadataFactory |
33
|
|
|
{ |
34
|
|
|
private ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory; |
35
|
|
|
private ResourceMetadataFactoryInterface $resourceMetadataFactory; |
36
|
|
|
private PageDataMetadataFactoryInterface $pageDataMetadataFactory; |
37
|
|
|
private ComponentPositionRepository $componentPositionRepository; |
38
|
|
|
private ManagerRegistry $managerRegistry; |
39
|
|
|
private PropertyAccessor $propertyAccessor; |
40
|
|
|
|
41
|
|
|
public function __construct( |
42
|
|
|
ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, |
43
|
|
|
ResourceMetadataFactoryInterface $resourceMetadataFactory, |
44
|
|
|
PageDataMetadataFactoryInterface $pageDataMetadataFactory, |
45
|
|
|
ComponentPositionRepository $componentPositionRepository, |
46
|
|
|
ManagerRegistry $managerRegistry |
47
|
|
|
) { |
48
|
|
|
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory; |
49
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
50
|
|
|
$this->pageDataMetadataFactory = $pageDataMetadataFactory; |
51
|
|
|
$this->componentPositionRepository = $componentPositionRepository; |
52
|
|
|
$this->managerRegistry = $managerRegistry; |
53
|
|
|
$this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function create(ComponentInterface $component): ComponentUsageMetadata |
57
|
|
|
{ |
58
|
|
|
$componentPositions = $this->componentPositionRepository->findByComponent($component); |
59
|
|
|
$componentPositionCount = \count($componentPositions); |
60
|
|
|
|
61
|
|
|
$pageDataCount = $this->getPageDataTotal($component); |
62
|
|
|
|
63
|
|
|
return new ComponentUsageMetadata($componentPositionCount, $pageDataCount); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function getPageDataTotal(ComponentInterface $component): int |
67
|
|
|
{ |
68
|
|
|
// we want the SHORT NAME |
69
|
|
|
$resourceClass = \get_class($component); |
70
|
|
|
$apiPlatformMetadata = $this->resourceMetadataFactory->create($resourceClass); |
71
|
|
|
$resourceShortName = $apiPlatformMetadata->getShortName(); |
72
|
|
|
|
73
|
|
|
$pageDataLocations = $this->getPageDataLocations($resourceShortName); |
|
|
|
|
74
|
|
|
$pageDataCount = 0; |
75
|
|
|
foreach ($pageDataLocations as $pageDataClassName => $properties) { |
76
|
|
|
$pageDataResources = $this->findPageDataResourcesByPropertiesAndComponent($pageDataClassName, $properties, $component); |
77
|
|
|
if (!$pageDataResources) { |
78
|
|
|
continue; |
79
|
|
|
} |
80
|
|
|
$componentInDataCount = 0; |
81
|
|
|
foreach ($pageDataResources as $pageDataResource) { |
82
|
|
|
foreach ($properties as $property) { |
83
|
|
|
if ($this->propertyAccessor->getValue($pageDataResource, $property) === $component) { |
84
|
|
|
++$componentInDataCount; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
$pageDataCount += $componentInDataCount; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $pageDataCount; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function findPageDataResourcesByPropertiesAndComponent(string $pageDataClassName, ArrayCollection $properties, ComponentInterface $component): ?array |
95
|
|
|
{ |
96
|
|
|
$em = $this->managerRegistry->getManagerForClass($pageDataClassName); |
97
|
|
|
if (!$em instanceof EntityManager) { |
98
|
|
|
return null; |
99
|
|
|
} |
100
|
|
|
$qb = $em->createQueryBuilder(); |
101
|
|
|
$expr = $qb->expr(); |
102
|
|
|
$qb |
103
|
|
|
->select('pd') |
104
|
|
|
->from($pageDataClassName, 'pd') |
105
|
|
|
->setParameter('component', $component); |
106
|
|
|
foreach ($properties as $property) { |
107
|
|
|
$qb->orWhere($expr->eq('pd.' . $property, ':component')); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $qb->getQuery()->getResult(); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function getPageDataLocations(string $resourceShortName): array |
114
|
|
|
{ |
115
|
|
|
$pageDataLocations = []; |
116
|
|
|
foreach ($this->resourceNameCollectionFactory->create() as $pageDataResourceClass) { |
117
|
|
|
$reflectionClass = new \ReflectionClass($pageDataResourceClass); |
118
|
|
|
if (!$reflectionClass->implementsInterface(PageDataInterface::class)) { |
119
|
|
|
continue; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$pageDataMetadata = $this->pageDataMetadataFactory->create($pageDataResourceClass); |
123
|
|
|
$resourceProperties = $pageDataMetadata->findPropertiesByComponentClass($resourceShortName); |
124
|
|
|
if ($resourceProperties->count() > 0) { |
125
|
|
|
$pageDataLocations[$pageDataResourceClass] = $resourceProperties->map(static function (PageDataPropertyMetadata $metadata) { |
126
|
|
|
return $metadata->getProperty(); |
127
|
|
|
}); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $pageDataLocations; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|