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 Silverback\ApiComponentsBundle\DataProvider\PageDataProvider; |
17
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\ComponentInterface; |
18
|
|
|
use Silverback\ApiComponentsBundle\Metadata\ComponentUsageMetadata; |
19
|
|
|
use Silverback\ApiComponentsBundle\Repository\Core\ComponentPositionRepository; |
20
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
21
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Daniel West <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class ComponentUsageMetadataFactory |
27
|
|
|
{ |
28
|
|
|
private ComponentPositionRepository $componentPositionRepository; |
29
|
|
|
private PropertyAccessor $propertyAccessor; |
30
|
|
|
private PageDataProvider $pageDataProvider; |
31
|
|
|
|
32
|
|
|
public function __construct( |
33
|
|
|
ComponentPositionRepository $componentPositionRepository, |
34
|
|
|
PageDataProvider $pageDataProvider |
35
|
|
|
) { |
36
|
|
|
$this->componentPositionRepository = $componentPositionRepository; |
37
|
|
|
$this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
38
|
|
|
$this->pageDataProvider = $pageDataProvider; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function create(ComponentInterface $component): ComponentUsageMetadata |
42
|
|
|
{ |
43
|
|
|
$componentPositions = $this->componentPositionRepository->findByComponent($component); |
44
|
|
|
$componentPositionCount = \count($componentPositions); |
45
|
|
|
|
46
|
|
|
$pageDataCount = $this->getPageDataTotal($component); |
47
|
|
|
|
48
|
|
|
return new ComponentUsageMetadata($componentPositionCount, $pageDataCount); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function getPageDataTotal(ComponentInterface $component): int |
52
|
|
|
{ |
53
|
|
|
$pageDataLocations = $this->pageDataProvider->findPageDataComponentMetadata($component); |
54
|
|
|
$pageDataCount = 0; |
55
|
|
|
foreach ($pageDataLocations as $pageDataComponentMetadata) { |
56
|
|
|
$componentInDataCount = 0; |
57
|
|
|
foreach ($pageDataComponentMetadata->getPageDataResources() as $pageDataResource) { |
58
|
|
|
foreach ($pageDataComponentMetadata->getProperties() as $property) { |
59
|
|
|
if ($this->propertyAccessor->getValue($pageDataResource, $property) === $component) { |
60
|
|
|
++$componentInDataCount; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
$pageDataCount += $componentInDataCount; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $pageDataCount; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|