Completed
Push — standalone ( 36bfab...ea9a00 )
by Philip
03:09
created

YamlDriver::parseFieldConfig()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 7.7656

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 6
cts 8
cp 0.75
rs 8.2222
cc 7
eloc 7
nc 8
nop 2
crap 7.7656
1
<?php
2
3
namespace Dontdrinkandroot\RestBundle\Metadata\Driver;
4
5
use Dontdrinkandroot\RestBundle\Metadata\ClassMetadata;
6
use Dontdrinkandroot\RestBundle\Metadata\PropertyMetadata;
7
use Metadata\Driver\AbstractFileDriver;
8
use Metadata\Driver\DriverInterface;
9
use Metadata\Driver\FileLocatorInterface;
10
use Symfony\Component\Yaml\Yaml;
11
12
class YamlDriver extends AbstractFileDriver
13
{
14
    /**
15
     * @var DriverInterface
16
     */
17
    private $doctrineDriver;
18
19 34
    public function __construct(FileLocatorInterface $locator, DriverInterface $doctrineDriver)
20
    {
21 34
        parent::__construct($locator);
22 34
        $this->doctrineDriver = $doctrineDriver;
23 34
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 14
    protected function loadMetadataFromFile(\ReflectionClass $class, $file)
29
    {
30
        /** @var ClassMetadata $ddrRestClassMetadata */
31 14
        $classMetadata = $this->doctrineDriver->loadMetadataForClass($class);
32 14
        if (null === $classMetadata) {
33
            $classMetadata = new ClassMetadata($class->getName());
34
        }
35
36 14
        $config = Yaml::parse(file_get_contents($file));
37 14
        $className = key($config);
38
39 14
        if ($className !== $class->name) {
40
            throw new \RuntimeException(
41
                sprintf('Class definition mismatch for "%s" in "%s": %s', $class->getName(), $file, key($config))
42
            );
43
        }
44
45 14
        $config = $config[$className];
46 14
        if (!is_array($config)) {
47
            $config = [];
48
        }
49
50 14
        if (array_key_exists('rootResource', $config) && true === $config['rootResource']) {
51 14
            $classMetadata->setRestResource(true);
52
        }
53
54 14
        if (array_key_exists('service', $config)) {
55 4
            $classMetadata->setService($config['service']);
56
        }
57
58 14
        $fieldConfigs = [];
59 14
        if (array_key_exists('fields', $config)) {
60 4
            $fieldConfigs = $config['fields'];
61
        }
62
63 14
        foreach ($class->getProperties() as $reflectionProperty) {
64
65 14
            $propertyName = $reflectionProperty->getName();
66 14
            $propertyMetadata = $this->getOrCreatePropertymetadata($classMetadata, $propertyName);
0 ignored issues
show
Compatibility introduced by
$classMetadata of type object<Metadata\ClassMetadata> is not a sub-type of object<Dontdrinkandroot\...Metadata\ClassMetadata>. It seems like you assume a child class of the class Metadata\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
67
68 14
            if (array_key_exists($propertyName, $fieldConfigs)) {
69 4
                $fieldConfig = $fieldConfigs[$propertyName];
70 4
                $this->parseFieldConfig($fieldConfig, $propertyMetadata);
71 4
                unset($fieldConfigs[$propertyName]);
72
            }
73
74 14
            $classMetadata->addPropertyMetadata($propertyMetadata);
75
        }
76
77
        /* Parse unbacked field definitions */
78 14
        foreach ($fieldConfigs as $name => $fieldConfig) {
79 4
            $propertyMetadata = $this->getOrCreatePropertymetadata($classMetadata, $name);
0 ignored issues
show
Compatibility introduced by
$classMetadata of type object<Metadata\ClassMetadata> is not a sub-type of object<Dontdrinkandroot\...Metadata\ClassMetadata>. It seems like you assume a child class of the class Metadata\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
80 4
            $this->parseFieldConfig($fieldConfig, $propertyMetadata);
81 4
            $classMetadata->addPropertyMetadata($propertyMetadata);
82
        }
83
84 14
        return $classMetadata;
85
    }
86
87 4
    protected function parseFieldConfig(array $fieldConfig, PropertyMetadata $propertyMetadata): void
88
    {
89 4
        if (array_key_exists('puttable', $fieldConfig) && true === $fieldConfig['puttable']) {
90
            $propertyMetadata->setPuttable(true);
91
        }
92
93 4
        if (array_key_exists('excluded', $fieldConfig) && true === $fieldConfig['excluded']) {
94 4
            $propertyMetadata->setExcluded(true);
95
        }
96
97 4
        if (array_key_exists('postable', $fieldConfig) && true === $fieldConfig['postable']) {
98
            $propertyMetadata->setPostable(true);
99
        }
100 4
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 34
    protected function getExtension()
106
    {
107 34
        return 'rest.yml';
108
    }
109
110 14
    protected function getOrCreatePropertymetadata(ClassMetadata $classMetadata, $propertyName): PropertyMetadata
111
    {
112 14
        $propertyMetadata = $classMetadata->getPropertyMetadata($propertyName);
113 14
        if (null === $propertyMetadata) {
114 4
            $propertyMetadata = new PropertyMetadata($classMetadata->name, $propertyName);
115
116 4
            return $propertyMetadata;
117
        }
118
119 12
        return $propertyMetadata;
120
    }
121
}
122