Completed
Push — master ( 10bc89...0b2a25 )
by Iqbal
22:43
created

CommandBus::commit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
/*
3
 * This file is part of the Borobudur-Cqrs package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Cqrs\Bus;
12
13
use Borobudur\Bus\Bus;
14
use Borobudur\Bus\Message\MessageInterface;
15
use Borobudur\Cqrs\Exception\InvalidArgumentException;
16
use Borobudur\Cqrs\Message\CommandInterface;
17
use Borobudur\Cqrs\ReadModel\Storage\TransactionalInterface;
18
use Borobudur\Queue\JobInterface;
19
use Borobudur\Queue\Queue;
20
use Borobudur\Queue\QueueMessage;
21
use Exception;
22
23
/**
24
 * @author      Iqbal Maulana <[email protected]>
25
 * @created     8/18/15
26
 */
27
class CommandBus extends Bus
28
{
29
    const BUS_NAME = 'bus.command';
30
31
    /**
32
     * @var TransactionalInterface
33
     */
34
    protected $transaction;
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param TransactionalInterface|null $transaction
40
     */
41
    public function __construct(TransactionalInterface $transaction = null)
42
    {
43
        $this->transaction = $transaction;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getName()
50
    {
51
        return CommandBus::BUS_NAME;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function dispatch(MessageInterface $command)
58
    {
59
        if (!$command instanceof CommandInterface) {
60
            throw new InvalidArgumentException(sprintf(
61
                'Command "%s" should implement \Borobudur\Cqrs\Message\CommandInterface',
62
                get_class($command)
63
            ));
64
        }
65
66
        if ($command instanceof JobInterface) {
67
            Queue::getInstance()->send(new QueueMessage($command));
68
            return null;
69
        }
70
71
        return $this->dispatchNow($command);
72
    }
73
74
    /**
75
     * Dispatch command immediately.
76
     *
77
     * @param MessageInterface $command
78
     *
79
     * @return mixed
80
     * @throws Exception
81
     */
82
    public function dispatchNow(MessageInterface $command)
83
    {
84
        $this->beginTransaction();
85
        try {
86
            $result = parent::dispatch($command);
87
            $this->commit();
88
89
            return $result;
90
        } catch (Exception $e) {
91
            $this->rollback();
92
            throw $e;
93
        }
94
    }
95
96
    /**
97
     * Prepare transaction if possible.
98
     */
99
    protected function beginTransaction()
100
    {
101
        if (null !== $this->transaction) {
102
            $this->transaction->beginTransaction();
103
        }
104
    }
105
106
    /**
107
     * Commit transaction if exist.
108
     */
109
    protected function commit()
110
    {
111
        if (null !== $this->transaction) {
112
            $this->transaction->commit();
113
        }
114
    }
115
116
    /**
117
     * Rollback transaction if exist.
118
     */
119
    protected function rollback()
120
    {
121
        if (null !== $this->transaction) {
122
            $this->transaction->rollback();
123
        }
124
    }
125
}
126