-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryHandler.php
More file actions
60 lines (49 loc) · 1.49 KB
/
QueryHandler.php
File metadata and controls
60 lines (49 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/*
* Copyright 2025 Cloud Creativity Limited
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
declare(strict_types=1);
namespace CloudCreativity\Modules\Application\Bus;
use CloudCreativity\Modules\Contracts\Application\Bus\QueryHandler as IQueryHandler;
use CloudCreativity\Modules\Contracts\Application\Messages\DispatchThroughMiddleware;
use CloudCreativity\Modules\Contracts\Toolkit\Messages\Query;
use CloudCreativity\Modules\Contracts\Toolkit\Result\Result;
final readonly class QueryHandler implements IQueryHandler
{
/**
* QueryHandler constructor.
*
* @param object $handler
*/
public function __construct(private object $handler)
{
}
/**
* @inheritDoc
*/
public function __invoke(Query $query): Result
{
assert(method_exists($this->handler, 'execute'), sprintf(
'Cannot dispatch "%s" - handler "%s" does not have an execute method.',
$query::class,
$this->handler::class,
));
$result = $this->handler->execute($query);
assert($result instanceof Result, 'Expecting query handler to return a result.');
return $result;
}
/**
* @inheritDoc
*/
public function middleware(): array
{
if ($this->handler instanceof DispatchThroughMiddleware) {
return $this->handler->middleware();
}
return [];
}
}