Completed
Push — master ( bdf573...4512af )
by Dominik
02:52
created

RedisReceive   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 13.04 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 9
loc 69
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A receive() 0 19 3
A receiveAll() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Saxulum\MessageQueue\Redis;
4
5
use Predis\ClientInterface;
6
use Saxulum\MessageQueue\MessageInterface;
7
use Saxulum\MessageQueue\MessageReceiveException;
8
use Saxulum\MessageQueue\MessageReceiveInterface;
9
10
final class RedisReceive implements MessageReceiveInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $messageClass;
16
17
    /**
18
     * @var ClientInterface
19
     */
20
    private $redis;
21
22
    /**
23
     * @var string
24
     */
25
    private $list;
26
27
    /**
28
     * @param string          $messageClass
29
     * @param ClientInterface $redis
30
     * @param string          $list
31
     */
32
    public function __construct(string $messageClass, ClientInterface $redis, string $list)
33
    {
34
        $this->messageClass = $messageClass;
35
        $this->redis = $redis;
36
        $this->list = $list;
37
    }
38
39
    /**
40
     * @return MessageInterface|null
41
     *
42
     * @throws MessageReceiveException
43
     */
44
    public function receive()
45
    {
46
        try {
47
            if (null === $json = $this->redis->lpop($this->list)) {
48
                return null;
49
            }
50
51
            /** @var MessageInterface $messageClass */
52
            $messageClass = $this->messageClass;
53
54
            return $messageClass::fromJson($json);
55
        } catch (\Exception $e) {
56
            throw new MessageReceiveException(
57
                MessageReceiveException::MESSAGE_RECEIVE_FAILED.'('.$e->getMessage().')',
58
                MessageReceiveException::CODE_RECEIVE_FAILED,
59
                $e
60
            );
61
        }
62
    }
63
64
    /**
65
     * @return array
66
     *
67
     * @throws MessageReceiveException
68
     */
69 View Code Duplication
    public function receiveAll(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        $messages = [];
72
        while (null !== $message = $this->receive()) {
73
            $messages[] = $message;
74
        }
75
76
        return $messages;
77
    }
78
}
79