Connection::__construct()   A
last analyzed

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 getClientData(): string
103
    {
104
        return $this->clientData;
105
    }
106
107
    public function __toString(): string
108
    {
109
        return $this->getConnectionId();
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public function getConnectionId(): string
116
    {
117
        return $this->connectionId;
118
    }
119
120
    /**
121
     * Remove publishers based on streamId
122
     * @param  string  $streamId
123
     */
124
    public function unpublish(string $streamId)
125
    {
126
        $this->publishers = array_filter($this->publishers, function (Publisher $publisher) use ($streamId) {
127
            return $streamId !== $publisher->getStreamId();
128
        });
129
    }
130
131
    /**
132
     * Remove subscribers  based on publisher streamId
133
     * @param  string  $streamId
134
     */
135
    public function unsubscribe(string $streamId)
136
    {
137
        if (!empty($this->subscribers)) {
138
            $this->subscribers = array_filter($this->subscribers, function (Subscriber $subscriber) use ($streamId) {
139
                return $subscriber->getPublisherStreamId() !== $streamId;
140
            });
141
        }
142
    }
143
144
    /**
145
     * Convert the model instance to JSON.
146
     *
147
     * @param  int  $options
148
     * @return string
149
     *
150
     */
151
    public function toJson($options = 0): string
152
    {
153
        $json = json_encode($this->jsonSerialize(), $options);
154
        return $json;
155
    }
156
157
    /**
158
     * Specify data which should be serialized to JSON
159
     * @link https://php.net/manual/en/jsonserializable.jsonserialize.php
160
     * @return mixed data which can be serialized by <b>json_encode</b>,
161
     * which is a value of any type other than a resource.
162
     * @since 5.4.0
163
     */
164
    public function jsonSerialize()
165
    {
166
        return $this->toArray();
167
    }
168
169
    /**
170
     * Convert the model instance to an array.
171
     *
172
     * @return array
173
     */
174
    public function toArray(): array
175
    {
176
        $array = [
177
            'connectionId' => $this->connectionId,
178
            'createdAt' => $this->createdAt,
179
            'role' => $this->role,
180
            'token' => $this->token,
181
            'location' => $this->location,
182
            'platform' => $this->platform,
183
            'serverData' => $this->serverData,
184
            'clientData' => $this->clientData,
185
            'subscribers' => $this->subscribers
186
        ];
187
        foreach ($this->publishers as $publisher) {
188
            $array['publishers'][] = $publisher->toArray();
189
        }
190
191
192
        foreach ($array as $key => $value) {
193
            if (is_null($value) || $value == '') {
194
                unset($array[$key]);
195
            }
196
        }
197
        return $array;
198
    }
199
200
    /**
201
     * @param  string  $json
202
     * @return Connection
203
     */
204
    public function fromJson(string $json): Connection
205
    {
206
        return $this->fromArray(json_decode($json, true));
207
    }
208
209
    /**
210
     * @param  array  $connectionArray
211
     * @return Connection
212
     */
213
    public function fromArray(array $connectionArray): Connection
214
    {
215
        $this->connectionId = $connectionArray['connectionId'];
216
        $this->createdAt = $connectionArray['createdAt'] ?? null;
217
        $this->role = $connectionArray['role'] ?? null;
218
        $this->token = $connectionArray['token'] ?? null;
219
        $this->location = $connectionArray['location'] ?? null;
220
        $this->platform = $connectionArray['platform'] ?? null;
221
        $this->serverData = $connectionArray['serverData'] ?? null;
222
        $this->clientData = $connectionArray['clientData'] ?? null;
223
224
225
        if (array_key_exists('subscribers', $connectionArray)) {
226
            $this->subscribers = $connectionArray['subscribers'];
227
        }
228
        return $this;
229
    }
230
231
    /**
232
     * @return int
233
     */
234
    public function getCreatedAt(): int
235
    {
236
        return $this->createdAt;
237
    }
238
239
    /**
240
     * @return string
241
     */
242
    public function getRole(): string
243
    {
244
        return $this->role;
245
    }
246
247
    /**
248
     * @return string
249
     */
250
    public function getToken(): string
251
    {
252
        return $this->token;
253
    }
254
255
    /**
256
     * @return string
257
     */
258
    public function getLocation(): string
259
    {
260
        return $this->location;
261
    }
262
263
    /**
264
     * @return string
265
     */
266
    public function getPlatform(): string
267
    {
268
        return $this->platform;
269
    }
270
271
    /**
272
     * @return string
273
     */
274
    public function getServerData(): string
275
    {
276
        return $this->serverData;
277
    }
278
279
    /**
280
     * @return array
281
     */
282
    public function getSubscribers(): array
283
    {
284
        return $this->subscribers;
285
    }
286
287
    /**
288
     * @return array
289
     */
290
    public function getPublishers(): array
291
    {
292
        return $this->publishers;
293
    }
294
}
295