Handler::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GBProd\ElasticaSpecification;
6
7
use Elastica\QueryBuilder;
8
use Elastica\Query\AbstractQuery;
9
use GBProd\ElasticaSpecification\QueryFactory\AndXFactory;
10
use GBProd\ElasticaSpecification\QueryFactory\Factory;
11
use GBProd\ElasticaSpecification\QueryFactory\NotFactory;
12
use GBProd\ElasticaSpecification\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 elastica specifications
20
 *
21
 * @author gbprod <[email protected]>
22
 */
23
class Handler
24
{
25
    /**
26
     * @param Registry
27
     */
28
    private $registry;
29
30
    /**
31
     * @param 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->qb = $qb;
42 3
        $this->registry = $registry;
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 querybuilder
51
     *
52
     * @param Specification $spec
53
     *
54
     * @return AbstractQuery
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