Listener   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 74
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getClientType() 0 4 1
A getClientIdentifier() 0 4 1
A attachAction() 0 6 1
A notify() 0 8 2
1
<?php
2
/*
3
 * This file is part of the Pomm's Foundation package.
4
 *
5
 * (c) 2014 - 2017 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PommProject\Foundation\Listener;
11
12
use PommProject\Foundation\Exception\FoundationException;
13
use PommProject\Foundation\Client\ClientInterface;
14
use PommProject\Foundation\Client\Client;
15
16
/**
17
 * Listener
18
 *
19
 * Listener client.
20
 * This class may attach actions that are triggered by events.
21
 *
22
 * @package   Foundation
23
 * @copyright 2014 - 2017 Grégoire HUBERT
24
 * @author    Grégoire HUBERT
25
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
26
 * @see       Client
27
 */
28
class Listener extends Client
29
{
30
    protected $name;
31
    protected $actions = [];
32
33
    /**
34
     * __construct
35
     *
36
     * Take the client identifier as argument.
37
     *
38
     * @param  string $name
39
     */
40
    public function __construct($name)
41
    {
42
        $this->name = $name;
43
    }
44
45
    /**
46
     * getClientType
47
     *
48
     * @see ClientInterface
49
     */
50
    public function getClientType()
51
    {
52
        return 'listener';
53
    }
54
55
    /**
56
     * getClientIdentifier
57
     *
58
     * @see ClientInterface
59
     */
60
    public function getClientIdentifier()
61
    {
62
        return $this->name;
63
    }
64
65
    /**
66
     * attachAction
67
     *
68
     * Attach a new callback to the callback list.
69
     *
70
     * @param  callable $action
71
     * @throws  FoundationException if $action is not a callable.
72
     * @return Listener $this
73
     */
74
    public function attachAction(callable $action)
75
    {
76
        $this->actions[] = $action;
77
78
        return $this;
79
    }
80
81
    /**
82
     * notify
83
     *
84
     * Trigger actions. All actions are executed passing the following parameters:
85
     * string   $name    name of the event
86
     * array    $data    event's payload if any
87
     * Session  $session the current session
88
     *
89
     * @param  string $name
90
     * @param  array $data
91
     * @return Listener $this
92
     */
93
    public function notify($name, array $data)
94
    {
95
        foreach ($this->actions as $action) {
96
            call_user_func($action, $name, $data, $this->getSession());
97
        }
98
99
        return $this;
100
    }
101
}
102