Completed
Pull Request — master (#41)
by Gawain
06:07
created

LimitQueueProcessor::process()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

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