Completed
Push — master ( 95cc27...d32772 )
by Vladimir
05:08 queued 03:05
created

PulseUser::isGuest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
322
     * @deprecated 0.3.0 Use PulseUser::isGuest() instead
323
     * @since  0.1.0
324
     * @return bool
325
     */
326
    public function getIsGuest ()
327
    {
328
        return $this->isGuest();
329
    }
330
331
    /**
332
     * The user's skills.
333
     *
334
     * @api
335
     *
336
     * @since  0.1.0
337
     *
338
     * @return string[]
339
     */
340
    public function getSkills ()
341
    {
342
        $this->lazyLoad();
343
344
        return $this->skills;
345
    }
346
347
    /**
348
     * Creation time.
349
     *
350
     * @api
351
     *
352
     * @since  0.1.0
353
     *
354
     * @return \DateTime
355
     */
356
    public function getCreatedAt ()
357
    {
358
        $this->lazyLoad();
359
        self::lazyCast($this->created_at, '\DateTime');
360
361
        return $this->created_at;
362
    }
363
364
    /**
365
     * Last update time.
366
     *
367
     * @api
368
     *
369
     * @since  0.1.0
370
     *
371
     * @return \DateTime
372
     */
373
    public function getUpdatedAt ()
374
    {
375
        $this->lazyLoad();
376
        self::lazyCast($this->updated_at, '\DateTime');
377
378
        return $this->updated_at;
379
    }
380
381
    /**
382
     * True if the user is guest, false otherwise
383
     *
384
     * @api
385
     *
386
     * @since  0.3.0
387
     *
388
     * @return bool
389
     */
390
    public function isGuest ()
391
    {
392
        $this->lazyLoad();
393
394
        return $this->is_guest;
395
    }
396
397
    /**
398
     * Get the user's newsfeed
399
     *
400
     * @api
401
     *
402
     * @param  array $params GET parameters that need to be passed in the URL
403
     *
404
     * @since  0.1.0
405
     *
406
     * @return PulseUpdate[] An array of PulseUpdates that make up the user's newsfeed
407
     */
408
    public function getNewsFeed ($params = [])
409
    {
410
        $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...
411
412
        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...
413
    }
414
415
    /**
416
     * Get the user's posts
417
     *
418
     * @api
419
     *
420
     * @param  array $params GET parameters that need to be passed in the URL
421
     *
422
     * @since  0.1.0
423
     *
424
     * @return PulseUpdate[] An array of PulseUpdates for each of the posts
425
     */
426
    public function getPosts ($params = [])
427
    {
428
        $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...
429
430
        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...
431
    }
432
433
    /**
434
     * Get the user's unread posts
435
     *
436
     * @api
437
     *
438
     * @param  array $params GET parameters that need to be passed in the URL
439
     *
440
     * @since  0.1.0
441
     *
442
     * @return PulseUpdate[] An array of PulseUpdates for each of the posts
443
     */
444
    public function getUnreadFeed ($params = [])
445
    {
446
        $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...
447
448
        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...
449
    }
450
451
    /**
452
     * Get all of the users
453
     *
454
     * @api
455
     *
456
     * @param  array $params GET parameters that need to be passed in the URL
457
     *
458
     * @since  0.1.0
459
     *
460
     * @return PulseUser[] An array of PulseUsers for each of the users
461
     */
462
    public static function getUsers ($params = [])
463
    {
464
        $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...
465
466
        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...
467
    }
468
469
    // =================================================================================================================
470
    //   Convenience functions
471
    // =================================================================================================================
472
473
    /**
474
     * Check whether a given value can be casted or used to get a user ID
475
     *
476
     * @internal
477
     *
478
     * @param int|PulseUser $user
479
     *
480
     * @since 0.3.0
481
     *
482
     * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object
483
     */
484 12
    public static function _isCastable ($user)
485
    {
486 12
        if ((!is_int($user) || (is_int($user) && $user < 1)) && !($user instanceof PulseUser))
487
        {
488 5
            throw new \InvalidArgumentException('$user is expected to be a positive integer or a PulseUser object');
489
        }
490 7
    }
491
492
    /**
493
     * @internal
494
     *
495
     * @param  int|PulseUser $user
496
     *
497
     * @since  0.3.0
498
     *
499
     * @return int
500
     */
501 12
    public static function _castToInt ($user)
502
    {
503 12
        self::_isCastable($user);
504
505 7
        return ($user instanceof PulseUser) ? $user->getId() : $user;
506
    }
507
}