Test Failed
Push — master ( a81572...90f7a4 )
by Daniel
04:32
created

PageDataMetadataFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
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 Doctrine\Persistence\ManagerRegistry;
18
use Silverback\ApiComponentsBundle\Entity\Core\AbstractPageData;
19
use Silverback\ApiComponentsBundle\Entity\Core\PageDataInterface;
20
use Silverback\ApiComponentsBundle\Exception\PageDataNotFoundException;
21
use Silverback\ApiComponentsBundle\Metadata\PageDataMetadata;
22
use Silverback\ApiComponentsBundle\Metadata\PageDataPropertyMetadata;
23
24
/**
25
 * @author Daniel West <[email protected]>
26
 */
27
class PageDataMetadataFactory implements PageDataMetadataFactoryInterface
28
{
29
    private ManagerRegistry $registry;
30
    private ResourceMetadataFactoryInterface $resourceMetadataFactory;
31
32
    public function __construct(
33
        ManagerRegistry $registry,
34
        ResourceMetadataFactoryInterface $resourceMetadataFactory
35
    ) {
36
        $this->registry = $registry;
37
        $this->resourceMetadataFactory = $resourceMetadataFactory;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function create(string $resourceClass): PageDataMetadata
44
    {
45
        // needs to be a class
46
        if (!class_exists($resourceClass)) {
47
            throw new PageDataNotFoundException(sprintf('`%s` was not found', $resourceClass));
48
        }
49
50
        // Check it is page data
51
        $reflection = new \ReflectionClass($resourceClass);
52
        if (!$reflection->implementsInterface(PageDataInterface::class)) {
53
            throw new PageDataNotFoundException(sprintf('Resource class `%s` is not a valid page data resource', $resourceClass));
54
        }
55
56
        // FInd the doctrine manager
57
        $manager = $this->registry->getManagerForClass($resourceClass);
58
        if (!$manager) {
59
            throw new PageDataNotFoundException(sprintf('Cannot find manager for page data resource `%s`', $resourceClass));
60
        }
61
        $classMetadata = $manager->getClassMetadata($resourceClass);
62
63
        // Get abstract prop names to exclude
64
        $abstractReflection = new \ReflectionClass(AbstractPageData::class);
65
        $reflProps = $abstractReflection->getProperties();
66
        $abstractProps = array_map(static function (\ReflectionProperty $prop) {
67
            return $prop->name;
68
        }, $reflProps);
69
70
        // Get all fields with association
71
        $assocFields = array_filter($classMetadata->getAssociationNames(), static function ($name) use ($abstractProps) {
72
            return !\in_array($name, $abstractProps, true);
73
        });
74
75
        // Create metadata with relations
76
        $metadata = new PageDataMetadata($resourceClass);
77
        foreach ($assocFields as $assocField) {
78
            $targetClass = $classMetadata->getAssociationTargetClass($assocField);
79
            $relationName = $this->resourceMetadataFactory->create($targetClass)->getShortName();
0 ignored issues
show
Bug introduced by
It seems like $targetClass can also be of type null; however, parameter $resourceClass of ApiPlatform\Core\Metadat...toryInterface::create() 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
            $relationName = $this->resourceMetadataFactory->create(/** @scrutinizer ignore-type */ $targetClass)->getShortName();
Loading history...
80
            $metadata->addProperty(new PageDataPropertyMetadata($assocField, $relationName));
0 ignored issues
show
Bug introduced by
It seems like $relationName can also be of type null; however, parameter $componentClass 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

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