Completed
Push — develop ( 3cf270...2733c9 )
by Vladimir
02:42
created

PulseUser::_isCastable()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 8.8571
cc 5
eloc 3
nc 2
nop 1
crap 5
1
<?php
2
3
/**
4
 * This file contains the PulseUser class
5
 *
6
 * @copyright 2015 Vladimir Jimenez
7
 * @license   https://github.com/allejo/PhpPulse/blob/master/LICENSE.md MIT
8
 */
9
10
namespace allejo\DaPulse;
11
12
use allejo\DaPulse\Objects\ApiObject;
13
14
/**
15
 * The PulseUser class contains all of the functions related to accessing information about a user
16
 *
17
 * @package allejo\DaPulse
18
 * @since   0.1.0
19
 */
20
class PulseUser extends ApiObject
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    const API_PREFIX = "users";
26
27
    /**
28
     * The resource's URL.
29
     *
30
     * @var string
31
     */
32
    protected $url;
33
34
    /**
35
     * The user's unique identifier.
36
     *
37
     * @var int
38
     */
39
    protected $id;
40
41
    /**
42
     * The user's name.
43
     *
44
     * @var string
45
     */
46
    protected $name;
47
48
    /**
49
     * The user's email.
50
     *
51
     * @var string
52
     */
53
    protected $email;
54
55
    /**
56
     * The user's photo_url.
57
     *
58
     * @var string
59
     */
60
    protected $photo_url;
61
62
    /**
63
     * The user's title.
64
     *
65
     * @var string
66
     */
67
    protected $title;
68
69
    /**
70
     * The user's position.
71
     *
72
     * @var string
73
     */
74
    protected $position;
75
76
    /**
77
     * The user's phone.
78
     *
79
     * @var string
80
     */
81
    protected $phone;
82
83
    /**
84
     * The user's location.
85
     *
86
     * @var string
87
     */
88
    protected $location;
89
90
    /**
91
     * The user's status.
92
     *
93
     * @var string
94
     */
95
    protected $status;
96
97
    /**
98
     * The user's birthday.
99
     *
100
     * @var string
101
     */
102
    protected $birthday;
103
104
    /**
105
     * True if the user is guest, false otherwise
106
     *
107
     * @var bool
108
     */
109
    protected $is_guest;
110
111
    /**
112
     * The user's skills.
113
     *
114
     * @var string[]
115
     */
116
    protected $skills;
117
118
    /**
119
     * Creation time.
120
     *
121
     * @var \DateTime
122
     */
123
    protected $created_at;
124
125
    /**
126
     * Last update time.
127
     *
128
     * @var \DateTime
129
     */
130
    protected $updated_at;
131
132
    /**
133
     * The membership type of this user with respect to a specific Pulse or PulseBoard. This value will not be set if
134
     * a PulseUser object is created standalone and not by another object which supports subscribers or membership.
135
     *
136
     * @var mixed
137
     */
138
    protected $membership;
139
140
    /**
141
     * The URL pattern used for all calls
142
     *
143
     * @var string
144
     */
145
    private $urlSyntax = "%s/%s/%s.json";
146
147
    // ================================================================================================================
148
    //   Getter functions
149
    // ================================================================================================================
150
151
    /**
152
     * The resource's URL.
153
     *
154
     * @return string
155
     */
156
    public function getUrl ()
157
    {
158
        return $this->url;
159
    }
160
161
    /**
162
     * The user's unique identifier.
163
     *
164
     * @return int
165
     */
166 3
    public function getId ()
167
    {
168 3
        return $this->id;
169
    }
170
171
    /**
172
     * The user's name.
173
     *
174
     * @return string
175
     */
176
    public function getName ()
177
    {
178
        return $this->name;
179
    }
180
181
    /**
182
     * The user's email.
183
     *
184
     * @return string
185
     */
186
    public function getEmail ()
187
    {
188
        return $this->email;
189
    }
190
191
    /**
192
     * The user's photo_url.
193
     *
194
     * @return string
195
     */
196
    public function getPhotoUrl ()
197
    {
198
        return $this->photo_url;
199
    }
200
201
    /**
202
     * The user's title.
203
     *
204
     * @return string
205
     */
206
    public function getTitle ()
207
    {
208
        return $this->title;
209
    }
210
211
    /**
212
     * The user's position.
213
     *
214
     * @return string
215
     */
216
    public function getPosition ()
217
    {
218
        return $this->position;
219
    }
220
221
    /**
222
     * The user's phone.
223
     *
224
     * @return string
225
     */
226
    public function getPhone ()
227
    {
228
        return $this->phone;
229
    }
230
231
    /**
232
     * The user's location.
233
     *
234
     * @return string
235
     */
236
    public function getLocation ()
237
    {
238
        return $this->location;
239
    }
240
241
    /**
242
     * The user's status.
243
     *
244
     * @return string
245
     */
246
    public function getStatus ()
247
    {
248
        return $this->status;
249
    }
250
251
    /**
252
     * The user's birthday.
253
     *
254
     * @return string
255
     */
256
    public function getBirthday ()
257
    {
258
        return $this->birthday;
259
    }
260
261
    /**
262
     * True if the user is guest, false otherwise
263
     *
264
     * @return bool
265
     */
266
    public function getIsGuest ()
267
    {
268
        return $this->is_guest;
269
    }
270
271
    /**
272
     * The user's skills.
273
     *
274
     * @return string[]
275
     */
276
    public function getSkills ()
277
    {
278
        return $this->skills;
279
    }
280
281
    /**
282
     * Creation time.
283
     *
284
     * @return \DateTime
285
     */
286
    public function getCreatedAt ()
287
    {
288
        self::lazyCast($this->created_at, '\DateTime');
289
290
        return $this->created_at;
291
    }
292
293
    /**
294
     * Last update time.
295
     *
296
     * @return \DateTime
297
     */
298
    public function getUpdatedAt ()
299
    {
300
        self::lazyCast($this->updated_at, '\DateTime');
301
302
        return $this->updated_at;
303
    }
304
305
    /**
306
     * This function is now a placeholder until it can be removed since the DaPulse API no longer supports this
307
     * functionality
308
     *
309
     * @api
310
     *
311
     * @deprecated 0.2.0
312
     *
313
     * @since   0.1.0
314
     *
315
     * @returns string An empty string
316
     */
317
    public function getMembership ()
318
    {
319
        return "";
320
    }
321
322
    /**
323
     * Get the user's newsfeed
324
     *
325
     * @param  array $params GET parameters that need to be passed in the URL
326
     *
327
     * @since  0.1.0
328
     *
329
     * @return PulseUpdate[] An array of PulseUpdates that make up the user's newsfeed
330
     */
331
    public function getNewsFeed ($params = array())
332
    {
333
        $url = sprintf($this->urlSyntax, parent::apiEndpoint(), $this->id, "newsfeed");
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (apiEndpoint() instead of getNewsFeed()). Are you sure this is correct? If so, you might want to change this to $this->apiEndpoint().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
334
335
        return parent::fetchAndCastToObjectArray($url, "PulseUpdate", $params);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (fetchAndCastToObjectArray() instead of getNewsFeed()). Are you sure this is correct? If so, you might want to change this to $this->fetchAndCastToObjectArray().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
336
    }
337
338
    /**
339
     * Get the user's posts
340
     *
341
     * @param  array $params GET parameters that need to be passed in the URL
342
     *
343
     * @since  0.1.0
344
     *
345
     * @return PulseUpdate[] An array of PulseUpdates for each of the posts
346
     */
347
    public function getPosts ($params = array())
348
    {
349
        $url = sprintf($this->urlSyntax, parent::apiEndpoint(), $this->id, "posts");
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (apiEndpoint() instead of getPosts()). Are you sure this is correct? If so, you might want to change this to $this->apiEndpoint().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
350
351
        return parent::fetchAndCastToObjectArray($url, "PulseUpdate", $params);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (fetchAndCastToObjectArray() instead of getPosts()). Are you sure this is correct? If so, you might want to change this to $this->fetchAndCastToObjectArray().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
352
    }
353
354
    /**
355
     * Get the user's unread posts
356
     *
357
     * @param  array $params GET parameters that need to be passed in the URL
358
     *
359
     * @since  0.1.0
360
     *
361
     * @return PulseUpdate[] An array of PulseUpdates for each of the posts
362
     */
363
    public function getUnreadFeed ($params = array())
364
    {
365
        $url = sprintf($this->urlSyntax, parent::apiEndpoint(), $this->id, "unread_feed");
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (apiEndpoint() instead of getUnreadFeed()). Are you sure this is correct? If so, you might want to change this to $this->apiEndpoint().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
366
367
        return parent::fetchAndCastToObjectArray($url, "PulseUpdate", $params);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (fetchAndCastToObjectArray() instead of getUnreadFeed()). Are you sure this is correct? If so, you might want to change this to $this->fetchAndCastToObjectArray().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
368
    }
369
370
    /**
371
     * Get all of the users
372
     *
373
     * @param  array $params GET parameters that need to be passed in the URL
374
     *
375
     * @since  0.1.0
376
     *
377
     * @return PulseUser[] An array of PulseUsers for each of the users
378
     */
379
    public static function getUsers ($params = array())
380
    {
381
        $url = sprintf("%s.json", parent::apiEndpoint());
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (apiEndpoint() instead of getUsers()). Are you sure this is correct? If so, you might want to change this to $this->apiEndpoint().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
382
383
        return parent::fetchAndCastToObjectArray($url, "PulseUser", $params);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (fetchAndCastToObjectArray() instead of getUsers()). Are you sure this is correct? If so, you might want to change this to $this->fetchAndCastToObjectArray().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
384
    }
385
386
    // =================================================================================================================
387
    //   Convenience functions
388
    // =================================================================================================================
389
390
    /**
391
     * Check whether a given value can be casted or used to get a user ID
392
     *
393
     * @internal
394
     *
395
     * @param int|PulseUser $user
396
     *
397
     * @since 0.3.0
398
     *
399
     * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object
400
     */
401 5
    public static function _isCastable ($user)
402
    {
403 5
        if ((!is_int($user) || (is_int($user) && $user < 1)) && !($user instanceof PulseUser))
404
        {
405
            throw new \InvalidArgumentException('$user is expected to be a positive integer or a PulseUser object');
406
        }
407
    }
408
}