QueryHandlerLocator::ensureHandlerExists()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 10
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