Completed
Push — master ( d3416c...51337e )
by Vladimir
02:28
created

PulseUpdate::getUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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 string
72
     */
73
    protected $assets;
74
75
    /**
76
     * The users who watch this update.
77
     *
78
     * @var array
79
     */
80
    protected $watched;
81
82
    /**
83
     * Creation time.
84
     *
85
     * @var \DateTime
86
     */
87
    protected $created_at;
88
89
    /**
90
     * Last update time.
91
     *
92
     * @var \DateTime
93
     */
94
    protected $updated_at;
95
96
    // =================================================================================================================
97
    //   Getter functions
98
    // =================================================================================================================
99
100
    /**
101
     * User who wrote the update.
102
     *
103
     * @deprecated 0.1.0 This function will be removed by 0.2.0. Use of PulseUpdate->getAuthor() instead.
104
     *
105
     * @return     PulseUser
106
     */
107
    public function getUser ()
108
    {
109
        return $this->getAuthor();
110
    }
111
112
    /**
113
     * User who wrote the update.
114
     *
115
     * @api
116
     * @since  0.1.0
117
     * @return PulseUser
118
     */
119
    public function getAuthor ()
120
    {
121
        self::lazyLoad($this->user, "PulseUser");
122
123
        return $this->user;
124
    }
125
126
    /**
127
     * The resource's URL.
128
     *
129
     * @api
130
     * @since  0.1.0
131
     * @return string
132
     */
133
    public function getUrl ()
134
    {
135
        return $this->url;
136
    }
137
138
    /**
139
     * The update's id.
140
     *
141
     * @api
142
     * @since  0.1.0
143
     * @return string
144
     */
145
    public function getId ()
146
    {
147
        return $this->id;
148
    }
149
150
    /**
151
     * The update's body.
152
     *
153
     * @api
154
     * @since  0.1.0
155
     * @return string
156
     */
157
    public function getBody ()
158
    {
159
        return $this->body;
160
    }
161
162
    /**
163
     * The update's body in plain text
164
     *
165
     * @api
166
     * @since  0.1.0
167
     * @return string
168
     */
169
    public function getBodyText ()
170
    {
171
        return $this->body_text;
172
    }
173
174
    /**
175
     * The replies made to this update.
176
     *
177
     * @api
178
     * @since  0.1.0
179
     * @return static[]
180
     */
181
    public function getReplies ()
182
    {
183
        self::lazyArray($this->replies, "PulseUpdate");
184
185
        return $this->replies;
186
    }
187
188
    /**
189
     * The update's kind.
190
     *
191
     * @api
192
     * @since  0.1.0
193
     * @return string
194
     */
195
    public function getKind ()
196
    {
197
        return $this->kind;
198
    }
199
200
    /**
201
     * The update's has_assets.
202
     *
203
     * @api
204
     * @since  0.1.0
205
     * @return string
206
     */
207
    public function getHasAssets ()
208
    {
209
        return $this->has_assets;
210
    }
211
212
    /**
213
     * The update's assets.
214
     *
215
     * @api
216
     * @since  0.1.0
217
     * @return string
218
     */
219
    public function getAssets ()
220
    {
221
        return $this->assets;
222
    }
223
224
    /**
225
     * Creation time.
226
     *
227
     * @api
228
     * @since  0.1.0
229
     * @return \DateTime
230
     */
231
    public function getCreatedAt ()
232
    {
233
        self::lazyLoad($this->created_at, '\DateTime');
234
235
        return $this->created_at;
236
    }
237
238
    /**
239
     * Last update time.
240
     *
241
     * @api
242
     * @since  0.1.0
243
     * @return \DateTime
244
     */
245
    public function getUpdatedAt ()
246
    {
247
        self::lazyLoad($this->updated_at, '\DateTime');
248
249
        return $this->updated_at;
250
    }
251
252
    /**
253
     * Get the users watching this update
254
     *
255
     * @api
256
     * @since  0.1.0
257
     * @return PulseUser[]
258
     */
259
    public function getWatchers ()
260
    {
261
        self::lazyArray($this->watched, "PulseUser");
262
263
        return $this->watched;
264
    }
265
266
    // =================================================================================================================
267
    //   Modification functions
268
    // =================================================================================================================
269
270
    /**
271
     * Delete this update
272
     *
273
     * @api
274
     *
275
     * @since  0.1.0
276
     *
277
     * @throws InvalidObjectException This PulseUpdate as already been deleted
278
     */
279 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...
280
    {
281
        $this->checkInvalid();
282
283
        $url = sprintf("%s/%d.json", self::apiEndpoint(), $this->getId());
284
        self::sendDelete($url);
285
286
        $this->deletedObject = true;
287
    }
288
289
    // =================================================================================================================
290
    //   Liking functions
291
    // =================================================================================================================
292
293
    /**
294
     * Have a user like an update
295
     *
296
     * @api
297
     *
298
     * @param int|PulseUser $user The user that will be liking/un-liking the update
299
     *
300
     * @since 0.1.0
301
     */
302
    public function likeUpdate ($user)
303
    {
304
        $this->likeUnlikeUpdate($user, true);
305
    }
306
307
    /**
308
     * Have a user unlike an update
309
     *
310
     * @api
311
     *
312
     * @param int|PulseUser $user The user that will be liking/un-liking the update
313
     *
314
     * @since 0.1.0
315
     */
316
    public function unlikeUpdate ($user)
317
    {
318
        $this->likeUnlikeUpdate($user, false);
319
    }
320
321
    /**
322
     * Like and un-liking functionality
323
     *
324
     * @param int|PulseUser $user The user that will be liking/un-liking the update
325
     * @param bool          $like True to like the update, false to unlike
326
     */
327
    private function likeUnlikeUpdate ($user, $like)
328
    {
329
        if ($user instanceof PulseUser)
330
        {
331
            $user = $user->getId();
332
        }
333
334
        $url    = sprintf("%s/%d/%s.json", self::apiEndpoint(), $this->getId(), (($like) ? "like" : "unlike"));
335
        $params = array(
336
            "user" => $user
337
        );
338
339
        self::sendPost($url, $params);
340
    }
341
342
    // =================================================================================================================
343
    //   PulseUpdate functions
344
    // =================================================================================================================
345
346
    /**
347
     * Get all of the account's updates (ordered from new to old)
348
     *
349
     * @api
350
     *
351
     * @param  array $params GET parameters passed to the URL
352
     *
353
     * @since  0.1.0
354
     *
355
     * @return PulseUpdate[]
356
     */
357
    public static function getUpdates ($params = array())
358
    {
359
        $url = sprintf("%s.json", self::apiEndpoint());
360
361
        return self::sendGet($url, $params);
362
    }
363
364
    /**
365
     * Create a new update
366
     *
367
     * @api
368
     *
369
     * @param  int|PulseUser $user          The author of this post
370
     * @param  int|Pulse     $pulse         The Pulse to whom this update will belong to
371
     * @param  string        $text          The content of the update
372
     * @param  null|bool     $announceToAll Whether or not to announce this update to everyone's wall
373
     *
374
     * @since  0.1.0
375
     *
376
     * @return PulseUpdate
377
     */
378
    public static function createUpdate ($user, $pulse, $text, $announceToAll = NULL)
379
    {
380
        if ($user instanceof PulseUser)
381
        {
382
            $user = $user->getId();
383
        }
384
385
        if ($pulse instanceof Pulse)
386
        {
387
            $pulse = $pulse->getId();
388
        }
389
390
        $url    = sprintf("%s.json", self::apiEndpoint());
391
        $params = array(
392
            "user"        => $user,
393
            "pulse"       => $pulse,
394
            "update_text" => $text
395
        );
396
397
        self::setIfNotNullOrEmpty($params, "announcement", $announceToAll);
398
399
        $result = self::sendPost($url, $params);
400
401
        return (new PulseUpdate($result));
402
    }
403
}