Completed
Push — master ( 28afa9...69e2c3 )
by JHONATAN
02:50
created

YmlDriver   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Test Coverage

Coverage 96.92%

Importance

Changes 0
Metric Value
wmc 15
dl 0
loc 122
ccs 63
cts 65
cp 0.9692
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
D loadMetadataForClass() 0 73 11
B loadYml() 0 26 3
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 5
    public function __construct(
38
        string $path,
39
        string $classMetadataClassName = ClassMetadata::class,
40
        string $propertyMetadataClassName = PropertyMetadata::class
41
    ) {
42 5
        $this->ymlParser                 = new Parser();
43 5
        $this->path                      = realpath($path);
44 5
        $this->classMetadataClassName    = $classMetadataClassName;
45 5
        $this->propertyMetadataClassName = $propertyMetadataClassName;
46 5
    }
47
    
48 5
    public function loadMetadataForClass(ReflectionClass $class): ClassMetadata
49
    {
50 5
        if ($class->implementsInterface(AccessInterceptorValueHolderInterface::class)) {
51
            $class = $class->getParentClass();
52
        }
53
        
54 5
        $yml = $this->loadYml($class);
55
        
56
        /* @var $classMetadata ClassMetadata */
57 5
        $classMetadata = (new ReflectionClass($this->classMetadataClassName))->newInstance($class->name);
58
        
59 5
        if (isset($yml['resource'])) {
60 3
            $resource         = new Resource();
61 3
            $resource->client = $yml['resource']['client'] ?? null;
62 3
            $resource->route  = $yml['resource']['route'] ?? null;
63 3
            $classMetadata->setAnnotations([Resource::class => $resource]);
64
        }
65
        
66 5
        foreach ($class->getMethods() as $method) {
67 4
            $classMetadata->addMethodMetadata(new MethodMetadata($class->name, $method->name));
68
        }
69
        
70
        /* @var $reflectionProperty ReflectionProperty */
71 5
        foreach ($class->getProperties() as $reflectionProperty) {
72 5
            $annotations = [];
73 5
            $annotations[Bindings::class] = $bindings = new Bindings();
74 5
            $name                         = $reflectionProperty->name;
75
76 5
            if ($name == ($yml['id'] ?? null)) {
77 4
                $annotations[Id::class] = new Id();
78
            }
79
            
80 5
            if (isset($yml['parameters'][$reflectionProperty->name])) {
81 4
                $config           = $yml['parameters'][$name];
82 4
                $bindings->source = $config['bindings']['source'] ?? null;
83 4
                $bindings->target = $config['bindings']['target'] ?? null;
84
85 4
                if (isset($config['belongsTo'])) {
86 2
                    $belongsTo = new BelongsTo();
87 2
                    $belongsTo->foreignField = $config['belongsTo']['foreignField'];
88 2
                    $annotations[BelongsTo::class] = $belongsTo;
89
                }
90
                
91 4
                if (isset($config['hasOne'])) {
92 1
                    $hasOne = new HasOne();
93 1
                    $hasOne->foreignField = $config['hasOne']['foreignField'];
94 1
                    $annotations[HasOne::class] = $hasOne;
95
                }
96
                
97 4
                if (isset($config['hasMany'])) {
98 1
                    $hasMany = new HasMany();
99 1
                    $hasMany->foreignField = $config['hasMany']['foreignField'];
100 1
                    $annotations[HasMany::class] = $hasMany;
101
                }
102
                
103 4
                if (isset($config['exclude'])) {
104 1
                    $exclude = new Exclude();
105 1
                    $exclude->input  = $config['exlude']['input'] ?? true;
106 1
                    $exclude->output = $config['exlude']['output'] ?? true;
107 1
                    $annotations[Exclude::class] = $exclude;
108
                }
109
            }
110
            
111
            /* @var $propertyMetadata PropertyMetadata */
112 5
            $propertyMetadata = (new ReflectionClass($this->propertyMetadataClassName))
113 5
                ->newInstance($class->name, $reflectionProperty->name);
114
            
115 5
            $propertyMetadata->setAnnotations($annotations);
116
            
117 5
            $classMetadata->addPropertyMetadata($propertyMetadata);
118
        }
119
        
120 5
        return $classMetadata;
121
    }
122
    
123 5
    private function loadYml(ReflectionClass $class)
124
    {
125 5
        $className = $class->getName();
126
        
127 5
        $path = sprintf(
128 5
            '%s/%s.yml', 
129 5
            preg_replace('/\/$/', '', $this->path), 
130 5
            str_replace('\\', DIRECTORY_SEPARATOR, $className)
131
        );
132
        
133 5
        if (is_file($path)) {
134
            return $this->ymlParser->parse(file_get_contents($path));
135
        }
136
        
137 5
        $path = sprintf(
138 5
            '%s/%s.yml', 
139 5
            preg_replace('/\/$/', '', $this->path), 
140 5
            str_replace('\\', '.', $className)
141
        );
142
        
143 5
        if (is_file($path)) {
144 4
            return $this->ymlParser->parse(file_get_contents($path));
145
        }
146
147 1
        return [];
148
        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...
149
    }
150
}
151