CommandHandlerLocator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Mcustiel\OneHundredFour\Command;
6
7
use Mcustiel\OneHundredFour\Command\Exceptions\CommandHandlerLocatorException;
8
use Mcustiel\OneHundredFour\Command\Exceptions\UnregisteredCommandHandlerException;
9
use Mcustiel\OneHundredFour\HandlerIdentifier;
10
use Psr\Container\ContainerInterface;
11
12
class CommandHandlerLocator
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 CommandHandlerLocatorException */
23 2
    public function locate(HandlerIdentifier $identifier): CommandHandler
24
    {
25 2
        $stringIdentifier = $identifier->get();
26 2
        $this->ensureHandlerExists($stringIdentifier);
27
28 1
        return $this->creator->get($stringIdentifier);
29
    }
30
31
    /** @throws UnregisteredCommandHandlerException */
32 2
    private function ensureHandlerExists(string $stringIdentifier)
33
    {
34 2
        if (!$this->creator->has($stringIdentifier)) {
35 1
            throw new UnregisteredCommandHandlerException(
36 1
                'Can not get instance of handler with id: ' . $stringIdentifier
37
            );
38
        }
39 1
    }
40
}
41