MemoryUniquePullCommandQueue::publish()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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