Code Duplication    Length = 57-57 lines in 2 locations

src/Command/Handler/Locator/ContainerCommandHandlerLocator.php 1 location

@@ 18-74 (lines=57) @@
15
use GpsLab\Component\Command\Handler\Locator\CommandHandlerLocator;
16
use Psr\Container\ContainerInterface;
17
18
class ContainerCommandHandlerLocator implements CommandHandlerLocator
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

src/Query/Handler/Locator/ContainerQueryHandlerLocator.php 1 location

@@ 17-73 (lines=57) @@
14
use GpsLab\Component\Query\Query;
15
use Psr\Container\ContainerInterface;
16
17
class ContainerQueryHandlerLocator implements QueryHandlerLocator
18
{
19
    /**
20
     * @var ContainerInterface
21
     */
22
    private $container;
23
24
    /**
25
     * @var string[]
26
     */
27
    private $query_handler_ids = [];
28
29
    /**
30
     * @param ContainerInterface $container
31
     */
32
    public function __construct(ContainerInterface $container)
33
    {
34
        $this->container = $container;
35
    }
36
37
    /**
38
     * @param Query $query
39
     *
40
     * @return QueryHandler|null
41
     */
42
    public function getQueryHandler(Query $query)
43
    {
44
        return $this->lazyLoad(get_class($query));
45
    }
46
47
    /**
48
     * @param string $query_name
49
     * @param string $service
50
     */
51
    public function registerService($query_name, $service)
52
    {
53
        $this->query_handler_ids[$query_name] = $service;
54
    }
55
56
    /**
57
     * @param $query_name
58
     *
59
     * @return QueryHandler
60
     */
61
    private function lazyLoad($query_name)
62
    {
63
        if (isset($this->query_handler_ids[$query_name])) {
64
            $handler = $this->container->get($this->query_handler_ids[$query_name]);
65
66
            if ($handler instanceof QueryHandler) {
67
                return $handler;
68
            }
69
        }
70
71
        return null;
72
    }
73
}
74