Completed
Push — master ( ec8ced...26f03f )
by Kévin
13s
created

InheritedPropertyMetadataFactory::create()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 8
nop 3
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[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 ApiPlatform\Core\Metadata\Property\Factory;
15
16
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
17
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
18
19
/**
20
 * Get property metadata from eventual child inherited properties.
21
 *
22
 * @author Antoine Bluchet <[email protected]>
23
 */
24
final class InheritedPropertyMetadataFactory implements PropertyMetadataFactoryInterface
25
{
26
    private $resourceNameCollection;
27
    private $decorated;
28
29
    public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollection, PropertyMetadataFactoryInterface $decorated = null)
30
    {
31
        $this->resourceNameCollection = $resourceNameCollection;
32
        $this->decorated = $decorated;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function create(string $resourceClass, string $property, array $options = []): PropertyMetadata
39
    {
40
        $propertyMetadata = $this->decorated ? $this->decorated->create($resourceClass, $property, $options) : new PropertyMetadata();
41
42
        foreach ($this->resourceNameCollection->create() as $knownResourceClass) {
43
            if ($resourceClass === $knownResourceClass) {
44
                continue;
45
            }
46
47
            if (is_subclass_of($knownResourceClass, $resourceClass)) {
48
                $propertyMetadata = $this->create($knownResourceClass, $property, $options);
49
50
                return $propertyMetadata->withChildInherited($knownResourceClass);
51
            }
52
        }
53
54
        return $propertyMetadata;
55
    }
56
}
57