PageDataMetadataFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 55
ccs 0
cts 28
cp 0
rs 10
c 2
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 44 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 ApiPlatform\Metadata\ApiResource;
17
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
18
use Doctrine\Persistence\ManagerRegistry;
19
use Silverback\ApiComponentsBundle\Entity\Core\AbstractPageData;
20
use Silverback\ApiComponentsBundle\Entity\Core\PageDataInterface;
21
use Silverback\ApiComponentsBundle\Exception\PageDataNotFoundException;
22
use Silverback\ApiComponentsBundle\Metadata\PageDataMetadata;
23
use Silverback\ApiComponentsBundle\Metadata\PageDataPropertyMetadata;
24
25
/**
26
 * @author Daniel West <[email protected]>
27
 */
28
class PageDataMetadataFactory implements PageDataMetadataFactoryInterface
29
{
30
    public function __construct(
31
        private readonly ManagerRegistry $registry,
32
        private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory
33
    ) {
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function create(string $resourceClass): PageDataMetadata
40
    {
41
        // needs to be a class
42
        if (!class_exists($resourceClass)) {
43
            throw new PageDataNotFoundException(sprintf('`%s` was not found', $resourceClass));
44
        }
45
46
        // Check it is page data
47
        $reflection = new \ReflectionClass($resourceClass);
48
        if (!$reflection->implementsInterface(PageDataInterface::class)) {
49
            throw new PageDataNotFoundException(sprintf('Resource class `%s` is not a valid page data resource', $resourceClass));
50
        }
51
52
        // Find the doctrine manager
53
        $manager = $this->registry->getManagerForClass($resourceClass);
54
        if (!$manager) {
55
            throw new PageDataNotFoundException(sprintf('Cannot find manager for page data resource `%s`', $resourceClass));
56
        }
57
        $classMetadata = $manager->getClassMetadata($resourceClass);
58
59
        // Get abstract prop names to exclude
60
        $abstractReflection = new \ReflectionClass(AbstractPageData::class);
61
        $reflProps = $abstractReflection->getProperties();
62
        $abstractProps = array_map(static function (\ReflectionProperty $prop) {
63
            return $prop->name;
64
        }, $reflProps);
65
66
        // Get all fields with association
67
        $assocFields = array_filter($classMetadata->getAssociationNames(), static function ($name) use ($abstractProps) {
68
            return !\in_array($name, $abstractProps, true);
69
        });
70
71
        // Create metadata with relations
72
        $metadata = new PageDataMetadata($resourceClass);
73
        foreach ($assocFields as $assocField) {
74
            $targetClass = $classMetadata->getAssociationTargetClass($assocField);
75
            $metadataCollection = $this->resourceMetadataFactory->create($targetClass);
76
            /** @var ApiResource $apiResource */
77
            $apiResource = $metadataCollection[0];
78
            $relationName = $apiResource->getShortName();
79
            $metadata->addProperty(new PageDataPropertyMetadata($assocField, $targetClass, $relationName));
0 ignored issues
show
Bug introduced by
It seems like $relationName can also be of type null; however, parameter $componentShortName of Silverback\ApiComponents...Metadata::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
            $metadata->addProperty(new PageDataPropertyMetadata($assocField, $targetClass, /** @scrutinizer ignore-type */ $relationName));
Loading history...
80
        }
81
82
        return $metadata;
83
    }
84
}
85