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

RedisQueue::fetch()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 0
crap 3.0261
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Queue;
5
6
use Genkgo\Mail\Exception\EmptyQueueException;
7
use Genkgo\Mail\Exception\QueueStoreException;
8
use Genkgo\Mail\GenericMessage;
9
use Genkgo\Mail\MessageInterface;
10
use Predis\ClientInterface;
11
use Predis\Connection\ConnectionException;
12
13
final class RedisQueue implements QueueInterface, \Countable
14
{
15
16
    /**
17
     * @var ClientInterface
18
     */
19
    private $client;
20
    /**
21
     * @var string
22
     */
23
    private $key;
24
25
    /**
26
     * RedisQueue constructor.
27
     * @param ClientInterface $client
28
     * @param string $key
29
     */
30 7
    public function __construct(ClientInterface $client, string $key)
31
    {
32 7
        $this->client = $client;
33 7
        $this->key = $key;
34 7
    }
35
36
    /**
37
     * @param MessageInterface $message
38
     * @throws QueueStoreException
39
     */
40 4
    public function store(MessageInterface $message): void
41
    {
42
        try {
43 4
            $this->client->rpush($this->key, (string)$message);
44 1
        } catch (ConnectionException $e) {
0 ignored issues
show
Bug introduced by
The class Predis\Connection\ConnectionException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
45 1
            throw new QueueStoreException('Cannot add message to redis queue: ' . $e->getMessage());
46
        }
47 3
    }
48
49
    /**
50
     * @return MessageInterface
51
     * @throws EmptyQueueException
52
     * @throws QueueStoreException
53
     */
54 3
    public function fetch(): MessageInterface
55
    {
56
        try {
57 3
            $message = $this->client->lpop($this->key);
58 2
            if ($message) {
59 2
                return GenericMessage::fromString($message);
60
            }
61
62
            throw new EmptyQueueException();
63 2
        } catch (ConnectionException $e) {
0 ignored issues
show
Bug introduced by
The class Predis\Connection\ConnectionException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
64 1
            throw new QueueStoreException('Cannot add message to redis queue ' . $e->getMessage());
65
        }
66
    }
67
68
69
    /**
70
     * @return int
71
     * @throws QueueStoreException
72
     */
73 2
    public function count(): int
74
    {
75
        try {
76 2
            return $this->client->llen($this->key);
77 1
        } catch (ConnectionException $e) {
0 ignored issues
show
Bug introduced by
The class Predis\Connection\ConnectionException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
78 1
            throw new QueueStoreException('Cannot get messages from redis queue ' . $e->getMessage());
79
        }
80
    }
81
}