Completed
Push — master ( a017c2...285708 )
by GBProd
02:17
created

Handler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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