QueryBus   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 10
c 1
b 0
f 0
dl 0
loc 37
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 13 2
A __construct() 0 3 1
1
<?php
2
3
/*
4
 * cqrs-tactician (https://github.com/phpgears/cqrs-tactician).
5
 * CQRS implementation with League Tactician.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/cqrs-tactician
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\CQRS\Tactician;
15
16
use Gears\CQRS\Exception\QueryReturnException;
17
use Gears\CQRS\Query;
18
use Gears\CQRS\QueryBus as QueryBusInterface;
19
use Gears\DTO\DTO;
20
use League\Tactician\CommandBus as TacticianCommandBus;
21
22
final class QueryBus implements QueryBusInterface
23
{
24
    /**
25
     * Wrapped command bus.
26
     *
27
     * @var TacticianCommandBus
28
     */
29
    private $wrappedCommandBus;
30
31
    /**
32
     * QueryBus constructor.
33
     *
34
     * @param TacticianCommandBus $wrappedCommandBus
35
     */
36
    public function __construct(TacticianCommandBus $wrappedCommandBus)
37
    {
38
        $this->wrappedCommandBus = $wrappedCommandBus;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     *
44
     * @throws QueryReturnException
45
     */
46
    public function handle(Query $query): DTO
47
    {
48
        $dto = $this->wrappedCommandBus->handle($query);
49
50
        if (!$dto instanceof DTO) {
51
            throw new QueryReturnException(\sprintf(
52
                'Query handler for "%s" should return an instance of "%s".',
53
                \get_class($query),
54
                DTO::class
55
            ));
56
        }
57
58
        return $dto;
59
    }
60
}
61