Completed
Pull Request — master (#41)
by Gawain
03:04
created

RedisQueue   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 69
ccs 19
cts 20
cp 0.95
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A store() 0 8 2
A fetch() 0 13 3
A count() 0 8 2
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
}