Passed
Push — master ( 10a085...dcdb8a )
by Divine Niiquaye
02:24
created

LazyEventDispatcher::callListeners()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 8.125

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 14
ccs 5
cts 10
cp 0.5
rs 9.6111
c 0
b 0
f 0
cc 5
nc 4
nop 3
crap 8.125
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Biurad opensource projects.
7
 *
8
 * PHP version 7.2 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Biurad\Events;
19
20
use DivineNii\Invoker\Interfaces\InvokerInterface;
21
use DivineNii\Invoker\Invoker;
22
use Psr\EventDispatcher\StoppableEventInterface;
23
use Symfony\Component\EventDispatcher\EventDispatcher;
24
use TypeError;
25
26
/**
27
 * {@inheritdoc}
28
 *
29
 * @author Divine Niiquaye Ibok <[email protected]>
30
 */
31
class LazyEventDispatcher extends EventDispatcher
32
{
33
    /** @var InvokerInterface */
34
    private $resolver;
35
36
    /**
37
     * @param null|InvokerInterface $invoker
38
     */
39 4
    public function __construct(?InvokerInterface $invoker = null)
40
    {
41 4
        parent::__construct();
42 4
        $this->resolver = $invoker ?? new Invoker();
43 4
    }
44
45
    /**
46
     * @return InvokerInterface
47
     */
48 3
    public function getResolver(): InvokerInterface
49
    {
50 3
        return $this->resolver;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 3
    protected function callListeners(iterable $listeners, string $eventName, object $event): void
57
    {
58 3
        foreach ($listeners as $listener) {
59 3
            if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
60 1
                break;
61
            }
62
63
            try {
64 2
                $listener($event, $eventName, $this);
65
            } catch (TypeError $error) {
66
                $this->resolver->call($listener, [
67
                    \get_class($event)  => $event,
68
                    'eventName'         => $eventName,
69
                    __CLASS__           => $this,
70
                ]);
71
            }
72
        }
73 3
    }
74
}
75