ListenerPooler   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 107
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPoolerType() 0 4 1
A createClient() 0 4 1
A notify() 0 20 4
A notifyAll() 0 10 2
A notifyClients() 0 20 4
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\Client\ClientPoolerInterface;
13
use PommProject\Foundation\Client\ClientPooler;
14
15
/**
16
 * ListenerPooler
17
 *
18
 * Pooler for listener clients.
19
 *
20
 * @package   Foundation
21
 * @copyright 2014 - 2017 Grégoire HUBERT
22
 * @author    Grégoire HUBERT
23
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
24
 * @see       ClientPooler
25
 */
26
class ListenerPooler extends ClientPooler
27
{
28
    /**
29
     * getPoolerType
30
     *
31
     * @see ClientPoolerInterface
32
     */
33
    public function getPoolerType()
34
    {
35
        return 'listener';
36
    }
37
38
39
    /**
40
     * createClient
41
     *
42
     * See @ClientPooler
43
     */
44
    protected function createClient($identifier)
45
    {
46
        return new Listener($identifier);
47
    }
48
49
    /**
50
     * notify
51
     *
52
     * Send a notification to clients.
53
     * Client identifiers may be a single client name, an array of client or
54
     * '*' to notify all clients.
55
     * Event name may use ':' to split indicate additional information (ie type
56
     * of payload). Events sent to 'pika', 'pika:chu' will both notify client
57
     * 'pika'.
58
     *
59
     * @param  string|array     $identifiers
60
     * @param  array            $data
61
     * @return ListenerPooler   $this
62
     */
63
    public function notify($identifiers, array $data)
64
    {
65
        $this->getSession()->hasLogger() &&
66
            $this->getSession()->getLogger()->debug(
67
                "Pomm: ListenerPooler: notification received.",
68
                [
69
                    'receivers' => $identifiers,
70
                ]
71
            );
72
73
        if (is_scalar($identifiers)) {
74
            if ($identifiers === '*') {
75
                return $this->notifyAll($data);
76
            }
77
78
            $identifiers = [ $identifiers ];
79
        }
80
81
        return $this->notifyClients($identifiers, $data);
82
    }
83
84
    /**
85
     * notifyAll
86
     *
87
     * Notify all existing clients.
88
     *
89
     * @param  array            $data
90
     * @return ListenerPooler   $this
91
     */
92
    protected function notifyAll(array $data)
93
    {
94
        foreach ($this
95
            ->getSession()
96
            ->getAllClientForType($this->getPoolerType()) as $client) {
97
            $client->notify('*', $data);
98
        }
99
100
        return $this;
101
    }
102
103
    /**
104
     * notifyClients
105
     *
106
     * Send a notification to the specified clients.
107
     *
108
     * @param  array            $identifiers
109
     * @param  array            $data
110
     * @return ListenerPooler   $this
111
     */
112
    protected function notifyClients(array $identifiers, array $data)
113
    {
114
        foreach ($identifiers as $identifier) {
115
            $client_name = strpos($identifier, ':') !== false
116
                ? substr($identifier, 0, strpos($identifier, ':'))
117
                : $identifier
118
                ;
119
120
            $client = $this
121
                ->getSession()
122
                ->getClient($this->getPoolerType(), $client_name)
123
                ;
124
125
            if ($client !== null) {
126
                $client->notify($identifier, $data);
127
            }
128
        }
129
130
        return $this;
131
    }
132
}
133