Completed
Push — master ( b72a90...b5fae3 )
by Antoine
40s queued 13s
created

DefaultPropertyMetadataFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 25 6
A __construct() 0 3 1
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\Exception\PropertyNotFoundException;
17
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
18
19
/**
20
 * Populates defaults values of the ressource properties using the default PHP values of properties.
21
 */
22
final class DefaultPropertyMetadataFactory implements PropertyMetadataFactoryInterface
23
{
24
    private $decorated;
25
26
    public function __construct(PropertyMetadataFactoryInterface $decorated = null)
27
    {
28
        $this->decorated = $decorated;
29
    }
30
31
    public function create(string $resourceClass, string $property, array $options = []): PropertyMetadata
32
    {
33
        if (null === $this->decorated) {
34
            $propertyMetadata = new PropertyMetadata();
35
        } else {
36
            try {
37
                $propertyMetadata = $this->decorated->create($resourceClass, $property, $options);
38
            } catch (PropertyNotFoundException $propertyNotFoundException) {
39
                $propertyMetadata = new PropertyMetadata();
40
            }
41
        }
42
43
        try {
44
            $reflectionClass = new \ReflectionClass($resourceClass);
45
        } catch (\ReflectionException $reflectionException) {
46
            return $propertyMetadata;
47
        }
48
49
        $defaultProperties = $reflectionClass->getDefaultProperties();
50
51
        if (!\array_key_exists($property, $defaultProperties) || null === ($defaultProperty = $defaultProperties[$property])) {
52
            return $propertyMetadata;
53
        }
54
55
        return $propertyMetadata->withDefault($defaultProperty);
56
    }
57
}
58