1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cubiche package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Cubiche |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace Cubiche\Core\Cqrs\Query; |
11
|
|
|
|
12
|
|
|
use Cubiche\Core\Bus\Bus; |
13
|
|
|
use Cubiche\Core\Bus\Exception\NotFoundException; |
14
|
|
|
use Cubiche\Core\Bus\MessageInterface; |
15
|
|
|
use Cubiche\Core\Bus\Middlewares\Handler\Locator\InMemoryLocator; |
16
|
|
|
use Cubiche\Core\Bus\Middlewares\Handler\Resolver\HandlerClass\HandlerClassResolver; |
17
|
|
|
use Cubiche\Core\Bus\Middlewares\Handler\Resolver\HandlerMethodName\MethodWithShortObjectNameResolver; |
18
|
|
|
use Cubiche\Core\Cqrs\Middlewares\Handler\QueryHandlerMiddleware; |
19
|
|
|
use Cubiche\Core\Cqrs\Middlewares\Handler\Resolver\NameOfQuery\ChainResolver as NameOfQueryChainResolver; |
20
|
|
|
use Cubiche\Core\Cqrs\Middlewares\Handler\Resolver\NameOfQuery\FromClassNameResolver; |
21
|
|
|
use Cubiche\Core\Cqrs\Middlewares\Handler\Resolver\NameOfQuery\FromQueryNamedResolver; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* QueryBus class. |
25
|
|
|
* |
26
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class QueryBus extends Bus |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var QueryHandlerMiddleware |
32
|
|
|
*/ |
33
|
|
|
protected $queryHandlerMiddleware; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return QueryBus |
37
|
|
|
*/ |
38
|
|
|
public static function create() |
39
|
|
|
{ |
40
|
|
|
return new static([ |
41
|
|
|
250 => new QueryHandlerMiddleware(new HandlerClassResolver( |
42
|
|
|
new NameOfQueryChainResolver([ |
43
|
|
|
new FromQueryNamedResolver(), |
44
|
|
|
new FromClassNameResolver(), |
45
|
|
|
]), |
46
|
|
|
new MethodWithShortObjectNameResolver('Query'), |
47
|
|
|
new InMemoryLocator() |
48
|
|
|
)), |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function dispatch(MessageInterface $query) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
if (!$query instanceof QueryInterface) { |
58
|
|
|
throw new \InvalidArgumentException( |
59
|
|
|
sprintf( |
60
|
|
|
'The object must be an instance of %s. Instance of %s given', |
61
|
|
|
QueryInterface::class, |
62
|
|
|
get_class($query) |
63
|
|
|
) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->ensureQueryHandlerMiddleware(); |
68
|
|
|
|
69
|
|
|
return parent::dispatch($query); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Ensure that exists an query handler middleware. |
74
|
|
|
* |
75
|
|
|
* @throws NotFoundException |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
protected function ensureQueryHandlerMiddleware() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
if ($this->queryHandlerMiddleware !== null) { |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
foreach ($this->middlewares as $priority => $collection) { |
84
|
|
|
foreach ($collection as $middleware) { |
85
|
|
|
if ($middleware instanceof QueryHandlerMiddleware) { |
86
|
|
|
$this->queryHandlerMiddleware = $middleware; |
87
|
|
|
|
88
|
|
|
return; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
throw NotFoundException::middlewareOfType(QueryHandlerMiddleware::class); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return QueryHandlerMiddleware |
98
|
|
|
*/ |
99
|
|
|
public function handlerMiddleware() |
100
|
|
|
{ |
101
|
|
|
$this->ensureQueryHandlerMiddleware(); |
102
|
|
|
|
103
|
|
|
return $this->queryHandlerMiddleware; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $queryName |
108
|
|
|
* @param mixed $queryHandler |
109
|
|
|
*/ |
110
|
|
|
public function addHandler($queryName, $queryHandler) |
111
|
|
|
{ |
112
|
|
|
$this->ensureQueryHandlerMiddleware(); |
113
|
|
|
|
114
|
|
|
$this->queryHandlerMiddleware->resolver()->addHandler($queryName, $queryHandler); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
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.