QueryHandlerLocator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 25
ccs 11
cts 11
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A ensureHandlerExists() 0 5 2
A locate() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Mcustiel\OneHundredFour\Query;
6
7
use Mcustiel\OneHundredFour\HandlerIdentifier;
8
use Mcustiel\OneHundredFour\Query\Exceptions\QueryHandlerLocatorException;
9
use Mcustiel\OneHundredFour\Query\Exceptions\UnregisteredQueryHandlerException;
10
use Psr\Container\ContainerInterface;
11
12
class QueryHandlerLocator
13
{
14
    /** @var ContainerInterface */
15
    private $creator;
16
17 2
    public function __construct(ContainerInterface $creator)
18
    {
19 2
        $this->creator = $creator;
20 2
    }
21
22
    /** @throws QueryHandlerLocatorException */
23 2
    public function locate(HandlerIdentifier $identifier): QueryHandler
24
    {
25 2
        $stringIdentifier = $identifier->get();
26 2
        $this->ensureHandlerExists($stringIdentifier);
27
28 1
        return $this->creator->get($stringIdentifier);
29
    }
30
31
    /** @throws UnregisteredQueryHandlerException */
32 2
    private function ensureHandlerExists(string $stringIdentifier)
33
    {
34 2
        if (!$this->creator->has($stringIdentifier)) {
35 1
            throw new UnregisteredQueryHandlerException(
36 1
                'Can not get instance of handler with id: ' . $stringIdentifier
37
            );
38
        }
39 1
    }
40
}
41