DispatcherAwareTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 6
c 5
b 1
f 0
lcom 1
cbo 0
dl 0
loc 52
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setDispatcher() 0 4 1
A getDispatcher() 0 4 1
A hasDispatcher() 0 6 2
A dispatch() 0 8 2
1
<?php
2
/**
3
 * This file is part of the bee4/events package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @copyright Bee4 2014
8
 * @author Stephane HULARD <[email protected]>
9
 * @package Bee4\Events
10
 */
11
12
namespace Bee4\Events;
13
14
/**
15
 * Trait implementation of the DispatcherAwareInterface
16
 * @package Bee4\Event
17
 */
18
trait DispatcherAwareTrait
19
{
20
    /**
21
     * The linked dispatcher instance
22
     * @var DispatcherInterface
23
     */
24
    private $dispatcher;
25
26
    /**
27
     * Dependency injection
28
     * @param DispatcherInterface $dispatcher
29
     */
30
    public function setDispatcher(DispatcherInterface $dispatcher)
31
    {
32
        $this->dispatcher = $dispatcher;
33
    }
34
35
    /**
36
     * Access to the current dispatcher
37
     * @return DispatcherInterface|null
38
     */
39
    final public function getDispatcher()
40
    {
41
        return $this->dispatcher;
42
    }
43
44
    /**
45
     * Check if a dispatcher has been injected or not
46
     * @return boolean
47
     */
48
    final public function hasDispatcher()
49
    {
50
        return
51
            $this->dispatcher !== null &&
52
            $this->dispatcher instanceof DispatcherInterface;
53
    }
54
55
    /**
56
     * Dispatch an event if the dispatcher is loaded
57
     * @param string $name event name to dispatch
58
     * @param EventInterface $event
59
     * @return boolean
60
     */
61
    final public function dispatch($name, EventInterface $event)
62
    {
63
        if ($this->hasDispatcher()) {
64
            $this->getDispatcher()->dispatch($name, $event);
65
            return true;
66
        }
67
        return false;
68
    }
69
}
70