Completed
Push — master ( 696320...2ffb28 )
by Julián
05:01
created

AbstractEventHandler::isEventSupported()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
/*
4
 * event (https://github.com/phpgears/event).
5
 * Event handling.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/event
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\Event;
15
16
use Gears\Event\Exception\InvalidEventException;
17
18
abstract class AbstractEventHandler implements EventHandler
19
{
20
    /**
21
     * {@inheritdoc}
22
     *
23
     * @throws InvalidEventException
24
     */
25
    final public function handle(Event $event): void
26
    {
27
        if (!\in_array($event->getEventType(), $this->getSupportedEventTypes(), true)) {
28
            throw new InvalidEventException(\sprintf(
29
                'Event handler "%s" can only handle events of: "%s", "%s" given',
30
                self::class,
31
                \implode('"or "', $this->getSupportedEventTypes()),
32
                \get_class($event)
33
            ));
34
        }
35
36
        $this->handleEvent($event);
37
    }
38
39
    /**
40
     * Get supported event type.
41
     *
42
     * @return string[]
43
     */
44
    abstract protected function getSupportedEventTypes(): array;
45
46
    /**
47
     * Handle event.
48
     *
49
     * @param Event $event
50
     */
51
    abstract protected function handleEvent(Event $event): void;
52
}
53