Completed
Push — develop ( a05ff5...d1fb89 )
by Vladimir
03:27
created

PulseUpdate::hasAssets()   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 0
Metric Value
c 0
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
     * @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 1
    public function getId ()
139
    {
140 1
        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 This PulseUpdate as 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
    public function likeUpdate ($user)
345
    {
346
        $this->likeUnlikeUpdate($user, true);
347
    }
348
349
    /**
350
     * Have a user unlike an update
351
     *
352
     * @api
353
     *
354
     * @param int|PulseUser $user The user that will be liking/un-liking the update
355
     *
356
     * @since 0.1.0
357
     */
358
    public function unlikeUpdate ($user)
359
    {
360
        $this->likeUnlikeUpdate($user, false);
361
    }
362
363
    /**
364
     * Like and un-liking functionality
365
     *
366
     * @param int|PulseUser $user The user that will be liking/un-liking the update
367
     * @param bool          $like True to like the update, false to unlike
368
     */
369
    private function likeUnlikeUpdate ($user, $like)
370
    {
371
        if ($user instanceof PulseUser)
372
        {
373
            $user = $user->getId();
374
        }
375
376
        $url    = sprintf("%s/%d/%s.json", self::apiEndpoint(), $this->getId(), (($like) ? "like" : "unlike"));
377
        $params = array(
378
            "user" => $user
379
        );
380
381
        self::sendPost($url, $params);
382
    }
383
384
    // =================================================================================================================
385
    //   PulseUpdate functions
386
    // =================================================================================================================
387
388
    /**
389
     * Get all of the account's updates (ordered from new to old)
390
     *
391
     * @api
392
     *
393
     * @param  array $params GET parameters passed to the URL
394
     *
395
     * @since  0.1.0
396
     *
397
     * @return PulseUpdate[]
398
     */
399
    public static function getUpdates ($params = array())
400
    {
401
        $url = sprintf("%s.json", self::apiEndpoint());
402
403
        return self::sendGet($url, $params);
404
    }
405
406
    /**
407
     * Create a new update
408
     *
409
     * @api
410
     *
411
     * @param  int|PulseUser $user          The author of this post
412
     * @param  int|Pulse     $pulse         The Pulse to whom this update will belong to
413
     * @param  string        $text          The content of the update
414
     * @param  null|bool     $announceToAll Whether or not to announce this update to everyone's wall
415
     *
416
     * @since  0.1.0
417
     *
418
     * @return PulseUpdate
419
     */
420
    public static function createUpdate ($user, $pulse, $text, $announceToAll = NULL)
421
    {
422
        if ($user instanceof PulseUser)
423
        {
424
            $user = $user->getId();
425
        }
426
427
        if ($pulse instanceof Pulse)
428
        {
429
            $pulse = $pulse->getId();
430
        }
431
432
        $url    = sprintf("%s.json", self::apiEndpoint());
433
        $params = array(
434
            "user"        => $user,
435
            "pulse"       => $pulse,
436
            "update_text" => $text
437
        );
438
439
        self::setIfNotNullOrEmpty($params, "announcement", $announceToAll);
440
441
        $result = self::sendPost($url, $params);
442
443
        return (new PulseUpdate($result));
444
    }
445
}