TransactionalCommandBus::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace RemiSan\TransactionManager\Command;
4
5
use League\Tactician\CommandBus;
6
use League\Tactician\Exception\InvalidCommandException;
7
use RemiSan\TransactionManager\Exception\BeginException;
8
use RemiSan\TransactionManager\Exception\CommitException;
9
use RemiSan\TransactionManager\Exception\NoRunningTransactionException;
10
use RemiSan\TransactionManager\Transactional;
11
12
class TransactionalCommandBus extends CommandBus implements Transactional
13
{
14
    /**
15
     * @var CommandBus
16
     */
17
    private $commandBus;
18
19
    /**
20
     * @var array
21
     */
22
    private $commandsToCommit;
23
24
    /**
25
     * @var boolean
26
     */
27
    private $transactionRunning;
28
29
    /**
30
     * TransactionalCommandBus constructor.
31
     *
32
     * @param CommandBus $commandBus
33
     */
34 27
    public function __construct(CommandBus $commandBus)
35
    {
36 27
        parent::__construct([]); // Build it to prevent warnings but never use it
37
38 27
        $this->commandBus = $commandBus;
39
40 27
        $this->reset();
41 27
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46 15
    public function handle($command)
47
    {
48 15
        if (!is_object($command)) {
49 3
            throw InvalidCommandException::forUnknownValue($command);
50
        }
51
52 12
        $this->checkTransactionIsRunning();
53
54 9
        $this->commandsToCommit[] = $command;
55
56 9
        return true;
57
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62 15
    public function beginTransaction()
63
    {
64 15
        if ($this->isTransactionRunning()) {
65 3
            throw new BeginException();
66
        }
67
68 15
        $this->set();
69 15
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74 9
    public function commit()
75
    {
76 9
        $this->checkTransactionIsRunning();
77
78 6
        foreach ($this->commandsToCommit as $command) {
79
            try {
80 6
                $this->commandBus->handle($command);
81 6
            } catch (\Exception $e) {
82 3
                throw new CommitException($e->getMessage(), $e->getCode(), $e);
83
            }
84 3
        }
85
86 3
        $this->reset();
87 3
    }
88
89
    /**
90
     * @inheritDoc
91
     */
92 6
    public function rollback()
93
    {
94 6
        $this->checkTransactionIsRunning();
95
96 3
        $this->reset();
97 3
    }
98
99
    /**
100
     * @return bool
101
     */
102 24
    private function isTransactionRunning()
103
    {
104 24
        return (boolean) $this->transactionRunning;
105
    }
106
107
    /**
108
     * @throws NoRunningTransactionException
109
     */
110 18
    private function checkTransactionIsRunning()
111
    {
112 18
        if (! $this->isTransactionRunning()) {
113 9
            throw new NoRunningTransactionException();
114
        }
115 9
    }
116
117 15
    private function set()
118
    {
119 15
        $this->commandsToCommit = [];
120 15
        $this->transactionRunning = true;
121 15
    }
122
123 27
    private function reset()
124
    {
125 27
        $this->commandsToCommit = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $commandsToCommit.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
126 27
        $this->transactionRunning = false;
127 27
    }
128
}
129