Dispatcher   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 73
Duplicated Lines 16.44 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 12
loc 73
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A dispatch() 0 10 4
A addProvider() 0 9 2
A getListeners() 12 12 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Phoole (PHP7.2+)
5
 *
6
 * @category  Library
7
 * @package   Phoole\Event
8
 * @copyright Copyright (c) 2019 Hong Zhang
9
 */
10
declare(strict_types=1);
11
12
namespace Phoole\Event;
13
14
use Phoole\Base\Queue\UniquePriorityQueue;
15
use Psr\EventDispatcher\StoppableEventInterface;
16
use Psr\EventDispatcher\EventDispatcherInterface;
17
18
/**
19
 * Dispatcher
20
 *
21
 * @package Phoole\Event
22
 */
23
class Dispatcher implements EventDispatcherInterface
24
{
25
    /**
26
     * @var Provider[]
27
     */
28
    protected $providers = [];
29
30
    /**
31
     * Dispatcher constructor.
32
     *
33
     * @param  Provider ...$providers
34
     */
35
    public function __construct(Provider ...$providers)
36
    {
37
        foreach ($providers as $p) {
38
            $this->addProvider($p);
39
        }
40
    }
41
42
    /**
43
     * Provide all relevant listeners with an event to process.
44
     *
45
     * @param  object $event
46
     *   The object to process.
47
     *
48
     * @return object
49
     *   The Event that was passed, now modified by listeners.
50
     */
51
    public function dispatch(object $event)
52
    {
53
        foreach ($this->getListeners($event) as $listener) {
54
            if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
55
                return $event;
56
            }
57
            $listener($event);
58
        }
59
        return $event;
60
    }
61
62
    /**
63
     * Add a provider to the dispatcher
64
     *
65
     * @param  Provider $provider
66
     * @return  void
67
     * @throws  \RuntimeException  if provider duplicated
68
     */
69
    protected function addProvider(Provider $provider)
70
    {
71
        $hash = \spl_object_hash($provider);
72
        if (!isset($this->providers[$hash])) {
73
            $this->providers[$hash] = $provider;
74
        } else {
75
            throw new \RuntimeException("Provider duplicated");
76
        }
77
    }
78
79
    /**
80
     * @param  object $event
81
     * @return iterable
82
     */
83 View Code Duplication
    protected function getListeners(object $event): iterable
84
    {
85
        $queue = new UniquePriorityQueue();
86
        foreach ($this->providers as $provider) {
87
            /** @var UniquePriorityQueue $q */
88
            $q = $provider->getListenersForEvent($event);
89
            if (count($q)) {
90
                $queue = $queue->combine($q);
91
            }
92
        }
93
        return $queue;
94
    }
95
}