Passed
Push — master ( 77f1b4...a6165e )
by Jacobo
03:07
created

Connection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 10
dl 0
loc 12
rs 9.9332
c 1
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace SquareetLabs\LaravelOpenVidu;
4
5
use JsonSerializable;
6
use SquareetLabs\LaravelOpenVidu\Enums\OpenViduRole;
7
8
/**
9
 * Class Connection
10
 * @package SquareetLabs\LaravelOpenVidu
11
 */
12
class Connection implements JsonSerializable
13
{
14
15
    /** @var  string
16
     * Identifier of the connection. You can call [[Session.forceDisconnect]] passing this property as parameter
17
     */
18
    private $connectionId;
19
20
    /** @var int
21
     * Timestamp when this connection was established, in UTC milliseconds (ms since Jan 1, 1970, 00:00:00 UTC)
22
     */
23
    private $createdAt;
24
25
26
    /** @var  string
27
     * Role of the connection {@see OpenViduRole}
28
     */
29
    private $role;
30
31
32
    /** @var string
33
     * Token associated to the connection
34
     */
35
    private $token;
36
37
    /** @var string
38
     * <a href="/docs/openvidu-pro/" target="_blank" style="display: inline-block; background-color: rgb(0, 136, 170); color: white; font-weight: bold; padding: 0px 5px; margin-right: 5px; border-radius: 3px; font-size: 13px; line-height:21px; font-family: Montserrat, sans-serif">PRO</a>
39
     * Geo location of the connection, with the following format: `"CITY, COUNTRY"` (`"unknown"` if it wasn't possible to locate it)
40
     */
41
    private $location;
42
43
    /** @var string
44
     * A complete description of the platform used by the participant to connect to the session
45
     */
46
    private $platform;
47
48
    /** @var string
49
     * Data associated to the connection on the server-side. This value is set with {@uses TokenOptions::$data} when calling {@see Session::generateToken()}
50
     */
51
    private $serverData;
52
53
    /** @var string
54
     * Data associated to the connection on the client-side. This value is set with second parameter of method
55
     * {@method Session.connect}(/api/openvidu-browser/classes/session.html#connect) in OpenVidu Browser
56
     */
57
    private $clientData;
58
59
    /** @var array
60
     * Array of Publisher objects this particular Connection is publishing to the Session (each Publisher object has one Stream, uniquely
61
     * identified by its `streamId`). You can call {@uses Session::forceUnpublish} passing any of this values as parameter
62
     */
63
    private $publishers = [];
64
65
    /** @var array
66
     *  Array of streams (their `streamId` properties) this particular Connection is subscribed to. Each one always corresponds to one
67
     * Publisher of some other Connection: each string of this array must be equal to one [[Publisher.streamId]] of other Connection
68
     */
69
    private $subscribers = [];
70
71
    /**
72
     * Connection constructor.
73
     * @param string $connectionId
74
     * @param int $createdAt
75
     * @param string $role
76
     * @param string $token
77
     * @param string $location
78
     * @param string $platform
79
     * @param string $serverData
80
     * @param string $clientData
81
     * @param array $publishers
82
     * @param array $subscribers
83
     */
84
    public function __construct(string $connectionId, int $createdAt, string $role, string $token, string $location, string $platform, string $serverData, string $clientData, array $publishers, array $subscribers)
85
    {
86
        $this->connectionId = $connectionId;
87
        $this->createdAt = $createdAt;
88
        $this->role = $role;
89
        $this->token = $token;
90
        $this->location = $location;
91
        $this->platform = $platform;
92
        $this->serverData = $serverData;
93
        $this->clientData = $clientData;
94
        $this->publishers = $publishers;
95
        $this->subscribers = $subscribers;
96
97
    }
98
99
    /**
100
     * @return string
101
     */
102
    public function getRole(): string
103
    {
104
        return $this->role;
105
    }
106
107
    /**
108
     * @return int
109
     */
110
    public function getCreatedAt(): int
111
    {
112
        return $this->createdAt;
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    public function getToken(): string
119
    {
120
        return $this->token;
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function getLocation(): string
127
    {
128
        return $this->location;
129
    }
130
131
    /**
132
     * @return string
133
     */
134
    public function getPlatform(): string
135
    {
136
        return $this->platform;
137
    }
138
139
    /**
140
     * @return string
141
     */
142
    public function getServerData(): string
143
    {
144
        return $this->serverData;
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    public function getClientData(): string
151
    {
152
        return $this->clientData;
153
    }
154
155
    /**
156
     * @return array
157
     */
158
    public function getPublishers(): array
159
    {
160
        return $this->publishers;
161
    }
162
163
    /**
164
     * @return array
165
     */
166
    public function getSubscribers(): array
167
    {
168
        return $this->subscribers;
169
    }
170
171
    public function __toString(): string
172
    {
173
        return $this->getConnectionId();
174
    }
175
176
    /**
177
     * @return string
178
     */
179
    public function getConnectionId(): string
180
    {
181
        return $this->connectionId;
182
    }
183
184
    /**
185
     * Remove publishers based on streamId
186
     * @param string $streamId
187
     */
188
    public function unpublish(string $streamId)
189
    {
190
        $this->publishers = array_filter($this->publishers, function (Publisher $publisher) use ($streamId) {
191
            return $streamId !== $publisher->getStreamId();
192
        });
193
    }
194
195
    /**
196
     * Remove subscribers  based on streamId
197
     * @param string $streamId
198
     */
199
    public function unsubscribe(string $streamId)
200
    {
201
        if ($this->subscribers && count($this->subscribers) > 0) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->subscribers of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
202
            $this->subscribers = array_filter($this->subscribers, function ($subscriber) use ($streamId) {
203
                if (is_array($subscriber) && array_key_exists('streamId', $subscriber)) {
204
                    return $subscriber['streamId'] !== $streamId;
205
                } else {
206
207
                    return $subscriber !== $streamId;
208
                }
209
            });
210
        }
211
    }
212
213
214
    /**
215
     * Convert the model instance to JSON.
216
     *
217
     * @param int $options
218
     * @return string
219
     *
220
     */
221
    public function toJson($options = 0): string
222
    {
223
        $json = json_encode($this->jsonSerialize(), $options);
224
        return $json;
225
    }
226
227
    /**
228
     * Specify data which should be serialized to JSON
229
     * @link https://php.net/manual/en/jsonserializable.jsonserialize.php
230
     * @return mixed data which can be serialized by <b>json_encode</b>,
231
     * which is a value of any type other than a resource.
232
     * @since 5.4.0
233
     */
234
    public function jsonSerialize()
235
    {
236
        return $this->toArray();
237
    }
238
239
    /**
240
     * Convert the model instance to an array.
241
     *
242
     * @return array
243
     */
244
    public function toArray(): array
245
    {
246
        $array = ['connectionId' => $this->connectionId,
247
            'createdAt' => $this->createdAt,
248
            'role' => $this->role,
249
            'token' => $this->token,
250
            'location' => $this->location,
251
            'platform' => $this->platform,
252
            'serverData' => $this->serverData,
253
            'clientData' => $this->clientData,
254
            'subscribers' => $this->subscribers];
255
        foreach ($this->publishers as $publisher) {
256
            $array['publishers'][] = $publisher->toArray();
257
        }
258
259
260
        foreach ($array as $key => $value) {
261
            if (is_null($value) || $value == '')
262
                unset($array[$key]);
263
        }
264
        return $array;
265
    }
266
267
268
269
    /**
270
     * @param Connection $other
271
     * @return bool
272
     */
273
    public function equal(Connection $other): bool
274
    {
275
        $equals = (
276
            $this->connectionId === $other->getConnectionId() &&
277
            $this->createdAt === $other->getCreatedAt() &&
278
            $this->role === $other->getRole() &&
279
            $this->token === $other->getToken() &&
280
            $this->location === $other->getLocation() &&
281
            $this->platform === $other->getPlatform() &&
282
            $this->serverData === $other->getServerData() &&
283
            $this->clientData === $other->clientData &&
284
            count($this->subscribers) === count($other->getSubscribers()) &&
285
            count($this->publishers) === count($other->getPublishers()));
286
287
        if ($equals) {
288
            $equals = json_encode($this->subscribers) === json_encode($other->getSubscribers());
289
            if ($equals) {
290
                $i = 0;
291
                while ($equals && $i < count($this->getPublishers())) {
292
                    $equals = $this->publishers[$i]->equalTo($other->getPublishers()[$i]);
293
                    $i++;
294
                }
295
                return $equals;
296
            } else {
297
                return false;
298
            }
299
        } else {
300
            return false;
301
        }
302
    }
303
}
304