YmlDriver   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 98.53%

Importance

Changes 0
Metric Value
wmc 16
dl 0
loc 127
ccs 67
cts 68
cp 0.9853
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B loadYml() 0 26 3
F loadMetadataForClass() 0 78 12
1
<?php
2
3
namespace Vox\Metadata\Driver;
4
5
use Metadata\Driver\DriverInterface;
6
use Metadata\MethodMetadata;
7
use ProxyManager\Proxy\AccessInterceptorValueHolderInterface;
8
use ReflectionClass;
9
use ReflectionProperty;
10
use RuntimeException;
11
use Symfony\Component\Yaml\Parser;
12
use Vox\Data\Mapping\Bindings;
13
use Vox\Data\Mapping\Exclude;
14
use Vox\Metadata\ClassMetadata;
15
use Vox\Metadata\PropertyMetadata;
16
use Vox\Webservice\Mapping\BelongsTo;
17
use Vox\Webservice\Mapping\HasMany;
18
use Vox\Webservice\Mapping\HasOne;
19
use Vox\Webservice\Mapping\Id;
20
use Vox\Webservice\Mapping\Resource;
21
22
/**
23
 * Yml driver to create a class metadata information
24
 * 
25
 * @author Jhonatan Teixeira <[email protected]>
26
 */
27
class YmlDriver implements DriverInterface
28
{
29
    private $ymlParser;
30
    
31
    private $path;
32
    
33
    private $classMetadataClassName;
34
    
35
    private $propertyMetadataClassName;
36
    
37 7
    public function __construct(
38
        string $path,
39
        string $classMetadataClassName = ClassMetadata::class,
40
        string $propertyMetadataClassName = PropertyMetadata::class
41
    ) {
42 7
        $this->ymlParser                 = new Parser();
43 7
        $this->path                      = realpath($path);
44 7
        $this->classMetadataClassName    = $classMetadataClassName;
45 7
        $this->propertyMetadataClassName = $propertyMetadataClassName;
46 7
    }
47
    
48 7
    public function loadMetadataForClass(ReflectionClass $class): ClassMetadata
49
    {
50 7
        if ($class->implementsInterface(AccessInterceptorValueHolderInterface::class)) {
51 1
            $class = $class->getParentClass();
52
        }
53
        
54 7
        $yml = $this->loadYml($class);
55
        
56
        /* @var $classMetadata ClassMetadata */
57 7
        $classMetadata = (new ReflectionClass($this->classMetadataClassName))->newInstance($class->name);
58
        
59 7
        if (isset($yml['resource'])) {
60 5
            $resource         = new Resource();
61 5
            $resource->client = $yml['resource']['client'] ?? null;
62 5
            $resource->route  = $yml['resource']['route'] ?? null;
63 5
            $classMetadata->setAnnotations([Resource::class => $resource]);
64
        }
65
        
66 7
        foreach ($class->getMethods() as $method) {
67 5
            $classMetadata->addMethodMetadata(new MethodMetadata($class->name, $method->name));
68
        }
69
        
70
        /* @var $reflectionProperty ReflectionProperty */
71 7
        foreach ($class->getProperties() as $reflectionProperty) {
72 7
            $annotations = [];
73 7
            $annotations[Bindings::class] = $bindings = new Bindings();
74 7
            $name                         = $reflectionProperty->name;
75
76 7
            $idMapping = $yml['id'] ?? [];
77
78 7
            $ids = is_scalar($idMapping) ? [$idMapping] : $idMapping;
79
80 7
            if (in_array($name, $ids)) {
81 6
                $annotations[Id::class] = new Id();
82
            }
83
            
84 7
            if (isset($yml['parameters'][$reflectionProperty->name])) {
85 5
                $config           = $yml['parameters'][$name];
86 5
                $bindings->source = $config['bindings']['source'] ?? null;
87 5
                $bindings->target = $config['bindings']['target'] ?? null;
88
89 5
                if (isset($config['belongsTo'])) {
90 2
                    $belongsTo = new BelongsTo();
91 2
                    $belongsTo->foreignField = $config['belongsTo']['foreignField'];
92 2
                    $annotations[BelongsTo::class] = $belongsTo;
93
                }
94
                
95 5
                if (isset($config['hasOne'])) {
96 1
                    $hasOne = new HasOne();
97 1
                    $hasOne->foreignField = $config['hasOne']['foreignField'];
98 1
                    $annotations[HasOne::class] = $hasOne;
99
                }
100
                
101 5
                if (isset($config['hasMany'])) {
102 2
                    $hasMany = new HasMany();
103 2
                    $hasMany->foreignField       = $config['hasMany']['foreignField'] ?? null;
104 2
                    $hasMany->iriCollectionField = $config['hasMany']['iriCollectionField'] ?? null;
105 2
                    $annotations[HasMany::class] = $hasMany;
106
                }
107
                
108 5
                if (isset($config['exclude'])) {
109 1
                    $exclude = new Exclude();
110 1
                    $exclude->input  = $config['exlude']['input'] ?? true;
111 1
                    $exclude->output = $config['exlude']['output'] ?? true;
112 1
                    $annotations[Exclude::class] = $exclude;
113
                }
114
            }
115
            
116
            /* @var $propertyMetadata PropertyMetadata */
117 7
            $propertyMetadata = (new ReflectionClass($this->propertyMetadataClassName))
118 7
                ->newInstance($class->name, $reflectionProperty->name);
119
            
120 7
            $propertyMetadata->setAnnotations($annotations);
121
            
122 7
            $classMetadata->addPropertyMetadata($propertyMetadata);
123
        }
124
        
125 7
        return $classMetadata;
126
    }
127
    
128 7
    private function loadYml(ReflectionClass $class)
129
    {
130 7
        $className = $class->getName();
131
        
132 7
        $path = sprintf(
133 7
            '%s/%s.yml', 
134 7
            preg_replace('/\/$/', '', $this->path), 
135 7
            str_replace('\\', DIRECTORY_SEPARATOR, $className)
136
        );
137
        
138 7
        if (is_file($path)) {
139
            return $this->ymlParser->parse(file_get_contents($path));
140
        }
141
        
142 7
        $path = sprintf(
143 7
            '%s/%s.yml', 
144 7
            preg_replace('/\/$/', '', $this->path), 
145 7
            str_replace('\\', '.', $className)
146
        );
147
        
148 7
        if (is_file($path)) {
149 6
            return $this->ymlParser->parse(file_get_contents($path));
150
        }
151
152 1
        return [];
153
        throw new RuntimeException("metadata file not found for class $className");
0 ignored issues
show
Unused Code introduced by
ThrowNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
154
    }
155
}
156