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

SymfonyContainerQueryHandlerLocator::lazyLoad()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 12
loc 12
c 1
b 0
f 0
rs 9.2
cc 4
eloc 6
nc 3
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 Symfony\Component\DependencyInjection\ContainerAwareInterface;
16
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
19 View Code Duplication
class SymfonyContainerQueryHandlerLocator implements QueryHandlerLocator, ContainerAwareInterface
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...
20
{
21
    use ContainerAwareTrait;
22
23
    /**
24
     * @var string[]
25
     */
26
    private $query_handler_ids = [];
27
28
    /**
29
     * @param Query $query
30
     *
31
     * @return QueryHandler|null
32
     */
33
    public function getQueryHandler(Query $query)
34
    {
35
        return $this->lazyLoad(get_class($query));
36
    }
37
38
    /**
39
     * @param string $query_name
40
     * @param string $service
41
     */
42
    public function registerService($query_name, $service)
43
    {
44
        $this->query_handler_ids[$query_name] = $service;
45
    }
46
47
    /**
48
     * @param $query_name
49
     *
50
     * @return QueryHandler
51
     */
52
    private function lazyLoad($query_name)
53
    {
54
        if ($this->container instanceof ContainerInterface && isset($this->query_handler_ids[$query_name])) {
55
            $handler = $this->container->get($this->query_handler_ids[$query_name]);
56
57
            if ($handler instanceof QueryHandler) {
58
                return $handler;
59
            }
60
        }
61
62
        return null;
63
    }
64
}
65