Completed
Push — master ( 980655...c21cce )
by Grégoire
01:53
created

AMQPMessageIterator::convertToAmqpLibMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 1
eloc 11
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\NotificationBundle\Iterator;
13
14
use Interop\Amqp\AmqpConsumer;
15
use Sonata\NotificationBundle\Model\Message;
16
17
final class AMQPMessageIterator implements MessageIteratorInterface
18
{
19
    /**
20
     * @var mixed
21
     */
22
    private $message;
23
24
    /**
25
     * @var int
26
     */
27
    private $counter;
28
29
    /**
30
     * @var int
31
     */
32
    private $timeout;
33
34
    /**
35
     * @var AmqpConsumer
36
     */
37
    private $consumer;
38
39
    /**
40
     * @var bool
41
     */
42
    private $isValid;
43
44
    public function __construct(AmqpConsumer $consumer)
45
    {
46
        $this->consumer = $consumer;
47
        $this->counter = 0;
48
        $this->timeout = 0;
49
        $this->isValid = true;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function current()
56
    {
57
        return $this->message;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function next()
64
    {
65
        $this->isValid = false;
66
67
        if ($amqpMessage = $this->consumer->receive($this->timeout)) {
68
            $data = json_decode($amqpMessage->getBody(), true);
69
            $data['body']['interopMessage'] = $amqpMessage;
70
71
            $message = new Message();
72
            $message->setBody($data['body']);
73
            $message->setType($data['type']);
74
            $message->setState($data['state']);
75
            $this->message = $message;
76
77
            ++$this->counter;
78
            $this->isValid = true;
79
        }
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function key()
86
    {
87
        $this->counter;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function valid()
94
    {
95
        return $this->isValid;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function rewind()
102
    {
103
        $this->isValid = true;
104
        $this->next();
105
    }
106
}
107