InteropContainerResolver::instantiate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
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\CommandBus\Resolver;
12
13
use Interop\Container\ContainerInterface;
14
use InvalidArgumentException;
15
use NilPortugues\MessageBus\CommandBus\Contracts\CommandHandler;
16
use NilPortugues\MessageBus\CommandBus\Contracts\CommandHandlerResolver;
17
18
/**
19
 * Class InteropContainerResolver.
20
 */
21 View Code Duplication
class InteropContainerResolver implements CommandHandlerResolver
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 Command Handler, return the instance.
38
     *
39
     * @param string $handler
40
     *
41
     * @return CommandHandler
42
     */
43
    public function instantiate(string $handler) : CommandHandler
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