InteropContainerResolver   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 33
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A instantiate() 0 10 2
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 23/03/16
5
 * Time: 22:11.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace NilPortugues\MessageBus\QueryBus\Resolver;
12
13
use Interop\Container\ContainerInterface;
14
use InvalidArgumentException;
15
use NilPortugues\MessageBus\QueryBus\Contracts\QueryHandler;
16
use NilPortugues\MessageBus\QueryBus\Contracts\QueryHandlerResolver;
17
18
/**
19
 * Class InteropContainerResolver.
20
 */
21
class InteropContainerResolver implements QueryHandlerResolver
22
{
23
    /** @var ContainerInterface */
24
    protected $container;
25
26
    /**
27
     * InteropContainerResolver constructor.
28
     *
29
     * @param ContainerInterface $container
30
     */
31
    public function __construct(ContainerInterface $container)
32
    {
33
        $this->container = $container;
34
    }
35
36
    /**
37
     * Given a string to identify the Query Handler, return the instance.
38
     *
39
     * @param string $handler
40
     *
41
     * @return QueryHandler
42
     */
43
    public function instantiate(string $handler) : QueryHandler
44
    {
45
        if (false === $this->container->has($handler)) {
46
            throw new InvalidArgumentException(
47
                sprintf('Handler %s could not be found. Did you register it?', $handler)
48
            );
49
        }
50
51
        return $this->container->get($handler);
52
    }
53
}
54