Completed
Push — master ( 8eb315...5aaba6 )
by Changwan
04:29
created

ArrayAdapter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 88%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 65
ccs 22
cts 25
cp 0.88
rs 10
wmc 10
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A flush() 0 4 1
A count() 0 4 1
A send() 0 8 1
A receive() 0 9 3
A remove() 0 10 3
1
<?php
2
namespace Wandu\Q\Adapter;
3
4
use Wandu\Q\Contracts\Adapter;
5
use Countable;
6
7
class ArrayAdapter implements Adapter, Countable
8
{
9
    /** @var array */
10
    protected $queue;
11
12 4
    public function __construct()
13
    {
14 4
        $this->queue = [];
15 4
    }
16
17
    /**
18
     * {@inheritdoc}
19
     */
20 3
    public function flush()
21
    {
22 3
        $this->queue = [];
23 3
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function count()
29
    {
30
        return count($this->queue);
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 4
    public function send(string $payload)
37
    {
38 4
        $this->queue[] = [
39 4
            'id' => uniqid(),
40 4
            'payload' => $payload,
41
            'reserved' => false,
42
        ];
43 4
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 4
    public function receive()
49
    {
50 4
        foreach ($this->queue as $idx => $item) {
51 3
            if (!$item['reserved']) {
52 3
                $this->queue[$idx]['reserved'] = true;
53 3
                return new ArrayJob($item['id'], $item['payload']);
54
            }
55
        }
56 2
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 1
    public function remove($job)
62
    {
63 1
        foreach ($this->queue as $idx => $item) {
64
            /** @var \Wandu\Q\Adapter\ArrayJob $job */
65 1
            if ($item['id'] === $job->getIdentifier()) {
66 1
                array_splice($this->queue, $idx, 1);
67 1
                return;
68
            }
69
        }
70
    }
71
}
72