Passed
Push — master ( c9ca10...2ba15e )
by Insolita
01:32
created

SimpleCircularQueue   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 79
Duplicated Lines 11.39 %

Importance

Changes 0
Metric Value
wmc 11
dl 9
loc 79
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A purgeQueued() 0 3 1
A listQueued() 7 7 3
A countQueued() 0 3 1
A fill() 0 4 1
A __construct() 0 10 1
A queueKey() 0 3 1
A next() 0 8 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
 * Created by solly [30.10.17 22:05]
4
 */
5
6
namespace insolita\cqueue;
7
8
use function array_map;
9
use insolita\cqueue\Contracts\EmptyQueueBehaviorInterface;
10
use insolita\cqueue\Contracts\PayloadConverterInterface;
11
use insolita\cqueue\Contracts\QueueInterface;
12
use insolita\cqueue\Contracts\StorageInterface;
13
14
class SimpleCircularQueue implements QueueInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $name;
20
    
21
    /**
22
     * @var \insolita\cqueue\Contracts\PayloadConverterInterface
23
     */
24
    protected $converter;
25
    
26
    /**
27
     * @var \insolita\cqueue\Contracts\EmptyQueueBehaviorInterface
28
     */
29
    protected $emptyQueueBehavior;
30
    
31
    /**
32
     * @var \insolita\cqueue\Contracts\StorageInterface
33
     */
34
    protected $storage;
35
    
36
    public function __construct(
37
        string $name,
38
        PayloadConverterInterface $converter,
39
        EmptyQueueBehaviorInterface $emptyQueueBehavior,
40
        StorageInterface $redis
41
    ) {
42
        $this->name = $name;
43
        $this->converter = $converter;
44
        $this->emptyQueueBehavior = $emptyQueueBehavior;
45
        $this->storage = $redis;
46
    }
47
    
48
    public function getName(): string
49
    {
50
        return $this->name;
51
    }
52
    
53
    public function fill(array $data)
54
    {
55
        $identities = array_map([$this->converter, 'toIdentity'], $data);
56
        $this->storage->listPush($this->queueKey(), $identities);
57
    }
58
    
59
    public function purgeQueued()
60
    {
61
        $this->storage->delete($this->queueKey());
62
    }
63
    
64
    public function countQueued(): int
65
    {
66
        return $this->storage->listCount($this->queueKey());
67
    }
68
    
69 View Code Duplication
    public function listQueued($converted = false): 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
        $list = $this->storage->listItems($this->queueKey());
72
        if ($converted === false || empty($list)) {
73
            return $list;
74
        } else {
75
            return array_map([$this->converter, 'toPayload'], $list);
76
        }
77
    }
78
    
79
    public function next()
80
    {
81
        $item = $this->storage->listPop($this->queueKey());
82
        if (!$item) {
83
            return $this->emptyQueueBehavior->resolve($this);
84
        } else {
85
            $this->storage->listPush($this->queueKey(), [$item]);
86
            return $this->converter->toPayload($item);
87
        }
88
    }
89
    
90
    protected function queueKey(): string
91
    {
92
        return $this->getName() . ':Queue';
93
    }
94
}
95