GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TransactionalCommandLockingDecorator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 66
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A execute() 0 17 2
A executeIgnoringLock() 0 4 1
A executeQueue() 0 6 2
1
<?php
2
3
namespace Chief\Decorators;
4
5
use Chief\Busses\SynchronousCommandBus;
6
use Chief\Command;
7
use Chief\CommandBus;
8
use Chief\Decorator;
9
10
/**
11
 * TransactionalCommandLockingDecorator treats commands as transactions. Meaning that any
12
 * subsequent Commands passed to the bus from inside the relevant CommandHandler
13
 * will not be executed until the initial command is completed.
14
 */
15
class TransactionalCommandLockingDecorator implements Decorator
16
{
17
    use InnerBusTrait;
18
19
    /**
20
     * Whether or not a Command is in progress and the bus is locked
21
     * @var bool
22
     */
23
    protected $locked = false;
24
25
    /**
26
     * Queued Commands to be executed when the current command finishes
27
     * @var array
28
     */
29
    protected $queue = [];
30
31
    public function __construct(CommandBus $innerCommandBus = null)
32
    {
33
        $this->setInnerBus($innerCommandBus ?: new SynchronousCommandBus());
34
    }
35
36
    /**
37
     * Execute a command
38
     *
39
     * @param Command $command
40
     * @return mixed
41
     */
42
    public function execute(Command $command)
43
    {
44
        if ($this->locked === true) {
45
            $this->queue[] = $command;
46
            return null;
47
        }
48
49
        $this->locked = true;
50
51
        $response = $this->executeIgnoringLock($command);
52
53
        $this->executeQueue();
54
55
        $this->locked = false;
56
57
        return $response;
58
    }
59
60
    /**
61
     * Execute a command, regardless of the lock
62
     *
63
     * @param Command $command
64
     * @return mixed
65
     */
66
    protected function executeIgnoringLock(Command $command)
67
    {
68
        return $this->innerCommandBus->execute($command);
69
    }
70
71
    /**
72
     * Execute all queued commands
73
     */
74
    protected function executeQueue()
75
    {
76
        foreach ($this->queue as $command) {
77
            $this->executeIgnoringLock($command);
78
        }
79
    }
80
}
81