Passed
Push — master ( ee6b03...f5a05a )
by Aleksandar
02:24
created

ModelCompiler::resolveColumns()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 22
ccs 12
cts 12
cp 1
crap 4
rs 9.8666
1
<?php
2
3
namespace ArekX\PQL\Orm;
4
5
use ArekX\PQL\Orm\Attributes\Column;
6
use ArekX\PQL\Orm\Attributes\MultiRelation;
0 ignored issues
show
Bug introduced by
The type ArekX\PQL\Orm\Attributes\MultiRelation was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use ArekX\PQL\Orm\Attributes\Relation;
8
use ArekX\PQL\Orm\Attributes\Table;
9
use ArekX\PQL\Orm\Attributes\ViaToModel;
10
use ReflectionClass;
11
use ReflectionProperty;
12
13
class ModelCompiler
14
{
15
    protected $compiled = [];
16
17 1
    public function resolve(string $modelClass): array
18
    {
19 1
        if (!empty($this->compiled[$modelClass])) {
20 1
            return $this->compiled[$modelClass];
21
        }
22
23 1
        $resolvedModel = [];
24
25 1
        $reflected = new ReflectionClass($modelClass);
26
27 1
        $resolvedModel['table'] = $reflected->getAttributes(Table::class)[0]->getArguments()[0];
28
29 1
        $properties = $reflected->getProperties(ReflectionProperty::IS_PUBLIC);
30
31 1
        $resolvedModel['columns'] = $this->resolveColumns($properties);
32
33 1
        $propertyColumnMap = array_flip($resolvedModel['columns']);
34
35 1
        $resolvedModel['relations'] = $this->resolveRelations($properties, $propertyColumnMap);
36
37 1
        $this->compiled[$modelClass] = $resolvedModel;
38
39 1
        foreach ($this->compiled[$modelClass]['relations'] as &$config) {
40 1
            $config['table'] = $this->resolve($config['model'])['table'];
41
        }
42
43 1
        return $this->compiled[$modelClass];
44
    }
45
46 1
    protected function resolveColumns(array $properties): array
47
    {
48 1
        $columns = [];
49
50 1
        foreach ($properties as $property) {
51 1
            $column = $property->getAttributes(Column::class);
52 1
            $propertyName = $property->getName();
53
54 1
            if (!empty($column)) {
55 1
                $column = $column[0]->getArguments();
56
57 1
                if (empty($column)) {
58 1
                    $column = $propertyName;
59
                } else {
60 1
                    $column = $column[0];
61
                }
62
63 1
                $columns[$column] = $propertyName;
64
            }
65
        }
66
67 1
        return $columns;
68
    }
69
70 1
    protected function resolveRelations(array $properties, array $propertyColumnMap): array
71
    {
72 1
        $relations = [];
73
74 1
        foreach ($properties as $property) {
75 1
            $relation = $property->getAttributes(Relation::class);
76
77 1
            if (empty($relation)) {
78 1
                continue;
79
            }
80
81 1
            $propertyName = $property->getName();
82
83 1
            [
84 1
                'via' => $via,
85 1
                'model' => $model,
86 1
                'at' => $at,
87 1
                'multiple' => $multipleResults
88 1
            ] = $relation[0]->getArguments();
89
90 1
            $relations[$propertyName] = [
91 1
                'model' => $model,
92 1
                'foreign_key' => $propertyColumnMap[$via],
93 1
                'table_key' => $at,
94 1
                'multiple_results' => $multipleResults
95 1
            ];
96
97 1
            $viaToModel = $property->getAttributes(ViaToModel::class);
0 ignored issues
show
Unused Code introduced by
The assignment to $viaToModel is dead and can be removed.
Loading history...
98
        }
99
100 1
        return $relations;
101
    }
102
}