Passed
Push — master ( 1fd682...2023c3 )
by Jacobo
03:15 queued 40s
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 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 streamId
133
     * @param string $streamId
134
     */
135
    public function unsubscribe(string $streamId)
136
    {
137
        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...
138
            $this->subscribers = array_filter($this->subscribers, function ($subscriber) use ($streamId) {
139
                if (is_string($subscriber)) {
140
                    return $subscriber !== $streamId;
141
                }
142
                return $subscriber['streamId'] !== $streamId;
143
            });
144
        }
145
    }
146
147
    /**
148
     * Convert the model instance to JSON.
149
     *
150
     * @param int $options
151
     * @return string
152
     *
153
     */
154
    public function toJson($options = 0): string
155
    {
156
        $json = json_encode($this->jsonSerialize(), $options);
157
        return $json;
158
    }
159
160
    /**
161
     * Specify data which should be serialized to JSON
162
     * @link https://php.net/manual/en/jsonserializable.jsonserialize.php
163
     * @return mixed data which can be serialized by <b>json_encode</b>,
164
     * which is a value of any type other than a resource.
165
     * @since 5.4.0
166
     */
167
    public function jsonSerialize()
168
    {
169
        return $this->toArray();
170
    }
171
172
    /**
173
     * Convert the model instance to an array.
174
     *
175
     * @return array
176
     */
177
    public function toArray(): array
178
    {
179
        $array = ['connectionId' => $this->connectionId,
180
            'createdAt' => $this->createdAt,
181
            'role' => $this->role,
182
            'token' => $this->token,
183
            'location' => $this->location,
184
            'platform' => $this->platform,
185
            'serverData' => $this->serverData,
186
            'clientData' => $this->clientData,
187
            'subscribers' => $this->subscribers];
188
        foreach ($this->publishers as $publisher) {
189
            $array['publishers'][] = $publisher->toArray();
190
        }
191
192
193
        foreach ($array as $key => $value) {
194
            if (is_null($value) || $value == '')
195
                unset($array[$key]);
196
        }
197
        return $array;
198
    }
199
200
    /**
201
     * @return int
202
     */
203
    public function getCreatedAt(): int
204
    {
205
        return $this->createdAt;
206
    }
207
208
    /**
209
     * @return string
210
     */
211
    public function getRole(): string
212
    {
213
        return $this->role;
214
    }
215
216
    /**
217
     * @return string
218
     */
219
    public function getToken(): string
220
    {
221
        return $this->token;
222
    }
223
224
    /**
225
     * @return string
226
     */
227
    public function getLocation(): string
228
    {
229
        return $this->location;
230
    }
231
232
    /**
233
     * @return string
234
     */
235
    public function getPlatform(): string
236
    {
237
        return $this->platform;
238
    }
239
240
    /**
241
     * @return string
242
     */
243
    public function getServerData(): string
244
    {
245
        return $this->serverData;
246
    }
247
248
    /**
249
     * @return array
250
     */
251
    public function getSubscribers(): array
252
    {
253
        return $this->subscribers;
254
    }
255
256
    /**
257
     * @return array
258
     */
259
    public function getPublishers(): array
260
    {
261
        return $this->publishers;
262
    }
263
}
264