Passed
Branch master (a01aa4)
by Mike
03:32
created

AutoQueuingCommandBusAdapter::handle()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 4
nop 2
crap 12
1
<?php
2
3
namespace MGDigital\BusQue\AutoQueue;
4
5
use MGDigital\BusQue\CommandBusAdapterInterface;
6
7
class AutoQueuingCommandBusAdapter implements CommandBusAdapterInterface, DeciderInterface
8
{
9
10
    private $baseCommandBus;
11
    private $fromQueueCommands = [];
12
13
    public function __construct(CommandBusAdapterInterface $baseCommandBus)
14
    {
15
        $this->baseCommandBus = $baseCommandBus;
16
    }
17
18
    public function handle($command, bool $fromQueue = false)
19
    {
20
        if ($fromQueue) {
21
            $hash = $this->getHash($command);
22
            $this->fromQueueCommands[$hash] = true;
23
        }
24
        $this->baseCommandBus->handle($command, $fromQueue);
25
        if (isset($hash)) {
26
            unset($this->fromQueueCommands[$hash]);
27
        }
28
    }
29
30
    public function shouldBeQueued($command): bool
31
    {
32
        $hash = $this->getHash($command);
33
        return !isset($this->fromQueueCommands[$hash]);
34
    }
35
36
    private function getHash($command): string
37
    {
38
        return md5(is_object($command) ? spl_object_hash($command) : (string) $command);
39
    }
40
}
41