Completed
Push — master ( 88c361...c3d442 )
by GBProd
05:04
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 0
Metric Value
c 0
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\DoctrineSpecification;
4
5
use GBProd\DoctrineSpecification\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 = array();
19
20
    /**
21
     * Register a factory
22
     *
23
     * @param string classname Fully qualified classname of the handled specification
24
     * @param Factory $factory
25
     */
26 10
    public function register($classname, Factory $factory)
27
    {
28 10
        $this->factories[$classname] = $factory;
29 10
    }
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 11
    public function getFactory(Specification $spec)
41
    {
42 11
        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 10
        return $this->factories[get_class($spec)];
50
    }
51
}
52