ClientHolder::shutdown()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 4
nc 4
nop 0
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\Client;
11
12
use PommProject\Foundation\Exception\PommException;
13
14
/**
15
 * ClientHolder
16
 *
17
 * Session clients are stored in this holder.
18
 *
19
 * @package   Pomm
20
 * @copyright 2014 - 2017 Grégoire HUBERT
21
 * @author    Grégoire HUBERT
22
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
23
 */
24
class ClientHolder
25
{
26
    protected $clients = [];
27
28
    /**
29
     * add
30
     *
31
     * Add a new client or replace existing one.
32
     *
33
     * @param  ClientInterface $client
34
     * @return ClientHolder    $this
35
     */
36
    public function add(ClientInterface $client)
37
    {
38
        $this->clients[$client->getClientType()][$client->getClientIdentifier()] = $client;
39
40
        return $this;
41
    }
42
43
    /**
44
     * has
45
     *
46
     * Tell if a client is in the pool or not.
47
     *
48
     * @param  string $type
49
     * @param  string $name
50
     * @return bool
51
     */
52
    public function has($type, $name)
53
    {
54
        return (bool) isset($this->clients[$type][$name]);
55
    }
56
57
    /**
58
     * get
59
     *
60
     * Return a client by its name or null if no client exist for that name.
61
     *
62
     * @param  string          $type
63
     * @param  string          $name
64
     * @return ClientInterface
65
     */
66
    public function get($type, $name)
67
    {
68
        return isset($this->clients[$type][$name]) ? $this->clients[$type][$name] : null;
69
    }
70
71
    /**
72
     * getAllFor
73
     *
74
     * Return all clients for a given type.
75
     *
76
     * @param  string $type
77
     * @return array
78
     */
79
    public function getAllFor($type)
80
    {
81
        if (!isset($this->clients[$type])) {
82
            return [];
83
        }
84
85
        return $this->clients[$type];
86
    }
87
88
    /**
89
     * clear
90
     *
91
     * Call shutdown and remove a client from the pool. If the client does not
92
     * exist, nothing is done.
93
     *
94
     * @param  string       $type
95
     * @param  string       $name
96
     * @return ClientHolder $this
97
     */
98
    public function clear($type, $name)
99
    {
100
        if (isset($this->clients[$type][$name])) {
101
            $this->clients[$type][$name]->shutdown();
102
            unset($this->clients[$type][$name]);
103
        }
104
105
        return $this;
106
    }
107
108
    /**
109
     * shutdown
110
     *
111
     * Call shutdown for all registered clients and unset the clients so they
112
     * can be cleaned by GC. It would have been better by far to use a
113
     * RecursiveArrayIterator to do this but it is not possible in PHP using
114
     * built'in iterators hence the double foreach recursion.
115
     * see http://fr2.php.net/manual/en/class.recursivearrayiterator.php#106519
116
     *
117
     * @return array exceptions caught during the shutdown
118
     */
119
    public function shutdown()
120
    {
121
        $exceptions = [];
122
123
        foreach ($this->clients as $type => $names) {
124
            foreach ($names as $name => $client) {
125
                try {
126
                    $client->shutdown();
127
                } catch (PommException $e) {
128
                    $exceptions[] = $e;
129
                }
130
            }
131
        }
132
133
        $this->clients = [];
134
135
        return $exceptions;
136
    }
137
}
138