Completed
Push — master ( 21e214...2bd91c )
by Irfaq
02:34
created

EmitsEvents::emitEvent()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 6.9849

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 14
loc 14
ccs 3
cts 7
cp 0.4286
rs 9.2
cc 4
eloc 7
nc 3
nop 1
crap 6.9849
1
<?php
2
3
namespace Telegram\Bot\Events;
4
5
use League\Event\Emitter;
6
use League\Event\EventInterface;
7
8
trait EmitsEvents
9
{
10
    /**
11
     * @var Emitter
12
     */
13
    private $eventEmitter;
14
15
    /**
16
     * @param EventInterface|string $event
17
     * @throws \InvalidArgumentException
18
     * @return bool true if emitted, false otherwise
19
     */
20 6 View Code Duplication
    private function emitEvent($event)
0 ignored issues
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...
21
    {
22 6
        if (is_null($this->eventEmitter)) {
23 6
            return false;
24
        }
25
26
        if (!is_string($event) && !$event instanceof EventInterface) {
27
            throw new \InvalidArgumentException('Event must be either be of type "string" or instance of League\Event\EventInterface');
28
        }
29
30
        $this->eventEmitter->emit($event);
31
32
        return true;
33
    }
34
35
    /**
36
     * @param EventInterface[]|string[] $events
37
     * @throws \InvalidArgumentException
38
     * @return bool true if all emitted, false otherwise
39
     */
40 View Code Duplication
    private function emitBatchOfEvents(array $events)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
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...
41
    {
42
        if (is_null($this->eventEmitter)) {
43
            return false;
44
        }
45
46
        foreach ($events as $e) {
47
            if (!is_string($e) && !$e instanceof EventInterface) {
48
                throw new \InvalidArgumentException('Event must be either be of type "string" or instance of League\Event\EventInterface');
49
            }
50
        }
51
52
        $this->emitBatchOfEvents($events);
53
54
        return true;
55
    }
56
57
    /**
58
     * @return Emitter
59
     */
60
    public function getEventEmitter()
61
    {
62
        return $this->eventEmitter;
63
    }
64
65
    /**
66
     * @param Emitter $eventEmitter
67
     */
68
    public function setEventEmitter($eventEmitter)
69
    {
70
        $this->eventEmitter = $eventEmitter;
71
    }
72
}
73