Completed
Pull Request — master (#5)
by Daniel
06:54
created

AnnotationDriver::getGridMetadata()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 28
rs 8.8571
c 1
b 0
f 0
cc 3
eloc 19
nc 4
nop 1
1
<?php
2
3
namespace Psi\Component\Grid\Metadata\Driver;
4
5
use Doctrine\Common\Annotations\Reader;
6
use Metadata\Driver\DriverInterface;
7
use Psi\Component\Grid\Metadata\Annotations;
8
use Psi\Component\Grid\Metadata\ClassMetadata;
9
use Psi\Component\Grid\Metadata\ColumnMetadata;
10
use Psi\Component\Grid\Metadata\GridMetadata;
11
use Psi\Component\Grid\Metadata\FilterMetadata;
12
use Psi\Component\Grid\Metadata\Annotations\Grid;
13
14
class AnnotationDriver implements DriverInterface
15
{
16
    /**
17
     * @var Reader
18
     */
19
    private $reader;
20
21
    public function __construct(Reader $reader)
22
    {
23
        $this->reader = $reader;
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function loadMetadataForClass(\ReflectionClass $class)
30
    {
31
        $annotations = $this->reader->getClassAnnotations($class);
32
33
        $grids = [];
34
        foreach ($annotations as $annotation) {
35
            if ($annotation instanceof Grid) {
36
                $grids[$annotation->name] = $this->getGridMetadata($annotation);
37
            }
38
        }
39
40
        return new ClassMetadata($class->getName(), $grids);
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
41
    }
42
43
    private function getGridMetadata(Grid $gridAnnot)
44
    {
45
        $columns = [];
46
        foreach ($gridAnnot->columns as $columnAnnot) {
47
            $columns[$columnAnnot->name] = new ColumnMetadata(
48
                $columnAnnot->name,
49
                $columnAnnot->type,
50
                $columnAnnot->options
51
            );
52
        }
53
54
        $filters = [];
55
        foreach ($gridAnnot->filters as $filterAnnot) {
56
            $filters[$filterAnnot->name] = new FilterMetadata(
57
                $filterAnnot->name,
58
                $filterAnnot->type,
59
                $filterAnnot->field,
60
                $filterAnnot->options
61
            );
62
        }
63
64
        return new GridMetadata(
65
            $gridAnnot->name,
66
            $columns,
67
            $filters,
68
            $gridAnnot->pageSize
69
        );
70
    }
71
}
72