Completed
Pull Request — master (#4)
by GBProd
02:45
created

Handler::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace GBProd\DoctrineSpecification;
4
5
use Doctrine\ORM\QueryBuilder;
6
use Doctrine\ORM\Query\Expr\Base;
7
use GBProd\DoctrineSpecification\QueryFactory\AndXFactory;
8
use GBProd\DoctrineSpecification\QueryFactory\Factory;
9
use GBProd\DoctrineSpecification\QueryFactory\NotFactory;
10
use GBProd\DoctrineSpecification\QueryFactory\OrXFactory;
11
use GBProd\Specification\AndX;
12
use GBProd\Specification\Not;
13
use GBProd\Specification\OrX;
14
use GBProd\Specification\Specification;
15
16
/**
17
 * Handler for doctrine specifications
18
 *
19
 * @author gbprod <[email protected]>
20
 */
21
class Handler
22
{
23
    /**
24
     * @var Registry
25
     */
26
    private $registry;
27
28
    /**
29
     * @var QueryBuilder
30
     */
31
    private $qb;
32
33
    /**
34
     * @param Registry     $registry
35
     * @param QueryBuilder $qb
36
     */
37 1
    public function __construct(Registry $registry, QueryBuilder $qb)
38
    {
39 1
        $this->registry = $registry;
40 1
        $this->qb       = $qb;
41
42 1
        $this->registry->register(AndX::class, new AndXFactory($registry));
43 1
        $this->registry->register(OrX::class, new OrXFactory($registry));
44 1
        $this->registry->register(Not::class, new NotFactory($registry));
45 1
    }
46
47
    /**
48
     * handle specification for queryfactory
49
     *
50
     * @param Specification $spec
51
     *
52
     * @return Base
53
     */
54
    public function handle(Specification $spec)
55
    {
56
        $factory = $this->registry->getFactory($spec);
57
58
        return $factory->create($spec, $this->qb);
59
    }
60
61
    /**
62
     * Register a factory for specification
63
     *
64
     * @param string  $classname specification fully qualified classname
65
     * @param Factory $factory
66
     */
67
    public function registerFactory($classname, Factory $factory)
68
    {
69
        $this->registry->register($classname, $factory);
70
    }
71
}
72