Completed
Push — standalone ( ee9bba...36bfab )
by Philip
02:39
created

YamlDriver::loadMetadataFromFile()   F

Complexity

Conditions 17
Paths 610

Size

Total Lines 63
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 34.6626

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 63
ccs 20
cts 33
cp 0.6061
rs 3.1746
cc 17
eloc 33
nc 610
nop 2
crap 34.6626

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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());
1 ignored issue
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
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))
1 ignored issue
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
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
        $propertyConfigs = [];
59 14
        if (array_key_exists('fields', $config)) {
60
            $propertyConfigs = $config['fields'];
61
        }
62
63 14
        foreach ($class->getProperties() as $reflectionProperty) {
64
65 12
            $propertyMetadata = $classMetadata->getPropertyMetadata($reflectionProperty->getName());
66 12
            if (null === $propertyMetadata) {
67
                $propertyMetadata = new PropertyMetadata($class->getName(), $reflectionProperty->getName());
1 ignored issue
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
68
            }
69
70 12
            if (array_key_exists($reflectionProperty->getName(), $propertyConfigs)) {
71
72
                $propertyConfig = $propertyConfigs[$reflectionProperty->getName()];
73
74
                if (array_key_exists('puttable', $propertyConfig) && true === $propertyConfig['puttable']) {
75
                    $propertyMetadata->setPuttable(true);
76
                }
77
78
                if (array_key_exists('excluded', $propertyConfig) && true === $propertyConfig['excluded']) {
79
                    $propertyMetadata->setExcluded(true);
80
                }
81
82
                if (array_key_exists('postable', $propertyConfig) && true === $propertyConfig['postable']) {
83
                    $propertyMetadata->setPostable(true);
84
                }
85
            }
86 12
            $classMetadata->addPropertyMetadata($propertyMetadata);
87
        }
88
89 14
        return $classMetadata;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 34
    protected function getExtension()
96
    {
97 34
        return 'rest.yml';
98
    }
99
}
100