AnnotationDriver::getGridMetadata()   B
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 44
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 32
nc 8
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid\Metadata\Driver;
6
7
use Doctrine\Common\Annotations\AnnotationReader;
8
use Doctrine\Common\Annotations\Reader;
9
use Metadata\Driver\DriverInterface;
10
use Psi\Component\Grid\Metadata\ActionMetadata;
11
use Psi\Component\Grid\Metadata\Annotations;
12
use Psi\Component\Grid\Metadata\ClassMetadata;
13
use Psi\Component\Grid\Metadata\ColumnMetadata;
14
use Psi\Component\Grid\Metadata\FilterMetadata;
15
use Psi\Component\Grid\Metadata\GridMetadata;
16
use Psi\Component\Grid\Metadata\QueryMetadata;
17
18
class AnnotationDriver implements DriverInterface
19
{
20
    /**
21
     * @var Reader
22
     */
23
    private $reader;
24
25
    public function __construct(Reader $reader = null)
26
    {
27
        $this->reader = $reader ?: new AnnotationReader();
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function loadMetadataForClass(\ReflectionClass $class)
34
    {
35
        $annotations = $this->reader->getClassAnnotations($class);
36
37
        $grids = [];
38
        $queries = [];
39
40
        foreach ($annotations as $annotation) {
41
            if ($annotation instanceof Annotations\Grid) {
42
                $grids[$annotation->name] = $this->getGridMetadata($annotation);
43
            }
44
45
            if ($annotation instanceof Annotations\Query) {
46
                $queries[$annotation->name] = $this->getQueryMetadata($annotation);
47
            }
48
        }
49
50
        if (empty($grids)) {
51
            return;
52
        }
53
54
        return new ClassMetadata($class->getName(), $grids, $queries);
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
55
    }
56
57
    private function getGridMetadata(Annotations\Grid $gridAnnot)
58
    {
59
        $columns = [];
60
        foreach ($gridAnnot->columns as $columnAnnot) {
61
            $columns[$columnAnnot->name] = new ColumnMetadata(
62
                $columnAnnot->name,
63
                $columnAnnot->type,
64
                $columnAnnot->groups,
65
                $columnAnnot->options,
66
                $columnAnnot->tags
67
            );
68
        }
69
70
        $filters = [];
71
        foreach ($gridAnnot->filters as $filterAnnot) {
72
            $filters[$filterAnnot->name] = new FilterMetadata(
73
                $filterAnnot->name,
74
                $filterAnnot->type,
75
                $filterAnnot->field,
76
                $filterAnnot->defaults,
77
                $filterAnnot->options,
78
                $filterAnnot->tags
79
            );
80
        }
81
82
        $actions = [];
83
        foreach ($gridAnnot->actions as $actionAnnot) {
84
            $actions[$actionAnnot->name] = new ActionMetadata(
85
                $actionAnnot->name,
86
                $actionAnnot->type,
87
                $actionAnnot->options,
88
                $actionAnnot->tags
89
            );
90
        }
91
92
        return new GridMetadata(
93
            $gridAnnot->name,
94
            $columns,
95
            $filters,
96
            $actions,
97
            $gridAnnot->pageSize,
98
            $gridAnnot->query
99
        );
100
    }
101
102
    private function getQueryMetadata(Annotations\Query $query)
103
    {
104
        return new QueryMetadata(
105
            $query->name,
106
            $query->selects,
107
            $query->joins,
108
            $query->criteria
109
        );
110
    }
111
}
112