LimitQueueProcessor::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Queue;
5
6
use Genkgo\Mail\Exception\AbstractProtocolException;
7
use Genkgo\Mail\Exception\EmptyQueueException;
8
use Genkgo\Mail\TransportInterface;
9
10
final class LimitQueueProcessor implements QueueProcessorInterface
11
{
12
    /**
13
     * @var TransportInterface
14
     */
15
    private $transport;
16
17
    /**
18
     * @var iterable|QueueInterface[]
19
     */
20
    private $queue;
21
22
    /**
23
     * @var int
24
     */
25
    private $limit;
26
27
    /**
28
     * @param TransportInterface $transport
29
     * @param QueueInterface[] $queue
30
     */
31 5
    public function __construct(TransportInterface $transport, iterable $queue, int $limit)
32
    {
33 5
        $this->transport = $transport;
34 5
        $this->queue = $queue;
35 5
        $this->limit = $limit;
36 5
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 5
    public function process(): int
42
    {
43 5
        $count = 0;
44 5
        foreach ($this->queue as $queue) {
45
            try {
46 5
                while ($message = $queue->fetch()) {
47 5
                    if ($this->limit > 0 && $this->limit <= $count) {
48 1
                        $queue->store($message);
49
50 1
                        return $count;
51
                    }
52
                    try {
53 5
                        $this->transport->send($message);
54 2
                    } catch (AbstractProtocolException $e) {
55 2
                        $queue->store($message);
56
57 2
                        return $count;
58
                    }
59 3
                    ++$count;
60
                }
61 2
            } catch (EmptyQueueException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
62
            }
63
        }
64
65 2
        return $count;
66
    }
67
}
68