SymfonyContainerResolver   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A instantiate() 11 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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