Completed
Push — master ( a51ad4...289114 )
by Peter
03:16
created

SymfonyContainerQueryHandlerLocator::lazyLoad()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 8.8571
c 1
b 0
f 0
cc 5
eloc 9
nc 4
nop 1
crap 5
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\Query;
14
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
18
class SymfonyContainerQueryHandlerLocator implements QueryHandlerLocator, ContainerAwareInterface
19
{
20
    use ContainerAwareTrait;
21
22
    /**
23
     * @var array
24
     */
25
    private $query_handler_ids = [];
26
27
    /**
28
     * @param Query $query
29
     *
30
     * @return callable|null
31
     */
32 5
    public function findHandler(Query $query)
33
    {
34 5
        return $this->lazyLoad(get_class($query));
35
    }
36
37
    /**
38
     * @param string $query_name
39
     * @param string $service
40
     * @param string $method
41
     */
42 5
    public function registerService($query_name, $service, $method = '__invoke')
43
    {
44 5
        $this->query_handler_ids[$query_name] = [$service, $method];
45 5
    }
46
47
    /**
48
     * @param $query_name
49
     *
50
     * @return callable
51
     */
52 5
    private function lazyLoad($query_name)
53
    {
54 5
        if ($this->container instanceof ContainerInterface && isset($this->query_handler_ids[$query_name])) {
55 4
            list($service, $method) = $this->query_handler_ids[$query_name];
56 4
            $handler = $this->container->get($service);
57
58 4
            if (is_callable($handler)) {
59 1
                return $handler;
60 3
            } elseif (is_callable([$handler, $method])) {
61 1
                return [$handler, $method];
62
            }
63 2
        }
64
65 3
        return null;
66
    }
67
}
68