Completed
Pull Request — 2.0 (#75)
by Julien
02:03
created

Listener   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 77
c 1
b 1
f 0
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 - 2015 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 - 2015 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
     * @access public
39
     * @param  string $name
40
     */
41
    public function __construct($name)
42
    {
43
        $this->name = $name;
44
    }
45
46
    /**
47
     * getClientType
48
     *
49
     * @see ClientInterface
50
     */
51
    public function getClientType()
52
    {
53
        return 'listener';
54
    }
55
56
    /**
57
     * getClientIdentifier
58
     *
59
     * @see ClientInterface
60
     */
61
    public function getClientIdentifier()
62
    {
63
        return $this->name;
64
    }
65
66
    /**
67
     * attachAction
68
     *
69
     * Attach a new callback to the callback list.
70
     *
71
     * @access public
72
     * @param  callable $action
73
     * @throws  FoundationException if $action is not a callable.
74
     * @return Listener $this
75
     */
76
    public function attachAction(callable $action)
77
    {
78
        $this->actions[] = $action;
79
80
        return $this;
81
    }
82
83
    /**
84
     * notify
85
     *
86
     * Trigger actions. All actions are executed passing the following parameters:
87
     * string   $name    name of the event
88
     * array    $data    event's payload if any
89
     * Session  $session the current session
90
     *
91
     * @access public
92
     * @param  string $name
93
     * @param  array $data
94
     * @return Listener $this
95
     */
96
    public function notify($name, array $data)
97
    {
98
        foreach ($this->actions as $action) {
99
            call_user_func($action, $name, $data, $this->getSession());
100
        }
101
102
        return $this;
103
    }
104
}
105