Completed
Push — master ( de5c9f...2ef464 )
by Daniel
21s queued 11s
created

AbstractEventBulkListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
eloc 11
c 1
b 0
f 1
dl 0
loc 38
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handleBulk() 0 17 4
A handle() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jellyfish\Event;
6
7
use Jellyfish\Event\Exception\NotSupportedMethodException;
8
use Jellyfish\Event\Exception\NotSupportedTypeException;
9
use Throwable;
10
11
use function sprintf;
12
13
abstract class AbstractEventBulkListener extends AbstractEventListener implements EventBulkListenerInterface
14
{
15
    /**
16
     * @param \Jellyfish\Event\EventInterface[] $events
17
     *
18
     * @return \Jellyfish\Event\EventBulkListenerInterface
19
     *
20
     * @throws \Jellyfish\Event\Exception\NotSupportedTypeException
21
     */
22
    public function handleBulk(array $events): EventBulkListenerInterface
23
    {
24
        if ($this->getType() === self::TYPE_SYNC) {
25
            throw new NotSupportedTypeException(
26
                sprintf('Event listeners that extend from "%s" only support type "%s"', __CLASS__, self::TYPE_ASYNC)
27
            );
28
        }
29
30
        foreach ($events as $event) {
31
            try {
32
                $this->doHandle($event);
33
            } catch (Throwable $e) {
34
                $this->handleError($e, $event);
35
            }
36
        }
37
38
        return $this;
39
    }
40
41
    /**
42
     * @param \Jellyfish\Event\EventInterface $event
43
     * @return \Jellyfish\Event\EventListenerInterface
44
     *
45
     * @throws \Exception
46
     */
47
    public function handle(EventInterface $event): EventListenerInterface
48
    {
49
        throw new NotSupportedMethodException(
50
            sprintf('Method "handle" is not supported by "%s"', get_class($this))
51
        );
52
    }
53
}
54