SymfonyContainerResolver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 15/02/17
5
 * Time: 11:13.
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 Symfony\Component\DependencyInjection\ContainerInterface;
14
use InvalidArgumentException;
15
use NilPortugues\MessageBus\CommandBus\Contracts\CommandHandler;
16
use NilPortugues\MessageBus\CommandBus\Contracts\CommandHandlerResolver;
17
18
/**
19
 * Class SymfonyContainerResolver.
20
 */
21 View Code Duplication
class SymfonyContainerResolver 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
     * SymfonyContainerResolver 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
        $handler = ltrim($handler, '\\');
46
        if (false === $this->container->has($handler)) {
47
            throw new InvalidArgumentException(
48
                sprintf('Handler %s could not be found. Did you register it?', $handler)
49
            );
50
        }
51
52
        return $this->container->get($handler);
53
    }
54
}
55