AsyncCommandBus   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 58
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A handle() 0 13 4
1
<?php
2
3
/*
4
 * cqrs-async (https://github.com/phpgears/cqrs-async).
5
 * Async decorator for CQRS command bus.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/cqrs-async
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\CQRS\Async;
15
16
use Gears\CQRS\Async\Discriminator\CommandDiscriminator;
17
use Gears\CQRS\Command;
18
use Gears\CQRS\CommandBus;
19
20
class AsyncCommandBus implements CommandBus
21
{
22
    /**
23
     * Wrapped command bus.
24
     *
25
     * @var CommandBus
26
     */
27
    private $wrappedCommandBus;
28
29
    /**
30
     * Command queue.
31
     *
32
     * @var CommandQueue
33
     */
34
    private $queue;
35
36
    /**
37
     * Command discriminator.
38
     *
39
     * @var CommandDiscriminator
40
     */
41
    private $discriminator;
42
43
    /**
44
     * AsyncCommandBus constructor.
45
     *
46
     * @param CommandBus           $wrappedCommandBus
47
     * @param CommandQueue         $queue
48
     * @param CommandDiscriminator $discriminator
49
     */
50
    public function __construct(
51
        CommandBus $wrappedCommandBus,
52
        CommandQueue $queue,
53
        CommandDiscriminator $discriminator
54
    ) {
55
        $this->wrappedCommandBus = $wrappedCommandBus;
56
        $this->discriminator = $discriminator;
57
        $this->queue = $queue;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     *
63
     * @throws \Gears\CQRS\Async\Exception\CommandQueueException
64
     */
65
    final public function handle(Command $command): void
66
    {
67
        if (!$command instanceof ReceivedCommand && $this->discriminator->shouldEnqueue($command)) {
68
            $this->queue->send($command);
69
70
            return;
71
        }
72
73
        if ($command instanceof ReceivedCommand) {
74
            $command = $command->getOriginalCommand();
75
        }
76
77
        $this->wrappedCommandBus->handle($command);
78
    }
79
}
80