AbstractEventListener   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
wmc 10
eloc 17
c 1
b 1
f 1
dl 0
loc 91
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getErrorHandlers() 0 3 1
A __construct() 0 3 1
A addErrorHandler() 0 5 1
A setErrorHandlers() 0 5 1
A handle() 0 9 2
A handleError() 0 11 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jellyfish\Event;
6
7
use Throwable;
8
9
use function count;
10
11
abstract class AbstractEventListener implements EventListenerInterface
12
{
13
    /**
14
     * @var \Jellyfish\Event\EventErrorHandlerInterface[]
15
     */
16
    protected $errorHandlers;
17
18
    /**
19
     * @param \Jellyfish\Event\EventErrorHandlerInterface[] $errorHandlers
20
     */
21
    public function __construct(array $errorHandlers = [])
22
    {
23
        $this->errorHandlers = $errorHandlers;
24
    }
25
26
    /**
27
     * @return \Jellyfish\Event\EventErrorHandlerInterface[]
28
     */
29
    public function getErrorHandlers(): array
30
    {
31
        return $this->errorHandlers;
32
    }
33
34
    /**
35
     * @param \Jellyfish\Event\EventErrorHandlerInterface[] $errorHandlers
36
     *
37
     * @return \Jellyfish\Event\EventListenerInterface
38
     */
39
    public function setErrorHandlers(array $errorHandlers): EventListenerInterface
40
    {
41
        $this->errorHandlers = $errorHandlers;
42
43
        return $this;
44
    }
45
46
    /**
47
     * @param \Jellyfish\Event\EventErrorHandlerInterface $errorHandler
48
     *
49
     * @return \Jellyfish\Event\EventListenerInterface
50
     */
51
    public function addErrorHandler(EventErrorHandlerInterface $errorHandler): EventListenerInterface
52
    {
53
        $this->errorHandlers[] = $errorHandler;
54
55
        return $this;
56
    }
57
58
    /**
59
     * @param \Jellyfish\Event\EventInterface $event
60
     *
61
     * @return \Jellyfish\Event\EventListenerInterface
62
     *
63
     * @throws \Throwable
64
     */
65
    public function handle(EventInterface $event): EventListenerInterface
66
    {
67
        try {
68
            $this->doHandle($event);
69
        } catch (Throwable $error) {
70
            $this->handleError($error, $event);
71
        }
72
73
        return $this;
74
    }
75
76
    /**
77
     * @param \Jellyfish\Event\EventInterface $event
78
     *
79
     * @return \Jellyfish\Event\EventListenerInterface
80
     */
81
    abstract protected function doHandle(EventInterface $event): EventListenerInterface;
82
83
    /**
84
     * @param \Throwable $error
85
     * @param \Jellyfish\Event\EventInterface $event
86
     *
87
     * @return \Jellyfish\Event\EventListenerInterface
88
     *
89
     * @throws \Throwable
90
     */
91
    protected function handleError(Throwable $error, EventInterface $event): EventListenerInterface
92
    {
93
        if ($this->errorHandlers === null || count($this->errorHandlers) === 0) {
94
            throw $error;
95
        }
96
97
        foreach ($this->errorHandlers as $errorHandler) {
98
            $errorHandler->handle($error, $this->getIdentifier(), $event);
99
        }
100
101
        return $this;
102
    }
103
}
104