Completed
Push — master ( 73efce...6751ec )
by JHONATAN
05:35
created

YmlDriver::loadYml()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.004

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 12
cts 13
cp 0.9231
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 1
crap 3.004
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
            $idMapping = $yml['id'] ?? [];
77 4
78
            $ids = is_scalar($idMapping) ? [$idMapping] : $idMapping;
79
80 5
            if (in_array($name, $ids)) {
81 4
                $annotations[Id::class] = new Id();
82 4
            }
83 4
            
84
            if (isset($yml['parameters'][$reflectionProperty->name])) {
85 4
                $config           = $yml['parameters'][$name];
86 2
                $bindings->source = $config['bindings']['source'] ?? null;
87 2
                $bindings->target = $config['bindings']['target'] ?? null;
88 2
89
                if (isset($config['belongsTo'])) {
90
                    $belongsTo = new BelongsTo();
91 4
                    $belongsTo->foreignField = $config['belongsTo']['foreignField'];
92 1
                    $annotations[BelongsTo::class] = $belongsTo;
93 1
                }
94 1
                
95
                if (isset($config['hasOne'])) {
96
                    $hasOne = new HasOne();
97 4
                    $hasOne->foreignField = $config['hasOne']['foreignField'];
98 1
                    $annotations[HasOne::class] = $hasOne;
99 1
                }
100 1
                
101
                if (isset($config['hasMany'])) {
102
                    $hasMany = new HasMany();
103 4
                    $hasMany->foreignField = $config['hasMany']['foreignField'];
104 1
                    $annotations[HasMany::class] = $hasMany;
105 1
                }
106 1
                
107 1
                if (isset($config['exclude'])) {
108
                    $exclude = new Exclude();
109
                    $exclude->input  = $config['exlude']['input'] ?? true;
110
                    $exclude->output = $config['exlude']['output'] ?? true;
111
                    $annotations[Exclude::class] = $exclude;
112 5
                }
113 5
            }
114
            
115 5
            /* @var $propertyMetadata PropertyMetadata */
116
            $propertyMetadata = (new ReflectionClass($this->propertyMetadataClassName))
117 5
                ->newInstance($class->name, $reflectionProperty->name);
118
            
119
            $propertyMetadata->setAnnotations($annotations);
120 5
            
121
            $classMetadata->addPropertyMetadata($propertyMetadata);
122
        }
123 5
        
124
        return $classMetadata;
125 5
    }
126
    
127 5
    private function loadYml(ReflectionClass $class)
128 5
    {
129 5
        $className = $class->getName();
130 5
        
131
        $path = sprintf(
132
            '%s/%s.yml', 
133 5
            preg_replace('/\/$/', '', $this->path), 
134
            str_replace('\\', DIRECTORY_SEPARATOR, $className)
135
        );
136
        
137 5
        if (is_file($path)) {
138 5
            return $this->ymlParser->parse(file_get_contents($path));
139 5
        }
140 5
        
141
        $path = sprintf(
142
            '%s/%s.yml', 
143 5
            preg_replace('/\/$/', '', $this->path), 
144 4
            str_replace('\\', '.', $className)
145
        );
146
        
147 1
        if (is_file($path)) {
148
            return $this->ymlParser->parse(file_get_contents($path));
149
        }
150
151
        return [];
152
        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...
153
    }
154
}
155