Completed
Pull Request — master (#276)
by Maksim
02:09
created

AMQPMessageIterator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 116
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A current() 0 4 1
A next() 0 20 2
A key() 0 4 1
A valid() 0 4 1
A 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 PhpAmqpLib\Channel\AMQPChannel;
17
use Sonata\NotificationBundle\Model\Message;
18
19
class AMQPMessageIterator implements MessageIteratorInterface
20
{
21
    /**
22
     * @deprecated since 3.2, will be removed in 4.x
23
     *
24
     * @var AMQPChannel
25
     */
26
    protected $channel;
27
28
    /**
29
     * @var mixed
30
     */
31
    protected $message;
32
33
    /**
34
     * @var AmqpMessage
35
     */
36
    protected $AMQMessage;
37
38
    /**
39
     * @deprecated since 3.2, will be removed in 4.x
40
     *
41
     * @var string
42
     */
43
    protected $queue;
44
45
    /**
46
     * @var int
47
     */
48
    protected $counter;
49
50
    /**
51
     * @var int
52
     */
53
    protected $timeout;
54
55
    /**
56
     * @var AmqpConsumer
57
     */
58
    protected $consumer;
59
60
    /**
61
     * @var bool
62
     */
63
    protected $isValid;
64
65
    /**
66
     * @param AMQPChannel $channel
67
     * @param AmqpConsumer $consumer
68
     */
69
    public function __construct(AMQPChannel $channel, AmqpConsumer $consumer)
70
    {
71
        $this->consumer = $consumer;
72
        $this->counter = 0;
73
        $this->timeout = 0;
74
        $this->isValid = true;
75
76
        $this->channel = $channel;
0 ignored issues
show
Deprecated Code introduced by
The property Sonata\NotificationBundl...ssageIterator::$channel has been deprecated with message: since 3.2, will be removed in 4.x

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
77
        $this->queue = $consumer->getQueue()->getQueueName();
0 ignored issues
show
Deprecated Code introduced by
The property Sonata\NotificationBundl...MessageIterator::$queue has been deprecated with message: since 3.2, will be removed in 4.x

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function current()
84
    {
85
        return $this->message;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function next()
92
    {
93
        if ($amqpMessage = $this->consumer->receive($this->timeout)) {
94
            $this->AMQMessage = $amqpMessage;
95
96
            $data = json_decode($this->AMQMessage->getBody(), true);
97
            $data['body']['AMQMessage'] = $amqpMessage;
98
99
            $message = new Message();
100
            $message->setBody($data['body']);
101
            $message->setType($data['type']);
102
            $message->setState($data['state']);
103
            $this->message = $message;
104
105
            ++$this->counter;
106
            $this->isValid = true;
107
        } else {
108
            $this->isValid = false;
109
        }
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function key()
116
    {
117
        $this->counter;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function valid()
124
    {
125
        return $this->isValid;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function rewind()
132
    {
133
    }
134
}
135