Completed
Push — master ( f61059...fbff81 )
by Peter
05:40
created

MemoryUniqueCommandQueue::push()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Component\Command\Queue\PullPush;
12
13
use GpsLab\Component\Command\Command;
14
15
class MemoryUniqueCommandQueue implements CommandQueue
16
{
17
    /**
18
     * @var Command[]
19
     */
20
    private $commands = [];
21
22
    /**
23
     * Push command to queue.
24
     *
25
     * @param Command $command
26
     *
27
     * @return bool
28
     */
29 1
    public function push(Command $command)
30
    {
31 1
        $index = array_search($command, $this->commands);
32
33
        // remove exists command and push it again
34 1
        if ($index !== false) {
35 1
            unset($this->commands[$index]);
36 1
        }
37
38 1
        $this->commands[] = $command;
39
40 1
        return true;
41
    }
42
43
    /**
44
     * Pop command from queue. Return NULL if queue is empty.
45
     *
46
     * @return Command|null
47
     */
48 1
    public function pull()
49
    {
50 1
        return array_shift($this->commands);
51
    }
52
}
53