Completed
Push — master ( d056c4...9c2487 )
by Peter
02:40
created

AggregateEventsRaiseInSelfTrait::pullEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 1
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\EventInterface;
13
use GpsLab\Domain\Event\NameResolver\EventNameResolverInterface;
14
use GpsLab\Domain\Event\NameResolver\NameResolverContainer;
15
16
trait AggregateEventsRaiseInSelfTrait
17
{
18
    /**
19
     * @var EventInterface[]
20
     */
21
    private $events = [];
22
23
    /**
24
     * @deprecated It will be removed in 2.0
25
     *
26
     * @var EventNameResolverInterface
27
     */
28
    private $resolver;
29
30
    /**
31
     * @deprecated It will be removed in 2.0. If you want change the event name resolver, you must override getMethodNameFromEvent() method.
32
     * @see AggregateEventsRaiseInSelfTrait::getMethodNameFromEvent()
33
     *
34
     * @param EventNameResolverInterface $resolver
35
     */
36
    protected function changeEventNameResolver(EventNameResolverInterface $resolver)
37
    {
38
        trigger_error('It will be removed in 2.0. If you want change the event name resolver, you must override getMethodNameFromEvent() method.', E_USER_DEPRECATED);
39
40
        $this->resolver = $resolver;
0 ignored issues
show
Deprecated Code introduced by
The property GpsLab\Domain\Event\Aggr...eInSelfTrait::$resolver has been deprecated with message: It will be removed in 2.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
41
    }
42
43
    /**
44
     * @param EventInterface $event
45
     */
46 3 View Code Duplication
    private function raiseInSelf(EventInterface $event)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48 3
        $method = $this->getMethodNameFromEvent($event);
49
50
        // if method is not exists is not a critical error
51 3
        if (method_exists($this, $method)) {
52 2
            call_user_func([$this, $method], $event);
53 2
        }
54 3
    }
55
56
    /**
57
     * @param EventInterface $event
58
     */
59 3
    protected function raise(EventInterface $event)
60
    {
61 3
        $this->events[] = $event;
62 3
        $this->raiseInSelf($event);
63 3
    }
64
65
    /**
66
     * @return EventInterface[]
67
     */
68 2
    public function pullEvents()
69
    {
70 2
        $events = $this->events;
71 2
        $this->events = [];
72
73 2
        return $events;
74
    }
75
76
    /**
77
     * Get handler method name from event.
78
     *
79
     * Override this method if you want to change algorithm to generate the handler method name.
80
     *
81
     * @param EventInterface $event
82
     *
83
     * @return string
84
     */
85 3
    protected function getMethodNameFromEvent(EventInterface $event)
86
    {
87
        // BC: use custom event name resolver if exists
88 3
        if ($this->resolver instanceof EventNameResolverInterface) {
0 ignored issues
show
Deprecated Code introduced by
The property GpsLab\Domain\Event\Aggr...eInSelfTrait::$resolver has been deprecated with message: It will be removed in 2.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
89 1
            return 'on'.$this->resolver->getEventName($event);
0 ignored issues
show
Deprecated Code introduced by
The property GpsLab\Domain\Event\Aggr...eInSelfTrait::$resolver has been deprecated with message: It will be removed in 2.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
90
        }
91
92 2
        return 'on'.NameResolverContainer::getResolver()->getEventName($event);
93
    }
94
}
95