Test Failed
Push — master ( 9ceea8...5b7b5b )
by Daniel
09:29
created

ComponentUsageMetadataFactory::findPageDataResourcesByPropertiesAndComponent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 17
rs 9.8666
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