MemoryUniquePullCommandQueue   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A publish() 0 13 2
A pull() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * GpsLab component.
6
 *
7
 * @author    Peter Gribanov <[email protected]>
8
 * @copyright Copyright (c) 2011, Peter Gribanov
9
 * @license   http://opensource.org/licenses/MIT
10
 */
11
12
namespace GpsLab\Component\Command\Queue\Pull;
13
14
use GpsLab\Component\Command\Command;
15
16
class MemoryUniquePullCommandQueue implements PullCommandQueue
17
{
18
    /**
19
     * @var Command[]
20
     */
21
    private $commands = [];
22
23
    /**
24
     * Publish command to queue.
25
     *
26
     * @param Command $command
27
     *
28
     * @return bool
29
     */
30 1
    public function publish(Command $command): bool
31
    {
32 1
        $index = array_search($command, $this->commands);
33
34
        // remove exists command and publish it again
35 1
        if (false !== $index) {
36 1
            unset($this->commands[$index]);
37
        }
38
39 1
        $this->commands[] = $command;
40
41 1
        return true;
42
    }
43
44
    /**
45
     * Pop command from queue. Return NULL if queue is empty.
46
     *
47
     * @return Command|null
48
     */
49 1
    public function pull(): ?Command
50
    {
51 1
        return array_shift($this->commands);
52
    }
53
}
54