Completed
Pull Request — master (#5)
by Daniel
08:08 queued 06:02
created

AnnotationDriver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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