Completed
Pull Request — 3.x (#375)
by
unknown
02:08
created

AMQPMessageIterator::convertToAmqpLibMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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