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

Registry   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 39
ccs 10
cts 10
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
A getFactory() 0 11 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