Completed
Push — master ( 4c0acc...9607bb )
by Vladimir
09:45
created

PulseBoard::deleteGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file contains the PulseBoard class
5
 *
6
 * @copyright 2015 Vladimir Jimenez
7
 * @license   https://github.com/allejo/PhpPulse/blob/master/LICENSE.md MIT
8
 */
9
10
namespace allejo\DaPulse;
11
12
use allejo\DaPulse\Exceptions\ArgumentMismatchException;
13
use allejo\DaPulse\Exceptions\InvalidArraySizeException;
14
use allejo\DaPulse\Objects\ApiObject;
15
16
/**
17
 * This class contains all of the respective functionality for working a board on DaPulse
18
 *
19
 * @api
20
 * @package allejo\DaPulse
21
 * @since   0.1.0
22
 */
23
class PulseBoard extends ApiObject
0 ignored issues
show
Coding Style introduced by
The property $created_at is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $updated_at is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
24
{
25
    /**
26
     * The suffix that is appended to the URL to access functionality for certain objects
27
     *
28
     * @internal
29
     */
30
    const API_PREFIX = "boards";
31
32
    // =================================================================================================================
33
    //   Instance Variables
34
    // =================================================================================================================
35
36
    /**
37
     * The resource's URL.
38
     *
39
     * @var string
40
     */
41
    protected $url;
42
43
    /**
44
     * The board's unique identifier.
45
     *
46
     * @var int
47
     */
48
    protected $id;
49
50
    /**
51
     * The board's name.
52
     *
53
     * @var string
54
     */
55
    protected $name;
56
57
    /**
58
     * The board's description.
59
     *
60
     * @var string
61
     */
62
    protected $description;
63
64
    /**
65
     * The board's visible columns.
66
     *
67
     * @var array
68
     */
69
    protected $columns;
70
71
    /**
72
     * Creation time.
73
     *
74
     * @var \DateTime
75
     */
76
    protected $created_at;
77
78
    /**
79
     * Last update time.
80
     *
81
     * @var \DateTime
82
     */
83
    protected $updated_at;
84
85
    // =================================================================================================================
86
    //   Getter functions
87
    // =================================================================================================================
88
89
    /**
90
     * The resource's URL.
91
     *
92
     * @api
93
     *
94
     * @since  0.1.0
95
     *
96
     * @return string
97
     */
98
    public function getUrl()
99
    {
100
        return $this->url;
101
    }
102
103
    /**
104
     * The board's unique identifier.
105
     *
106
     * @api
107
     *
108
     * @since  0.1.0
109
     *
110
     * @return int
111
     */
112
    public function getId()
113
    {
114
        return $this->id;
115
    }
116
117
    /**
118
     * The board's name.
119
     *
120
     * @api
121
     *
122
     * @since  0.1.0
123
     *
124
     * @return string
125
     */
126
    public function getName()
127
    {
128
        return $this->name;
129
    }
130
131
    /**
132
     * The board's description.
133
     *
134
     * @api
135
     *
136
     * @since  0.1.0
137
     *
138
     * @return string
139
     */
140
    public function getDescription()
141
    {
142
        return $this->description;
143
    }
144
145
    /**
146
     * The board's visible columns.
147
     *
148
     * @api
149
     *
150
     * @since  0.1.0
151
     *
152
     * @return PulseColumn[]
153
     */
154
    public function getColumns()
155
    {
156
        self::lazyInject($this->columns, array(
157
            "board_id" => $this->getId()
158
        ));
159
        self::lazyArray($this->columns, "PulseColumn");
160
161
        return $this->columns;
162
    }
163
164
    /**
165
     * Creation time.
166
     *
167
     * @api
168
     *
169
     * @since  0.1.0
170
     *
171
     * @return \DateTime
172
     */
173
    public function getCreatedAt()
174
    {
175
        self::lazyLoad($this->created_at, "DateTime");
176
177
        return $this->created_at;
178
    }
179
180
    /**
181
     * Last update time.
182
     *
183
     * @api
184
     *
185
     * @since  0.1.0
186
     *
187
     * @return \DateTime
188
     */
189
    public function getUpdatedAt()
190
    {
191
        self::lazyLoad($this->updated_at, "DateTime");
192
193
        return $this->updated_at;
194
    }
195
196
    // =================================================================================================================
197
    //   Subscriber functions
198
    // =================================================================================================================
199
200
    /**
201
     * Get the users who are subscribed to this board
202
     *
203
     * @api
204
     *
205
     * @param  array $params
206
     *
207
     * @since  0.1.0
208
     *
209
     * @return PulseUser[]
210
     */
211
    public function getSubscribers ($params = array())
212
    {
213
        $url = sprintf("%s/%d/subscribers.json", $this::apiEndpoint(), $this->getId());
214
215
        return self::fetchJsonArrayToObjectArray($url, "PulseUser", $params);
216
    }
217
218
    /**
219
     * Subscriber a user to a board
220
     *
221
     * @api
222
     *
223
     * @param int|PulseUser $user_id  The user that will be subscribed to the board
224
     * @param bool|null     $as_admin Set to true if the user will be an admin of the board
225
     *
226
     * @since 0.1.0
227
     */
228
    public function addSubscriber ($user_id, $as_admin = NULL)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $user_id is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The parameter $as_admin is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
229
    {
230
        if ($user_id instanceof PulseUser)
231
        {
232
            $user_id = $user_id->getId();
233
        }
234
235
        $url = sprintf("%s/%d/subscribers.json", self::apiEndpoint(), $this->getId());
236
        $params = array(
237
            "user_id" => $user_id
238
        );
239
240
        self::setIfNotNullOrEmpty($params, "as_admin", $as_admin);
0 ignored issues
show
Documentation introduced by
$as_admin is of type boolean|null, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
241
        self::sendPut($url, $params);
242
    }
243
244
    /**
245
     * Unsubscribe a user from this board
246
     *
247
     * @api
248
     *
249
     * @param int|PulseUser $user_id The user that will be unsubscribed from the board
250
     *
251
     * @since 0.1.0
252
     */
253 View Code Duplication
    public function removeSubscriber ($user_id)
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...
Coding Style Naming introduced by
The parameter $user_id is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
254
    {
255
        if ($user_id instanceof PulseUser)
256
        {
257
            $user_id = $user_id->getId();
258
        }
259
260
        $url = sprintf("%s/%d/subscribers/%d.json", self::apiEndpoint(), $this->getId(), $user_id);
261
262
        self::sendDelete($url);
263
    }
264
265
    // =================================================================================================================
266
    //   Columns functions
267
    // =================================================================================================================
268
269
    /**
270
     * Create a new column for the current board.
271
     *
272
     * If you are creating a status column, use the constants available in the **PulseColumnColorValue** class to match
273
     * the colors. Keep in mind this array cannot have a key higher than 11 nor can it be an associative array. Here's
274
     * an example of how to match statuses with specific colors.
275
     *
276
     * ```php
277
     * $labels = array(
278
     *     PulseColumnColorValue::Orange  => "Working on it",
279
     *     PulseColumnColorValue::L_Green => "Done",
280
     *     PulseColumnColorValue::Red     => "Delayed"
281
     * );
282
     * ```
283
     *
284
     * @api
285
     *
286
     * @param string $title  The title of the column. This title will automatically be "slugified" and become the ID
287
     *                       of the column.
288
     * @param string $type   The type of value that this column will use. Either use the available constants in the
289
     *                       PulseColumn class or use the following strings: "date", "person", "status", "text".
290
     * @param array  $labels If the column type will be "status," then this array will be the values for each of the
291
     *                       colors.
292
     *
293
     * @see PulseColumn::Date   PulseColumn::Date
294
     * @see PulseColumn::Person PulseColumn::Person
295
     * @see PulseColumn::Status PulseColumn::Status
296
     * @see PulseColumn::Text   PulseColumn::Text
297
     * @see PulseColumnColorValue::Orange  PulseColumnColorValue::Orange
298
     * @see PulseColumnColorValue::L_Green PulseColumnColorValue::L_Green
299
     * @see PulseColumnColorValue::Red     PulseColumnColorValue::Red
300
     * @see PulseColumnColorValue::Blue    PulseColumnColorValue::Blue
301
     * @see PulseColumnColorValue::Purple  PulseColumnColorValue::Purple
302
     * @see PulseColumnColorValue::Grey    PulseColumnColorValue::Grey
303
     * @see PulseColumnColorValue::Green   PulseColumnColorValue::Green
304
     * @see PulseColumnColorValue::L_Blue  PulseColumnColorValue::L_Blue
305
     * @see PulseColumnColorValue::Gold    PulseColumnColorValue::Gold
306
     * @see PulseColumnColorValue::Yellow  PulseColumnColorValue::Yellow
307
     * @see PulseColumnColorValue::Black   PulseColumnColorValue::Black
308
     *
309
     * @since 0.1.0
310
     *
311
     * @throws ArgumentMismatchException Status definitions were defined yet the type of the column was not a status
312
     *                                   type column
313
     * @throws InvalidArraySizeException The array containing the value of statuses has a key larger than the
314
     *                                   supported 10 indices
315
     *
316
     * @return $this This instance will be updated to have updated information to reflect the new column that was
317
     *               created
318
     */
319
    public function createColumn ($title, $type, $labels = array())
320
    {
321
        if ($type !== PulseColumn::Status && !empty($labels))
322
        {
323
            throw new ArgumentMismatchException("No color definitions are required for a non-color column.");
324
        }
325
326
        if ($type === PulseColumn::Status && count($labels) > 0 && max(array_keys($labels)) > 10)
327
        {
328
            throw new InvalidArraySizeException("The range of status can only be from 0-10.");
329
        }
330
331
        $url        = sprintf("%s/%d/columns.json", self::apiEndpoint(), $this->getId());
332
        $postParams = array(
333
            "title" => $title,
334
            "type"  => $type
335
        );
336
337
        self::setIfNotNullOrEmpty($postParams, "labels", $labels);
0 ignored issues
show
Documentation introduced by
$labels is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
338
339
        $this->jsonResponse = self::sendPost($url, $postParams);
0 ignored issues
show
Documentation Bug introduced by
It seems like self::sendPost($url, $postParams) of type * is incompatible with the declared type array of property $jsonResponse.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
340
        $this->assignResults();
341
342
        return $this;
343
    }
344
345
    // =================================================================================================================
346
    //   Group functions
347
    // =================================================================================================================
348
349
    /**
350
     * Get all of the groups belonging to a board.
351
     *
352
     * A group is defined as the colorful headers that split up pulses into categories.
353
     *
354
     * @api
355
     *
356
     * @param bool $show_archived Set to true if you would like to get archived groups in a board as well
0 ignored issues
show
Documentation introduced by
Should the type for parameter $show_archived not be boolean|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
357
     *
358
     * @since 0.1.0
359
     *
360
     * @return PulseGroup[]
361
     */
362
    public function getGroups ($show_archived = NULL)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $show_archived is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
363
    {
364
        $url = sprintf("%s/%d/groups.json", self::apiEndpoint(), $this->getId());
365
        $params = array();
366
367
        self::setIfNotNullOrEmpty($params, "show_archived", $show_archived);
0 ignored issues
show
Documentation introduced by
$show_archived is of type boolean|null, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
368
369
        return self::fetchJsonArrayToObjectArray($url, "PulseGroup", $params);
370
    }
371
372
    /**
373
     * Create a new group in a board
374
     *
375
     * @api
376
     *
377
     * @param  string $title The title of the board
378
     *
379
     * @since  0.1.0
380
     *
381
     * @return PulseGroup
382
     */
383
    public function createGroup ($title)
384
    {
385
        $url        = sprintf("%s/%s/groups.json", self::apiEndpoint(), $this->getId());
386
        $postParams = array("title" => $title);
387
388
        // The API doesn't return the board ID, so since we have access to it here: set it manually
389
        $groupResult             = self::sendPost($url, $postParams);
390
        $groupResult["board_id"] = $this->getId();
391
392
        return (new PulseGroup($groupResult));
393
    }
394
395
    /**
396
     * Delete a group from a board
397
     *
398
     * @api
399
     *
400
     * @param string $group_id The group ID to be deleted
401
     *
402
     * @since 0.1.0
403
     */
404
    public function deleteGroup ($group_id)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $group_id is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
405
    {
406
        $url = sprintf("%s/%d/groups/%s.json", self::apiEndpoint(), $this->getId(), $group_id);
407
408
        self::sendDelete($url);
409
    }
410
411
    // =================================================================================================================
412
    //   Pulse functions
413
    // =================================================================================================================
414
415
    /**
416
     * @return Pulse[]
417
     */
418
    public function getPulses ()
419
    {
420
        $url = sprintf("%s/%d/pulses.json", self::apiEndpoint(), $this->getId());
421
        $data = self::sendGet($url);
422
        $pulses = array();
423
424
        foreach ($data as $entry)
425
        {
426
            $this->pulseInjection($entry);
427
428
            $pulses[] = new Pulse($entry["pulse"]);
429
        }
430
431
        return $pulses;
432
    }
433
434
    public function createPulse ($name, $owner, $group_id = null)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $group_id is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
435
    {
436
        $url = sprintf("%s/%d/pulses.json", self::apiEndpoint(), $this->getId());
437
        $postParams = array(
438
            "user_id" => $owner,
439
            "pulse" => array(
440
                "name" => $name
441
            )
442
        );
443
444
        self::setIfNotNullOrEmpty($postParams, "group_id", $group_id);
445
446
        $result = self::sendPost($url, $postParams);
447
        $this->pulseInjection($result);
448
449
        return (new Pulse($result["pulse"]));
450
    }
451
452 View Code Duplication
    private function pulseInjection (&$result)
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...
453
    {
454
        // Inject some information so a Pulse object can survive on its own
455
        $result["pulse"]["group_id"] = $result["board_meta"]["group_id"];
456
        $result["pulse"]["column_structure"] = $this->getColumns();
457
        $result["pulse"]["raw_column_values"] = $result["column_values"];
458
    }
459
460
    // =================================================================================================================
461
    //   Board functions
462
    // =================================================================================================================
463
464 View Code Duplication
    public function archiveBoard ()
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...
465
    {
466
        $this->checkInvalid();
467
468
        $url = sprintf("%s/%s.json", self::apiEndpoint(), $this->getId());
469
        self::sendDelete($url);
470
471
        $this->deletedObject = true;
472
    }
473
474
    public static function createBoard ($name, $user_id, $description = NULL)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $user_id is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
475
    {
476
        $url        = sprintf("%s.json", self::apiEndpoint());
477
        $postParams = array(
478
            "user_id" => $user_id,
479
            "name"    => $name
480
        );
481
482
        self::setIfNotNullOrEmpty($postParams, "description", $description);
483
484
        $boardResult = self::sendPost($url, $postParams);
485
486
        return (new PulseBoard($boardResult));
487
    }
488
489
    public static function getBoards ($params = array())
490
    {
491
        $url = sprintf("%s.json", self::apiEndpoint());
492
493
        return self::fetchJsonArrayToObjectArray($url, "PulseBoard", $params);
494
    }
495
}