ExecutingSubscribeCommandQueue::publish()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.9666
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\Subscribe;
13
14
use GpsLab\Component\Command\Command;
15
16
class ExecutingSubscribeCommandQueue implements SubscribeCommandQueue
17
{
18
    /**
19
     * @var callable[]
20
     */
21
    private $handlers = [];
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
        // absence of a handlers is not a error
33 1
        foreach ($this->handlers as $handler) {
34 1
            $handler($command);
35
        }
36
37 1
        return true;
38
    }
39
40
    /**
41
     * Subscribe on command queue.
42
     *
43
     * @param callable $handler
44
     */
45 1
    public function subscribe(callable $handler): void
46
    {
47 1
        $this->handlers[] = $handler;
48 1
    }
49
50
    /**
51
     * Unsubscribe on command queue.
52
     *
53
     * @param callable $handler
54
     *
55
     * @return bool
56
     */
57 1
    public function unsubscribe(callable $handler): bool
58
    {
59 1
        $index = array_search($handler, $this->handlers);
60
61 1
        if (false === $index) {
62 1
            return false;
63
        }
64
65 1
        unset($this->handlers[$index]);
66
67 1
        return true;
68
    }
69
}
70