Completed
Push — master ( b39006...2667f3 )
by GBProd
03:38
created

Registry::getBuilder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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