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

AutoQueuingCommandBusAdapter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 34
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 11 3
A shouldBeQueued() 0 5 1
A getHash() 0 4 2
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