Completed
Push — develop ( 205ec8...3516bb )
by Vladimir
03:24
created

PulseUpdate::getBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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
     * @api
97
     * @since  0.1.0
98
     * @return PulseUser
99
     */
100 1
    public function getAuthor ()
101
    {
102 1
        self::lazyCast($this->user, "PulseUser");
103
104 1
        return $this->user;
105
    }
106
107
    /**
108
     * The resource's URL.
109
     *
110
     * @api
111
     * @since  0.1.0
112
     * @return string
113
     */
114 1
    public function getUrl ()
115
    {
116 1
        return $this->url;
117
    }
118
119
    /**
120
     * The update's id.
121
     *
122
     * @api
123
     * @since  0.1.0
124
     * @return string
125
     */
126 4
    public function getId ()
127
    {
128 4
        return $this->id;
129
    }
130
131
    /**
132
     * The update's body.
133
     *
134
     * @api
135
     * @since  0.1.0
136
     * @return string
137
     */
138 1
    public function getBody ()
139
    {
140 1
        return $this->body;
141
    }
142
143
    /**
144
     * The update's body in plain text
145
     *
146
     * @api
147
     * @since  0.1.0
148
     * @return string
149
     */
150 1
    public function getBodyText ()
151
    {
152 1
        return $this->body_text;
153
    }
154
155
    /**
156
     * The replies made to this update.
157
     *
158
     * @api
159
     * @since  0.1.0
160
     * @return static[]
161
     */
162 3
    public function getReplies ()
163
    {
164 3
        self::lazyCastAll($this->replies, "PulseUpdate");
165
166 3
        return $this->replies;
167
    }
168
169
    /**
170
     * The update's kind.
171
     *
172
     * @api
173
     * @since  0.1.0
174
     * @return string
175
     */
176 1
    public function getKind ()
177
    {
178 1
        return $this->kind;
179
    }
180
181
    /**
182
     * Retrieve whether or not this update has any attachments
183
     *
184
     * @api
185
     * @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...
186
     * @deprecated 0.3.0 Use PulseUpdate::hasAssets(). To be removed in 0.4.0
187
     * @since  0.1.0
188
     * @return string
189
     */
190
    public function getHasAssets ()
191
    {
192
        return $this->hasAssets();
193
    }
194
195
    /**
196
     * Get an array of this update's assets
197
     *
198
     * Sample array structure of assets
199
     *
200
     * ```
201
     * array(
202
     *   0 => array(
203
     *     'account_id' => 115448
204
     *     'big_geometry' => '250x250'
205
     *     'created_at' => '2017-01-21T09:45:28Z'
206
     *     'crocodoc_status' => null
207
     *     'crocodoc_uuid' => null
208
     *     'crocodoc_viewable' => true
209
     *     'desc' => null
210
     *     'holder_id' => 23611844
211
     *     'holder_type' => 'Post'
212
     *     'id' => 2401793
213
     *     'large_geometry' => '250x250'
214
     *     'metadata' => Array ()
215
     *     'original_geometry' => '250x250'
216
     *     'resource_content_type' => 'image/png'
217
     *     'resource_file_name' => 'sample.png'
218
     *     'resource_file_size' => 6077
219
     *     'thumb_big_geometry' => '250x250'
220
     *     'thumb_geometry' => '150x150'
221
     *     'updated_at' => '2017-01-21T09:45:32Z'
222
     *     'uploaded_by_id' => 303448
223
     *   )
224
     * )
225
     * ```
226
     *
227
     * @api
228
     *
229
     * @since  0.3.0 Documentation has been corrected; this returns an array
230
     * @since  0.1.0
231
     *
232
     * @return array
233
     */
234 2
    public function getAssets ()
235
    {
236 2
        return $this->assets;
237
    }
238
239
    /**
240
     * Creation time.
241
     *
242
     * @api
243
     * @since  0.1.0
244
     * @return \DateTime
245
     */
246 1
    public function getCreatedAt ()
247
    {
248 1
        self::lazyCast($this->created_at, '\DateTime');
249
250 1
        return $this->created_at;
251
    }
252
253
    /**
254
     * Last update time.
255
     *
256
     * @api
257
     * @since  0.1.0
258
     * @return \DateTime
259
     */
260 1
    public function getUpdatedAt ()
261
    {
262 1
        self::lazyCast($this->updated_at, '\DateTime');
263
264 1
        return $this->updated_at;
265
    }
266
267
    /**
268
     * Get the users watching this update
269
     *
270
     * @api
271
     * @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...
272
     * @deprecated 0.3.0 This data is no longer provided by the DaPulse API; this function will be removed in the next
273
     *                   major release with planned replacement
274
     * @since  0.1.0
275
     * @return PulseUser[]
276
     */
277
    public function getWatchers ()
278
    {
279
        return [];
280
    }
281
282
    /**
283
     * Retrieve whether or not this update has any attachments
284
     *
285
     * @api
286
     *
287
     * @since  0.3.0 Previously was available as 'getHasAssets()'
288
     *
289
     * @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...
290
     */
291 1
    public function hasAssets ()
292
    {
293 1
        return $this->has_assets;
294
    }
295
296
    // =================================================================================================================
297
    //   Modification functions
298
    // =================================================================================================================
299
300
    /**
301
     * Delete this update
302
     *
303
     * @api
304
     *
305
     * @since  0.1.0
306
     *
307
     * @throws InvalidObjectException if this PulseUpdate has already been deleted
308
     */
309 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...
310
    {
311
        $this->checkInvalid();
312
313
        $url = sprintf("%s/%d.json", self::apiEndpoint(), $this->getId());
314
        self::sendDelete($url);
315
316
        $this->deletedObject = true;
317
    }
318
319
    // =================================================================================================================
320
    //   Liking functions
321
    // =================================================================================================================
322
323
    /**
324
     * Have a user like an update
325
     *
326
     * @api
327
     *
328
     * @param int|PulseUser $user The user that will be liking/un-liking the update
329
     *
330
     * @since 0.1.0
331
     *
332
     * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object
333
     *
334
     * @return bool Returns true on success
335
     */
336 1
    public function likeUpdate ($user)
337
    {
338 1
        return $this->likeUnlikeUpdate($user, true);
339
    }
340
341
    /**
342
     * Have a user unlike an update
343
     *
344
     * @api
345
     *
346
     * @param int|PulseUser $user The user that will be liking/un-liking the update
347
     *
348
     * @since 0.1.0
349
     *
350
     * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object
351
     *
352
     * @return bool Returns true on success
353
     */
354 2
    public function unlikeUpdate ($user)
355
    {
356 2
        return $this->likeUnlikeUpdate($user, false);
357
    }
358
359
    /**
360
     * Like and un-liking functionality
361
     *
362
     * @param int|PulseUser $user The user that will be liking/un-liking the update
363
     * @param bool          $like True to like the update, false to unlike
364
     *
365
     * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object
366
     *
367
     * @return bool Returns true on success
368
     */
369 3
    private function likeUnlikeUpdate ($user, $like)
370
    {
371 3
        PulseUser::_isCastable($user);
372
373 3
        $user   = ($user instanceof PulseUser) ? $user->getId() : $user;
374 3
        $url    = sprintf("%s/%d/%s.json", self::apiEndpoint(), $this->getId(), (($like) ? "like" : "unlike"));
375
        $params = array(
376
            "user" => $user
377 3
        );
378
379 3
        return self::sendPost($url, $params);
380
    }
381
382
    // =================================================================================================================
383
    //   PulseUpdate functions
384
    // =================================================================================================================
385
386
    /**
387
     * Get all of the account's updates (ordered from newest to oldest)
388
     *
389
     * ```
390
     * array['since']         \DateTime - Get updates from a specific date
391
     *      ['until']         \DateTime - Get updates until a specific date
392
     *      ['updated_since'] \DateTime - Get updates that were edited or replied to after a specific date
393
     *      ['updated_until'] \DateTime - Get updates that were edited or replied to before a specific date
394
     * ```
395
     *
396
     * If you do not pass \DateTime objects, they should be strings of dates in the format, YYYY-mm-dd, or a unix timestamp
397
     *
398
     * @api
399
     *
400
     * @param  array $params GET parameters passed to the URL (see above)
401
     *
402
     * @since  0.3.0 $params now accepts \DateTime objects and will be converted automatically. Strings will also try to
403
     *               be converted to Unix timestamps
404
     * @since  0.1.0
405
     *
406
     * @return PulseUpdate[]
407
     */
408 4
    public static function getUpdates ($params = [])
409
    {
410 4
        $url = sprintf("%s.json", self::apiEndpoint());
411 4
        $dateKeys = ['since', 'until', 'updated_since', 'updated_until'];
412
413 4
        foreach ($params as $key => &$value)
414
        {
415 3
            if (in_array($key, $dateKeys))
416 3
            {
417 3
                if ($value instanceof \DateTime)
418 3
                {
419 1
                    $value = date_format($value, 'U'); // A unix timestamp will allow for hours & minutes
420 1
                }
421 2
                else if (($unix = strtotime($value)))
422 2
                {
423 1
                    $value = $unix;
424 1
                }
425 3
            }
426 4
        }
427
428 4
        return self::fetchAndCastToObjectArray($url, 'PulseUpdate', $params);
429
    }
430
431
    /**
432
     * Create a new update
433
     *
434
     * @api
435
     *
436
     * @param  int|PulseUser $user          The author of this post
437
     * @param  int|Pulse     $pulse         The Pulse to whom this update will belong to
438
     * @param  string        $text          The content of the update
439
     * @param  null|bool     $announceToAll Whether or not to announce this update to everyone's wall
440
     *
441
     * @since  0.1.0
442
     *
443
     * @return PulseUpdate
444
     */
445
    public static function createUpdate ($user, $pulse, $text, $announceToAll = NULL)
446
    {
447
        if ($user instanceof PulseUser)
448
        {
449
            $user = $user->getId();
450
        }
451
452
        if ($pulse instanceof Pulse)
453
        {
454
            $pulse = $pulse->getId();
455
        }
456
457
        $url    = sprintf("%s.json", self::apiEndpoint());
458
        $params = array(
459
            "user"        => $user,
460
            "pulse"       => $pulse,
461
            "update_text" => $text
462
        );
463
464
        self::setIfNotNullOrEmpty($params, "announcement", $announceToAll);
465
466
        $result = self::sendPost($url, $params);
467
468
        return (new PulseUpdate($result));
469
    }
470
}