Completed
Pull Request — master (#5)
by Daniel
10:04 queued 08:01
created

AnnotationDriver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 58
c 0
b 0
f 0
rs 10

3 Methods

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