AggregateEventsRaiseInSelfTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 63
c 0
b 0
f 0
ccs 20
cts 20
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A raiseInSelf() 0 9 2
A raise() 0 5 1
A pullEvents() 0 7 1
A eventHandlerName() 0 13 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\Aggregator;
11
12
use GpsLab\Domain\Event\Event;
13
14
trait AggregateEventsRaiseInSelfTrait
15
{
16
    /**
17
     * @var Event[]
18
     */
19
    private $events = [];
20
21
    /**
22
     * @param Event $event
23
     */
24 2
    private function raiseInSelf(Event $event)
25
    {
26 2
        $method = $this->eventHandlerName($event);
27
28
        // if method is not exists is not a critical error
29 2
        if (method_exists($this, $method)) {
30 1
            $this->{$method}($event);
31
        }
32 2
    }
33
34
    /**
35
     * @param Event $event
36
     */
37 2
    protected function raise(Event $event)
38
    {
39 2
        $this->events[] = $event;
40 2
        $this->raiseInSelf($event);
41 2
    }
42
43
    /**
44
     * @return Event[]
45
     */
46 2
    public function pullEvents()
47
    {
48 2
        $events = $this->events;
49 2
        $this->events = [];
50
51 2
        return $events;
52
    }
53
54
    /**
55
     * Get handler method name from event.
56
     *
57
     * Override this method if you want to change algorithm to generate the handler method name.
58
     *
59
     * @param Event $event
60
     *
61
     * @return string
62
     */
63 2
    protected function eventHandlerName(Event $event)
64
    {
65 2
        $class = get_class($event);
66
67 2
        if ('Event' === substr($class, -5)) {
68 1
            $class = substr($class, 0, -5);
69
        }
70
71 2
        $class = str_replace('_', '\\', $class); // convert names for classes not in namespace
72 2
        $parts = explode('\\', $class);
73
74 2
        return 'on'.end($parts);
75
    }
76
}
77