Handler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GBProd\DoctrineSpecification;
6
7
use Doctrine\ORM\QueryBuilder;
8
use Doctrine\ORM\Query\Expr\Base;
9
use GBProd\DoctrineSpecification\QueryFactory\AndXFactory;
10
use GBProd\DoctrineSpecification\QueryFactory\Factory;
11
use GBProd\DoctrineSpecification\QueryFactory\NotFactory;
12
use GBProd\DoctrineSpecification\QueryFactory\OrXFactory;
13
use GBProd\Specification\AndX;
14
use GBProd\Specification\Not;
15
use GBProd\Specification\OrX;
16
use GBProd\Specification\Specification;
17
18
/**
19
 * Handler for doctrine specifications
20
 *
21
 * @author gbprod <[email protected]>
22
 */
23
class Handler
24
{
25
    /**
26
     * @var Registry
27
     */
28
    private $registry;
29
30
    /**
31
     * @var QueryBuilder
32
     */
33
    private $qb;
34
35
    /**
36
     * @param Registry     $registry
37
     * @param QueryBuilder $qb
38
     */
39 3
    public function __construct(Registry $registry, QueryBuilder $qb)
40
    {
41 3
        $this->registry = $registry;
42 3
        $this->qb       = $qb;
43
44 3
        $this->registry->register(AndX::class, new AndXFactory($registry));
45 3
        $this->registry->register(OrX::class, new OrXFactory($registry));
46 3
        $this->registry->register(Not::class, new NotFactory($registry));
47 3
    }
48
49
    /**
50
     * handle specification for queryfactory
51
     *
52
     * @param Specification $spec
53
     *
54
     * @return Base
0 ignored issues
show
Documentation introduced by
Should the return type not be QueryFactory\Base?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
55
     */
56 1
    public function handle(Specification $spec)
57
    {
58 1
        $factory = $this->registry->getFactory($spec);
59
60 1
        return $factory->create($spec, $this->qb);
61
    }
62
63
    /**
64
     * Register a factory for specification
65
     *
66
     * @param string  $classname specification fully qualified classname
67
     * @param Factory $factory
68
     */
69 2
    public function registerFactory($classname, Factory $factory)
70
    {
71 2
        $this->registry->register($classname, $factory);
72 2
    }
73
}
74