Registry::getFactory()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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