Completed
Push — master ( 1fd03a...66da5b )
by Peter
03:41
created

MemoryUniqueEventQueue::pop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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