SymfonyContainerQueryHandlerLocator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 80
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A findHandler() 0 4 1
A registerService() 0 4 1
A registerSubscriberService() 0 9 4
A lazyLoad() 0 10 3
A resolve() 0 14 3
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * GpsLab component.
6
 *
7
 * @author    Peter Gribanov <[email protected]>
8
 * @copyright Copyright (c) 2011, Peter Gribanov
9
 * @license   http://opensource.org/licenses/MIT
10
 */
11
12
namespace GpsLab\Component\Query\Handler\Locator;
13
14
use GpsLab\Component\Query\Handler\QuerySubscriber;
15
use GpsLab\Component\Query\Query;
16
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
17
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
20
class SymfonyContainerQueryHandlerLocator implements QueryHandlerLocator, ContainerAwareInterface
21
{
22
    use ContainerAwareTrait;
23
24
    /**
25
     * @var array[]
26
     */
27
    private $query_handler_ids = [];
28
29
    /**
30
     * @param Query $query
31
     *
32
     * @return callable|null
33
     */
34 7
    public function findHandler(Query $query): ?callable
35
    {
36 7
        return $this->lazyLoad(get_class($query));
37
    }
38
39
    /**
40
     * @param string $query_name
41
     * @param string $service
42
     * @param string $method
43
     */
44 6
    public function registerService(string $query_name, string $service, string $method = '__invoke'): void
45
    {
46 6
        $this->query_handler_ids[$query_name] = [$service, $method];
47 6
    }
48
49
    /**
50
     * @param string $service_name
51
     * @param string $class_name
52
     */
53 1
    public function registerSubscriberService(string $service_name, string $class_name): void
54
    {
55 1
        $get_subscribed_queries = [$class_name, 'getSubscribedQueries'];
56 1
        if (is_callable($get_subscribed_queries) && is_a($class_name, QuerySubscriber::class, true)) {
57 1
            foreach ($get_subscribed_queries() as $query_name => $method) {
58 1
                $this->registerService($query_name, $service_name, $method);
59
            }
60
        }
61 1
    }
62
63
    /**
64
     * @param string $query_name
65
     *
66
     * @return callable|null
67
     */
68 7
    private function lazyLoad(string $query_name): ?callable
69
    {
70 7
        if ($this->container instanceof ContainerInterface && isset($this->query_handler_ids[$query_name])) {
71 5
            [$service, $method] = $this->query_handler_ids[$query_name];
0 ignored issues
show
Bug introduced by
The variable $service does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $method does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
72
73 5
            return $this->resolve($this->container->get($service), $method);
74
        }
75
76 2
        return null;
77
    }
78
79
    /**
80
     * @param mixed  $service
81
     * @param string $method
82
     *
83
     * @return callable|null
84
     */
85 5
    private function resolve($service, string $method): ?callable
86
    {
87 5
        if (is_callable($service)) {
88 1
            return $service;
89
        }
90
91 4
        $handler = [$service, $method];
92
93 4
        if (is_callable($handler)) {
94 2
            return $handler;
95
        }
96
97 2
        return null;
98
    }
99
}
100