ExecutingSubscribeEventQueue   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 22.22 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 0
dl 12
loc 54
ccs 13
cts 13
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A publish() 0 9 2
A subscribe() 0 4 1
A unsubscribe() 12 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Domain\Event\Queue\Subscribe;
12
13
use GpsLab\Domain\Event\Event;
14
15
class ExecutingSubscribeEventQueue implements SubscribeEventQueue
16
{
17
    /**
18
     * @var callable[]
19
     */
20
    private $handlers = [];
21
22
    /**
23
     * Publish event to queue.
24
     *
25
     * @param Event $event
26
     *
27
     * @return bool
28
     */
29 1
    public function publish(Event $event)
30
    {
31
        // absence of a handlers is not a error
32 1
        foreach ($this->handlers as $handler) {
33 1
            call_user_func($handler, $event);
34
        }
35
36 1
        return true;
37
    }
38
39
    /**
40
     * Subscribe on event queue.
41
     *
42
     * @param callable $handler
43
     */
44 1
    public function subscribe(callable $handler)
45
    {
46 1
        $this->handlers[] = $handler;
47 1
    }
48
49
    /**
50
     * Unsubscribe on event queue.
51
     *
52
     * @param callable $handler
53
     *
54
     * @return bool
55
     */
56 1 View Code Duplication
    public function unsubscribe(callable $handler)
57
    {
58 1
        $index = array_search($handler, $this->handlers);
59
60 1
        if ($index === false) {
61 1
            return false;
62
        }
63
64 1
        unset($this->handlers[$index]);
65
66 1
        return true;
67
    }
68
}
69