AbstractEventHandler   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 8
c 1
b 0
f 0
dl 0
loc 34
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 12 2
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
                static::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