Failed Conditions
Pull Request — experimental/3.1 (#2285)
by Kiyotaka
41:31
created

Queries::addCustomizer()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
4
namespace Eccube\Doctrine\Query;
5
6
7
use Doctrine\Common\Annotations\AnnotationReader;
8
use Doctrine\ORM\QueryBuilder;
9
use Eccube\Annotation\QueryExtension;
10
use Psr\Log\InvalidArgumentException;
11
12
class Queries
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
13
{
14
15
    private $customizers = [];
16
17
    public function addCustomizer(QueryCustomizer $customizer) {
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
18
        if (!$customizer) {
19
            throw new InvalidArgumentException('Customizer should not be null.');
20
        }
21
        $reader = new AnnotationReader();
22
        $rc = new \ReflectionClass($customizer);
23
        $anno = $reader->getClassAnnotation($rc, QueryExtension::class);
24
        if (!$anno) {
25
            throw new InvalidArgumentException(get_class($customizer).' doesn\'t have any '.QueryExtension::class.' annotation.');
26
        }
27
        foreach ($anno->value as $queryKey) {
28
            $this->customizers[$queryKey][] = $customizer;
29
        }
30
    }
31
32
    public function customize($queryKey, QueryBuilder $builder, $params)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
33
    {
34
        if (isset($this->customizers[$queryKey])) {
35
            /* @var QueryCustomizer $customizer */
36
            foreach ($this->customizers[$queryKey] as $customizer) {
37
                $customizer->customize($builder, $params, $queryKey);
38
            }
39
        }
40
        return $builder;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
41
    }
42
}