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

PulseUpdate::getUpdates()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
ccs 10
cts 10
cp 1
rs 8.6737
cc 5
eloc 10
nc 5
nop 1
crap 5
1
<?php
2
3
namespace allejo\DaPulse;
4
5
use allejo\DaPulse\Exceptions\InvalidObjectException;
6
use allejo\DaPulse\Objects\ApiObject;
7
8
class PulseUpdate extends ApiObject
9
{
10
    const API_PREFIX = "updates";
11
12
    /**
13
     * User who wrote the update.
14
     *
15
     * @var array|PulseUser
16
     */
17
    protected $user;
18
19
    /**
20
     * The resource's URL.
21
     *
22
     * @var string
23
     */
24
    protected $url;
25
26
    /**
27
     * The update's id.
28
     *
29
     * @var string
30
     */
31
    protected $id;
32
33
    /**
34
     * The update's body.
35
     *
36
     * @var string
37
     */
38
    protected $body;
39
40
    /**
41
     * The update's body in plain text
42
     *
43
     * @var string
44
     */
45
    protected $body_text;
46
47
    /**
48
     * The replies made to this update.
49
     *
50
     * @var array
51
     */
52
    protected $replies;
53
54
    /**
55
     * The update's kind.
56
     *
57
     * @var string
58
     */
59
    protected $kind;
60
61
    /**
62
     * The update's has_assets.
63
     *
64
     * @var string
65
     */
66
    protected $has_assets;
67
68
    /**
69
     * The update's assets.
70
     *
71
     * @var array
72
     */
73
    protected $assets;
74
75
    /**
76
     * Creation time.
77
     *
78
     * @var \DateTime
79
     */
80
    protected $created_at;
81
82
    /**
83
     * Last update time.
84
     *
85
     * @var \DateTime
86
     */
87
    protected $updated_at;
88
89
    // =================================================================================================================
90
    //   Getter functions
91
    // =================================================================================================================
92
93
    /**
94
     * User who wrote the update.
95
     *
96
     * @deprecated 0.1.0 This function will be removed by 0.2.0. Use of PulseUpdate->getAuthor() instead.
97
     *
98
     * @return     PulseUser
99
     */
100
    public function getUser ()
101
    {
102
        return $this->getAuthor();
103
    }
104
105
    /**
106
     * User who wrote the update.
107
     *
108
     * @api
109
     * @since  0.1.0
110
     * @return PulseUser
111
     */
112 1
    public function getAuthor ()
113
    {
114 1
        self::lazyCast($this->user, "PulseUser");
115
116 1
        return $this->user;
117
    }
118
119
    /**
120
     * The resource's URL.
121
     *
122
     * @api
123
     * @since  0.1.0
124
     * @return string
125
     */
126 1
    public function getUrl ()
127
    {
128 1
        return $this->url;
129
    }
130
131
    /**
132
     * The update's id.
133
     *
134
     * @api
135
     * @since  0.1.0
136
     * @return string
137
     */
138 4
    public function getId ()
139
    {
140 4
        return $this->id;
141
    }
142
143
    /**
144
     * The update's body.
145
     *
146
     * @api
147
     * @since  0.1.0
148
     * @return string
149
     */
150 1
    public function getBody ()
151
    {
152 1
        return $this->body;
153
    }
154
155
    /**
156
     * The update's body in plain text
157
     *
158
     * @api
159
     * @since  0.1.0
160
     * @return string
161
     */
162 1
    public function getBodyText ()
163
    {
164 1
        return $this->body_text;
165
    }
166
167
    /**
168
     * The replies made to this update.
169
     *
170
     * @api
171
     * @since  0.1.0
172
     * @return static[]
173
     */
174 3
    public function getReplies ()
175
    {
176 3
        self::lazyCastAll($this->replies, "PulseUpdate");
177
178 3
        return $this->replies;
179
    }
180
181
    /**
182
     * The update's kind.
183
     *
184
     * @api
185
     * @since  0.1.0
186
     * @return string
187
     */
188 1
    public function getKind ()
189
    {
190 1
        return $this->kind;
191
    }
192
193
    /**
194
     * Retrieve whether or not this update has any attachments
195
     *
196
     * @api
197
     * @todo Remove at 0.4.0 or next major 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...
198
     * @deprecated 0.3.0 Use PulseUpdate::hasAssets(). To be removed in 0.4.0
199
     * @since  0.1.0
200
     * @return string
201
     */
202
    public function getHasAssets ()
203
    {
204
        return $this->hasAssets();
205
    }
206
207
    /**
208
     * Get an array of this update's assets
209
     *
210
     * Sample array structure of assets
211
     *
212
     * ```
213
     * array(
214
     *   0 => array(
215
     *     'account_id' => 115448
216
     *     'big_geometry' => '250x250'
217
     *     'created_at' => '2017-01-21T09:45:28Z'
218
     *     'crocodoc_status' => null
219
     *     'crocodoc_uuid' => null
220
     *     'crocodoc_viewable' => true
221
     *     'desc' => null
222
     *     'holder_id' => 23611844
223
     *     'holder_type' => 'Post'
224
     *     'id' => 2401793
225
     *     'large_geometry' => '250x250'
226
     *     'metadata' => Array ()
227
     *     'original_geometry' => '250x250'
228
     *     'resource_content_type' => 'image/png'
229
     *     'resource_file_name' => 'sample.png'
230
     *     'resource_file_size' => 6077
231
     *     'thumb_big_geometry' => '250x250'
232
     *     'thumb_geometry' => '150x150'
233
     *     'updated_at' => '2017-01-21T09:45:32Z'
234
     *     'uploaded_by_id' => 303448
235
     *   )
236
     * )
237
     * ```
238
     *
239
     * @api
240
     *
241
     * @since  0.3.0 Documentation has been corrected; this returns an array
242
     * @since  0.1.0
243
     *
244
     * @return array
245
     */
246 2
    public function getAssets ()
247
    {
248 2
        return $this->assets;
249
    }
250
251
    /**
252
     * Creation time.
253
     *
254
     * @api
255
     * @since  0.1.0
256
     * @return \DateTime
257
     */
258 1
    public function getCreatedAt ()
259
    {
260 1
        self::lazyCast($this->created_at, '\DateTime');
261
262 1
        return $this->created_at;
263
    }
264
265
    /**
266
     * Last update time.
267
     *
268
     * @api
269
     * @since  0.1.0
270
     * @return \DateTime
271
     */
272 1
    public function getUpdatedAt ()
273
    {
274 1
        self::lazyCast($this->updated_at, '\DateTime');
275
276 1
        return $this->updated_at;
277
    }
278
279
    /**
280
     * Get the users watching this update
281
     *
282
     * @api
283
     * @todo Remove at 0.4.0 or next major 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...
284
     * @deprecated 0.3.0 This data is no longer provided by the DaPulse API; this function will be removed in the next
285
     *                   major release with planned replacement
286
     * @since  0.1.0
287
     * @return PulseUser[]
288
     */
289
    public function getWatchers ()
290
    {
291
        return [];
292
    }
293
294
    /**
295
     * Retrieve whether or not this update has any attachments
296
     *
297
     * @api
298
     *
299
     * @since  0.3.0 Previously was available as 'getHasAssets()'
300
     *
301
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
302
     */
303 1
    public function hasAssets ()
304
    {
305 1
        return $this->has_assets;
306
    }
307
308
    // =================================================================================================================
309
    //   Modification functions
310
    // =================================================================================================================
311
312
    /**
313
     * Delete this update
314
     *
315
     * @api
316
     *
317
     * @since  0.1.0
318
     *
319
     * @throws InvalidObjectException if this PulseUpdate has already been deleted
320
     */
321 View Code Duplication
    public function deleteUpdate ()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
322
    {
323
        $this->checkInvalid();
324
325
        $url = sprintf("%s/%d.json", self::apiEndpoint(), $this->getId());
326
        self::sendDelete($url);
327
328
        $this->deletedObject = true;
329
    }
330
331
    // =================================================================================================================
332
    //   Liking functions
333
    // =================================================================================================================
334
335
    /**
336
     * Have a user like an update
337
     *
338
     * @api
339
     *
340
     * @param int|PulseUser $user The user that will be liking/un-liking the update
341
     *
342
     * @since 0.1.0
343
     *
344
     * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object
345
     *
346
     * @return bool Returns true on success
347
     */
348 1
    public function likeUpdate ($user)
349
    {
350 1
        return $this->likeUnlikeUpdate($user, true);
351
    }
352
353
    /**
354
     * Have a user unlike an update
355
     *
356
     * @api
357
     *
358
     * @param int|PulseUser $user The user that will be liking/un-liking the update
359
     *
360
     * @since 0.1.0
361
     *
362
     * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object
363
     *
364
     * @return bool Returns true on success
365
     */
366 2
    public function unlikeUpdate ($user)
367
    {
368 2
        return $this->likeUnlikeUpdate($user, false);
369
    }
370
371
    /**
372
     * Like and un-liking functionality
373
     *
374
     * @param int|PulseUser $user The user that will be liking/un-liking the update
375
     * @param bool          $like True to like the update, false to unlike
376
     *
377
     * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object
378
     *
379
     * @return bool Returns true on success
380
     */
381 3
    private function likeUnlikeUpdate ($user, $like)
382
    {
383 3
        PulseUser::_isCastable($user);
384
385 3
        $user   = ($user instanceof PulseUser) ? $user->getId() : $user;
386 3
        $url    = sprintf("%s/%d/%s.json", self::apiEndpoint(), $this->getId(), (($like) ? "like" : "unlike"));
387
        $params = array(
388 3
            "user" => $user
389
        );
390
391 3
        return self::sendPost($url, $params);
392
    }
393
394
    // =================================================================================================================
395
    //   PulseUpdate functions
396
    // =================================================================================================================
397
398
    /**
399
     * Get all of the account's updates (ordered from newest to oldest)
400
     *
401
     * array['since']         \DateTime Get updates from a specific date
402
     *      ['until']         \DateTime Get updates until a specific date
403
     *      ['updated_since'] \DateTime Get updates that were edited or replied to after a specific date
404
     *      ['updated_until'] \DateTime Get updates that were edited or replied to before a specific date
405
     *
406
     * If you do not pass \DateTime objects, they should be strings of dates in the format, YYYY-mm-dd, or a unix timestamp
407
     *
408
     * @api
409
     *
410
     * @param  array $params GET parameters passed to the URL (see above)
411
     *
412
     * @since  0.3.0 $params now accepts \DateTime objects and will be converted automatically. Strings will also try to
413
     *               be converted to Unix timestamps
414
     * @since  0.1.0
415
     *
416
     * @return PulseUpdate[]
417
     */
418 4
    public static function getUpdates ($params = [])
419
    {
420 4
        $url = sprintf("%s.json", self::apiEndpoint());
421 4
        $dateKeys = ['since', 'until', 'updated_since', 'updated_until'];
422
423 4
        foreach ($params as $key => &$value)
424
        {
425 3
            if (in_array($key, $dateKeys))
426
            {
427 3
                if ($value instanceof \DateTime)
428
                {
429 1
                    $value = date_format($value, 'U'); // A unix timestamp will allow for hours & minutes
430
                }
431 2
                else if (($unix = strtotime($value)))
432
                {
433 3
                    $value = $unix;
434
                }
435
            }
436
        }
437
438 4
        return self::fetchAndCastToObjectArray($url, 'PulseUpdate', $params);
439
    }
440
441
    /**
442
     * Create a new update
443
     *
444
     * @api
445
     *
446
     * @param  int|PulseUser $user          The author of this post
447
     * @param  int|Pulse     $pulse         The Pulse to whom this update will belong to
448
     * @param  string        $text          The content of the update
449
     * @param  null|bool     $announceToAll Whether or not to announce this update to everyone's wall
450
     *
451
     * @since  0.1.0
452
     *
453
     * @return PulseUpdate
454
     */
455
    public static function createUpdate ($user, $pulse, $text, $announceToAll = NULL)
456
    {
457
        if ($user instanceof PulseUser)
458
        {
459
            $user = $user->getId();
460
        }
461
462
        if ($pulse instanceof Pulse)
463
        {
464
            $pulse = $pulse->getId();
465
        }
466
467
        $url    = sprintf("%s.json", self::apiEndpoint());
468
        $params = array(
469
            "user"        => $user,
470
            "pulse"       => $pulse,
471
            "update_text" => $text
472
        );
473
474
        self::setIfNotNullOrEmpty($params, "announcement", $announceToAll);
475
476
        $result = self::sendPost($url, $params);
477
478
        return (new PulseUpdate($result));
479
    }
480
}