Completed
Push — master ( 8e5551...30910b )
by GBProd
02:20
created

Registry::getBuilder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
3
namespace GBProd\ElasticaSpecification;
4
5
use GBProd\ElasticaSpecification\ExpressionBuilder\Builder;
6
use GBProd\Specification\Specification;
7
8
/**
9
 * Registry class for Expression builders
10
 *
11
 * @author gbprod <[email protected]>
12
 */
13
class Registry
14
{
15
    /**
16
     * @var array<Builder>
17
     */
18
    private $builders = [];
19
20
    /**
21
     * Register a builder
22
     *
23
     * @param string classname Fully qualified classname of the handled specification
24
     * @param Builder $builder
25
     */
26 1
    public function register($classname, Builder $builder)
27
    {
28 1
        $this->builders[$classname] = $builder;
29 1
    }
30
31
    /**
32
     * Get registred builder for Specification
33
     *
34
     * @param Specification $spec
35
     *
36
     * @return Builder
37
     *
38
     * @throw OutOfRangeException if builder not found
39
     */
40 2
    public function getBuilder(Specification $spec)
41
    {
42 2
        if (!isset($this->builders[get_class($spec)])) {
43 1
            throw new \OutOfRangeException(sprintf(
44 1
                'Builder for Specification "%s" not registred',
45 1
                get_class($spec)
46 1
            ));
47
        }
48
49 1
        return $this->builders[get_class($spec)];
50
    }
51
}
52