Completed
Push — master ( d37ad3...5c38af )
by Markus
15s queued 11s
created

AbstractEventListener::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
eloc 5
c 1
b 1
f 1
dl 0
loc 9
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Jellyfish\Event;
4
5
use Closure;
6
use Exception;
7
8
abstract class AbstractEventListener implements EventListenerInterface
9
{
10
    /**
11
     * @var \Closure|null
12
     */
13
    protected $errorHandler;
14
15
    /**
16
     * @param \Closure|null $errorHandler
17
     *
18
     * @return \Jellyfish\Event\EventListenerInterface
19
     */
20
    public function setErrorHandler(?Closure $errorHandler): EventListenerInterface
21
    {
22
        $this->errorHandler = $errorHandler;
23
24
        return $this;
25
    }
26
27
    /**
28
     * @param \Jellyfish\Event\EventInterface $event
29
     * @return \Jellyfish\Event\EventListenerInterface
30
     */
31
    public function handle(EventInterface $event): EventListenerInterface
32
    {
33
        try {
34
            $this->doHandle($event);
35
        } catch (Exception $e) {
36
            $this->handleError($e, $event);
37
        }
38
39
        return $this;
40
    }
41
42
    /**
43
     * @param \Jellyfish\Event\EventInterface $event
44
     *
45
     * @return \Jellyfish\Event\EventListenerInterface
46
     */
47
    abstract protected function doHandle(EventInterface $event): EventListenerInterface;
48
49
    /**
50
     * @param \Exception $e
51
     * @param \Jellyfish\Event\EventInterface $event
52
     *
53
     * @return \Jellyfish\Event\EventListenerInterface
54
     */
55
    protected function handleError(Exception $e, EventInterface $event): EventListenerInterface
56
    {
57
        if ($this->errorHandler === null) {
58
            return $this;
59
        }
60
61
        $this->errorHandler->call($this, $e, $event);
62
63
        return $this;
64
    }
65
}
66