Completed
Push — master ( 3633f5...fa8aa1 )
by Rémi
03:57
created

TransactionalQueuePublisher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
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 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace RemiSan\TransactionManager\Amqp;
4
5
use Burrow\QueuePublisher;
6
use RemiSan\TransactionManager\Transactional;
7
8
class TransactionalQueuePublisher implements QueuePublisher, Transactional
9
{
10
    /** @var QueuePublisher */
11
    private $publisher;
12
13
    /** @var array */
14
    private $messages;
15
16
    /**
17
     * Constructor.
18
     *
19
     * @param QueuePublisher $publisher
20
     */
21
    public function __construct(QueuePublisher $publisher)
22
    {
23
        $this->publisher = $publisher;
24
        $this->messages = array();
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function publish($data, $routingKey = "")
31
    {
32
        $this->messages[] = array(
33
            'data' => $data,
34
            'routingKey' => $routingKey
35
        );
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function beginTransaction()
42
    {
43
        $this->messages = array();
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function commit()
50
    {
51
        foreach ($this->messages as $message) {
52
            $this->publisher->publish($message['data'], $message['routingKey']);
53
        }
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function rollback()
60
    {
61
        $this->messages = array();
62
    }
63
}
64