Completed
Push — master ( 134712...8789de )
by Peter
06:34
created

ContainerCommandHandlerLocator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 57
loc 57
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A getCommandHandler() 4 4 1
A registerService() 4 4 1
A lazyLoad() 12 12 3

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
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Component\Command\Locator\Handler;
12
13
use GpsLab\Component\Command\Command;
14
use GpsLab\Component\Command\Handler\CommandHandler;
15
use GpsLab\Component\Command\Handler\Locator\CommandHandlerLocator;
16
use Psr\Container\ContainerInterface;
17
18 View Code Duplication
class ContainerCommandHandlerLocator implements CommandHandlerLocator
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...
19
{
20
    /**
21
     * @var ContainerInterface
22
     */
23
    private $container;
24
25
    /**
26
     * @var string[]
27
     */
28
    private $command_handler_ids = [];
29
30
    /**
31
     * @param ContainerInterface $container
32
     */
33
    public function __construct(ContainerInterface $container)
34
    {
35
        $this->container = $container;
36
    }
37
38
    /**
39
     * @param Command $command
40
     *
41
     * @return CommandHandler|null
42
     */
43
    public function getCommandHandler(Command $command)
44
    {
45
        return $this->lazyLoad(get_class($command));
46
    }
47
48
    /**
49
     * @param string $command_name
50
     * @param string $service
51
     */
52
    public function registerService($command_name, $service)
53
    {
54
        $this->command_handler_ids[$command_name] = $service;
55
    }
56
57
    /**
58
     * @param $command_name
59
     *
60
     * @return CommandHandler
61
     */
62
    private function lazyLoad($command_name)
63
    {
64
        if (isset($this->command_handler_ids[$command_name])) {
65
            $handler = $this->container->get($this->command_handler_ids[$command_name]);
66
67
            if ($handler instanceof CommandHandler) {
68
                return $handler;
69
            }
70
        }
71
72
        return null;
73
    }
74
}
75