AMQPMessageIterator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A current() 0 4 1
A next() 0 18 2
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 5 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