LazyEventDispatcher   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 68.75%

Importance

Changes 0
Metric Value
wmc 7
eloc 15
dl 0
loc 39
ccs 11
cts 16
cp 0.6875
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getResolver() 0 3 1
A callListeners() 0 14 5
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
25
/**
26
 * {@inheritdoc}
27
 *
28
 * @author Divine Niiquaye Ibok <[email protected]>
29
 */
30
class LazyEventDispatcher extends EventDispatcher
31
{
32
    /** @var InvokerInterface */
33
    private $resolver;
34
35
    /**
36
     * @param null|InvokerInterface $invoker
37
     */
38 4
    public function __construct(?InvokerInterface $invoker = null)
39
    {
40 4
        parent::__construct();
41 4
        $this->resolver = $invoker ?? new Invoker();
42 4
    }
43
44
    /**
45
     * @return InvokerInterface
46
     */
47 3
    public function getResolver(): InvokerInterface
48
    {
49 3
        return $this->resolver;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 3
    protected function callListeners(iterable $listeners, string $eventName, object $event): void
56
    {
57 3
        foreach ($listeners as $listener) {
58 3
            if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
59 1
                break;
60
            }
61
62
            try {
63 2
                $listener($event, $eventName, $this);
64
            } catch (\TypeError $error) {
65
                $this->resolver->call($listener, [
66
                    \get_class($event)  => $event,
67
                    'eventName'         => $eventName,
68
                    __CLASS__           => $this,
69
                ]);
70
            }
71
        }
72 3
    }
73
}
74