Completed
Pull Request — master (#276)
by Maksim
04:40
created

AMQPMessageIterator::receiveMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A AMQPMessageIterator::rewind() 0 3 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 Interop\Amqp\AmqpMessage;
16
use Sonata\NotificationBundle\Model\Message;
17
18
class AMQPMessageIterator implements MessageIteratorInterface
19
{
20
    /**
21
     * @var mixed
22
     */
23
    protected $message;
24
25
    /**
26
     * @var AmqpMessage
27
     */
28
    protected $AMQMessage;
29
30
    /**
31
     * @var int
32
     */
33
    protected $counter;
34
35
    /**
36
     * @var int
37
     */
38
    protected $timeout;
39
40
    /**
41
     * @var AmqpConsumer
42
     */
43
    protected $consumer;
44
45
    /**
46
     * @var bool
47
     */
48
    protected $isValid;
49
50
    /**
51
     * @param AmqpConsumer $consumer
52
     */
53
    public function __construct(AmqpConsumer $consumer)
54
    {
55
        $this->consumer = $consumer;
56
        $this->counter = 0;
57
        $this->timeout = 0;
58
        $this->isValid = true;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function current()
65
    {
66
        return $this->message;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function next()
73
    {
74
        if ($amqpMessage = $this->consumer->receive($this->timeout)) {
75
            $this->AMQMessage = $amqpMessage;
76
77
            $data = json_decode($this->AMQMessage->getBody(), true);
78
            $data['body']['AMQMessage'] = $amqpMessage;
79
80
            $message = new Message();
81
            $message->setBody($data['body']);
82
            $message->setType($data['type']);
83
            $message->setState($data['state']);
84
            $this->message = $message;
85
86
            ++$this->counter;
87
            $this->isValid = true;
88
        } else {
89
            $this->isValid = false;
90
        }
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function key()
97
    {
98
        $this->counter;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function valid()
105
    {
106
        return $this->isValid;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function rewind()
113
    {
114
    }
115
}
116