InMemoryLocator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 55
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A addHandler() 0 18 4
A locate() 0 8 2
1
<?php
2
/**
3
 * This file is part of the Cubiche package.
4
 *
5
 * Copyright (c) Cubiche
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Cubiche\Core\Bus\Middlewares\Handler\Locator;
11
12
use Cubiche\Core\Bus\Exception\NotFoundException;
13
use Cubiche\Core\Collections\ArrayCollection\ArrayHashMap;
14
15
/**
16
 * InMemoryLocator class.
17
 *
18
 * @author Ivannis Suárez Jerez <[email protected]>
19
 */
20
class InMemoryLocator implements LocatorInterface
21
{
22
    /**
23
     * @var ArrayHashMap
24
     */
25
    protected $handlers;
26
27
    /**
28
     * InMemoryLocator constructor.
29
     *
30
     * @param array $nameOfMessageToHandlerMap
31
     */
32
    public function __construct(array $nameOfMessageToHandlerMap = [])
33
    {
34
        $this->handlers = new ArrayHashMap();
35
        foreach ($nameOfMessageToHandlerMap as $nameOfMessage => $handler) {
36
            $this->addHandler($nameOfMessage, $handler);
37
        }
38
    }
39
40
    /**
41
     * @param string $nameOfMessage
42
     * @param object $handler
43
     */
44
    public function addHandler($nameOfMessage, $handler)
45
    {
46
        if (!is_string($nameOfMessage)) {
47
            throw new \InvalidArgumentException(sprintf(
48
                'Expected an string as a name of message. Instance of %s given',
49
                is_object($nameOfMessage) ? get_class($nameOfMessage) : gettype($nameOfMessage)
50
            ));
51
        }
52
53
        if (!is_object($handler)) {
54
            throw new \InvalidArgumentException(sprintf(
55
                'Expected an object as handler. Instance of %s given',
56
                gettype($handler)
57
            ));
58
        }
59
60
        $this->handlers->set($nameOfMessage, $handler);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function locate($nameOfMessage)
67
    {
68
        if (!$this->handlers->containsKey($nameOfMessage)) {
69
            throw NotFoundException::handlerFor($nameOfMessage);
70
        }
71
72
        return $this->handlers->get($nameOfMessage);
73
    }
74
}
75