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

ContainerQueryHandlerLocator::getQueryHandler()   A

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
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
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\Query\Handler\Locator;
12
13
use GpsLab\Component\Query\Handler\QueryHandler;
14
use GpsLab\Component\Query\Query;
15
use Psr\Container\ContainerInterface;
16
17 View Code Duplication
class ContainerQueryHandlerLocator implements QueryHandlerLocator
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...
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