ComponentUsageMetadataFactory   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 62
ccs 0
cts 29
cp 0
rs 10
c 1
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A create() 0 19 4
A getPageDataTotal() 0 19 5
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 Doctrine\Persistence\ManagerRegistry;
17
use Silverback\ApiComponentsBundle\DataProvider\PageDataProvider;
18
use Silverback\ApiComponentsBundle\Entity\Core\ComponentInterface;
19
use Silverback\ApiComponentsBundle\Helper\Publishable\PublishableStatusChecker;
20
use Silverback\ApiComponentsBundle\Metadata\ComponentUsageMetadata;
21
use Silverback\ApiComponentsBundle\Repository\Core\ComponentPositionRepository;
22
use Silverback\ApiComponentsBundle\Utility\ClassMetadataTrait;
23
use Symfony\Component\PropertyAccess\PropertyAccess;
24
use Symfony\Component\PropertyAccess\PropertyAccessor;
25
26
/**
27
 * @author Daniel West <[email protected]>
28
 */
29
class ComponentUsageMetadataFactory
30
{
31
    use ClassMetadataTrait;
32
33
    private ComponentPositionRepository $componentPositionRepository;
34
    private PropertyAccessor $propertyAccessor;
35
    private PageDataProvider $pageDataProvider;
36
    private PublishableStatusChecker $publishableStatusChecker;
37
38
    public function __construct(
39
        ComponentPositionRepository $componentPositionRepository,
40
        PageDataProvider $pageDataProvider,
41
        PublishableStatusChecker $publishableStatusChecker,
42
        ManagerRegistry $registry
43
    ) {
44
        $this->componentPositionRepository = $componentPositionRepository;
45
        $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
46
        $this->pageDataProvider = $pageDataProvider;
47
        $this->publishableStatusChecker = $publishableStatusChecker;
48
        $this->initRegistry($registry);
49
    }
50
51
    public function create(ComponentInterface $component): ComponentUsageMetadata
52
    {
53
        $annotationReader = $this->publishableStatusChecker->getAttributeReader();
54
        if ($annotationReader->isConfigured($component) && !$this->publishableStatusChecker->isActivePublishedAt($component)) {
55
            // get the published component to run checks against
56
            $configuration = $annotationReader->getConfiguration($component);
57
            $classMetadata = $this->getClassMetadata($component);
58
59
            $publishedResourceAssociation = $classMetadata->getFieldValue($component, $configuration->associationName);
60
            if ($publishedResourceAssociation) {
61
                $component = $publishedResourceAssociation;
62
            }
63
        }
64
        $componentPositions = $this->componentPositionRepository->findByComponent($component);
65
        $componentPositionCount = \count($componentPositions);
66
67
        $pageDataCount = $this->getPageDataTotal($component);
68
69
        return new ComponentUsageMetadata($componentPositionCount, $pageDataCount);
70
    }
71
72
    private function getPageDataTotal(ComponentInterface $component): int
73
    {
74
        /** @var \Generator $pageDataLocations */
75
        $pageDataLocations = $this->pageDataProvider->findPageDataComponentMetadata($component);
76
        $pageDataCount = 0;
77
78
        foreach ($pageDataLocations as $pageDataComponentMetadata) {
79
            $componentInDataCount = 0;
80
            foreach ($pageDataComponentMetadata->getPageDataResources() as $pageDataResource) {
81
                foreach ($pageDataComponentMetadata->getProperties() as $property) {
82
                    if ($this->propertyAccessor->getValue($pageDataResource, $property) === $component) {
83
                        ++$componentInDataCount;
84
                    }
85
                }
86
            }
87
            $pageDataCount += $componentInDataCount;
88
        }
89
90
        return $pageDataCount;
91
    }
92
}
93