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

SymfonyContainerQueryHandlerLocator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 46
loc 46
c 1
b 0
f 0
wmc 6
lcom 1
cbo 2
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getQueryHandler() 4 4 1
A registerService() 4 4 1
A lazyLoad() 12 12 4

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\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