Completed
Push — master ( 6e5a1f...732aa4 )
by Vladimir
03:17
created

PulseBoard::getPulses()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 0
loc 15
rs 9.4286
cc 2
eloc 8
nc 2
nop 0
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
use allejo\DaPulse\Utilities\ArrayUtilities;
16
17
/**
18
 * This class contains all of the respective functionality for working a board on DaPulse
19
 *
20
 * @api
21
 * @package allejo\DaPulse
22
 * @since   0.1.0
23
 */
24
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...
25
{
26
    /**
27
     * The suffix that is appended to the URL to access functionality for certain objects
28
     *
29
     * @internal
30
     */
31
    const API_PREFIX = "boards";
32
33
    // ================================================================================================================
34
    //   Instance Variables
35
    // ================================================================================================================
36
37
    /**
38
     * The resource's URL.
39
     *
40
     * @var string
41
     */
42
    protected $url;
43
44
    /**
45
     * The board's unique identifier.
46
     *
47
     * @var int
48
     */
49
    protected $id;
50
51
    /**
52
     * The board's name.
53
     *
54
     * @var string
55
     */
56
    protected $name;
57
58
    /**
59
     * The board's description.
60
     *
61
     * @var string
62
     */
63
    protected $description;
64
65
    /**
66
     * The board's visible columns.
67
     *
68
     * @var array
69
     */
70
    protected $columns;
71
72
    /**
73
     * The board's visible groups.
74
     *
75
     * @var array
76
     */
77
    protected $groups;
78
79
    /**
80
     * Creation time.
81
     *
82
     * @var \DateTime
83
     */
84
    protected $created_at;
85
86
    /**
87
     * Last update time.
88
     *
89
     * @var \DateTime
90
     */
91
    protected $updated_at;
92
93
    /**
94
     * Whether or not groups have been fetched. Group data comes from both a unique API call and from the initial call
95
     * of getting the board data, so this data is merged; this boolean is to avoid fetching this data twice.
96
     *
97
     * @var bool
98
     */
99
    private $groupsFetched = false;
100
101
    // ================================================================================================================
102
    //   Getter functions
103
    // ================================================================================================================
104
105
    /**
106
     * The resource's URL.
107
     *
108
     * @api
109
     * @since  0.1.0
110
     * @return string
111
     */
112
    public function getUrl()
113
    {
114
        return $this->url;
115
    }
116
117
    /**
118
     * The board's unique identifier.
119
     *
120
     * @api
121
     * @since  0.1.0
122
     * @return int
123
     */
124
    public function getId()
125
    {
126
        return $this->id;
127
    }
128
129
    /**
130
     * The board's name.
131
     *
132
     * @api
133
     * @since  0.1.0
134
     * @return string
135
     */
136
    public function getName()
137
    {
138
        return $this->name;
139
    }
140
141
    /**
142
     * The board's description.
143
     *
144
     * @api
145
     * @since  0.1.0
146
     * @return string
147
     */
148
    public function getDescription()
149
    {
150
        return $this->description;
151
    }
152
153
    /**
154
     * The board's visible columns.
155
     *
156
     * @api
157
     * @since  0.1.0
158
     * @return PulseColumn[]
159
     */
160
    public function getColumns()
161
    {
162
        self::lazyInject($this->columns, array(
163
            "board_id" => $this->getId()
164
        ));
165
        self::lazyArray($this->columns, "PulseColumn");
166
167
        return $this->columns;
168
    }
169
170
    /**
171
     * Creation time.
172
     *
173
     * @api
174
     * @since  0.1.0
175
     * @return \DateTime
176
     */
177
    public function getCreatedAt()
178
    {
179
        self::lazyLoad($this->created_at, "DateTime");
180
181
        return $this->created_at;
182
    }
183
184
    /**
185
     * Last update time.
186
     *
187
     * @api
188
     * @since  0.1.0
189
     * @return \DateTime
190
     */
191
    public function getUpdatedAt()
192
    {
193
        self::lazyLoad($this->updated_at, "DateTime");
194
195
        return $this->updated_at;
196
    }
197
198
    // ================================================================================================================
199
    //   Columns functions
200
    // ================================================================================================================
201
202
    /**
203
     * Create a new column for the current board.
204
     *
205
     * If you are creating a status column, use the constants available in the **PulseColumnColorValue** class to match the
206
     * colors. Keep in mind this array cannot have a key higher than 11 nor can it be an associative array. Here's an
207
     * example of how to match statuses with specific colors.
208
     *
209
     * ```php
210
     * $labels = array(
211
     *     PulseColumnColorValue::Orange  => "Working on it",
212
     *     PulseColumnColorValue::L_Green => "Done",
213
     *     PulseColumnColorValue::Red     => "Delayed"
214
     * );
215
     * ```
216
     *
217
     * @api
218
     *
219
     * @param string $title  The title of the column. This title will automatically be "slugified" and become the ID
220
     *                       of the column.
221
     * @param string $type   The type of value that this column will use. Either use the available constants in the
222
     *                       PulseColumn class or use the following strings: "date", "person", "status", "text".
223
     * @param array  $labels If the column type will be "status," then this array will be the values for each of the
224
     *                       colors.
225
     *
226
     * @see PulseColumn::Date   PulseColumn::Date
227
     * @see PulseColumn::Person PulseColumn::Person
228
     * @see PulseColumn::Status PulseColumn::Status
229
     * @see PulseColumn::Text   PulseColumn::Text
230
     * @see PulseColumnColorValue::Orange  PulseColumnColorValue::Orange
231
     * @see PulseColumnColorValue::L_Green PulseColumnColorValue::L_Green
232
     * @see PulseColumnColorValue::Red     PulseColumnColorValue::Red
233
     * @see PulseColumnColorValue::Blue    PulseColumnColorValue::Blue
234
     * @see PulseColumnColorValue::Purple  PulseColumnColorValue::Purple
235
     * @see PulseColumnColorValue::Grey    PulseColumnColorValue::Grey
236
     * @see PulseColumnColorValue::Green   PulseColumnColorValue::Green
237
     * @see PulseColumnColorValue::L_Blue  PulseColumnColorValue::L_Blue
238
     * @see PulseColumnColorValue::Gold    PulseColumnColorValue::Gold
239
     * @see PulseColumnColorValue::Yellow  PulseColumnColorValue::Yellow
240
     * @see PulseColumnColorValue::Black   PulseColumnColorValue::Black
241
     *
242
     * @since 0.1.0
243
     *
244
     * @throws ArgumentMismatchException Status definitions were defined yet the type of the column was not a status
245
     *                                   type column
246
     * @throws InvalidArraySizeException The array containing the value of statuses has a key larger than the
247
     *                                   supported 10 indices
248
     *
249
     * @return $this This instance will be updated to have updated information to reflect the new column that was
250
     *               created
251
     */
252
    public function createColumn ($title, $type, $labels = array())
253
    {
254
        if ($type !== PulseColumn::Status && !empty($labels))
255
        {
256
            throw new ArgumentMismatchException("No color definitions are required for a non-color column.");
257
        }
258
259
        if ($type === PulseColumn::Status && count($labels) > 0 && max(array_keys($labels)) > 10)
260
        {
261
            throw new InvalidArraySizeException("The range of status can only be from 0-10.");
262
        }
263
264
        $url        = sprintf("%s/%d/columns.json", parent::apiEndpoint(), $this->getId());
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (apiEndpoint() instead of createColumn()). Are you sure this is correct? If so, you might want to change this to $this->apiEndpoint().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
265
        $postParams = array(
266
            "title" => $title,
267
            "type"  => $type
268
        );
269
270
        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...
271
272
        $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...
273
        $this->assignResults();
274
275
        return $this;
276
    }
277
278
    // ================================================================================================================
279
    //   Group functions
280
    // ================================================================================================================
281
282
    /**
283
     * Get all of the groups belonging to a board.
284
     *
285
     * A group is defined as the colorful headers that split up pulses into categories.
286
     *
287
     * @api
288
     *
289
     * @param bool $showArchived Set to true if you would like to get archived groups in a board as well
290
     *
291
     * @since 0.1.0
292
     *
293
     * @return PulseGroup[]
294
     */
295
    public function getGroups ($showArchived = false)
296
    {
297
        if (!$this->groupsFetched)
298
        {
299
            $fetchedGroups = $this->fetchGroups($showArchived);
300
301
            $this->groups = ArrayUtilities::array_merge_recursive_distinct($this->groups, $fetchedGroups);
302
            $this->groupsFetched = true;
303
        }
304
305
        self::lazyArray($this->groups, "PulseGroup");
306
307
        return $this->groups;
308
    }
309
310
    public function createGroup ($title)
311
    {
312
        $url        = sprintf("%s/%s/groups.json", parent::apiEndpoint(), $this->getId());
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (apiEndpoint() instead of createGroup()). Are you sure this is correct? If so, you might want to change this to $this->apiEndpoint().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
313
        $postParams = array("title" => $title);
314
315
        // The API doesn't return the board ID, so since we have access to it here: set it manually
316
        $groupResult             = self::sendPost($url, $postParams);
317
        $groupResult["board_id"] = $this->getId();
318
319
        return (new PulseGroup($groupResult));
320
    }
321
322
    private function fetchGroups ($showArchived)
323
    {
324
        $url = sprintf("%s/%s/groups.json", parent::apiEndpoint(), $this->getId());
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (apiEndpoint() instead of fetchGroups()). Are you sure this is correct? If so, you might want to change this to $this->apiEndpoint().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
325
326
        $this->groupsFetched = true;
327
328
        return self::sendGet($url, array("show_archived" => $showArchived));
329
    }
330
331
    // ================================================================================================================
332
    //   Pulse functions
333
    // ================================================================================================================
334
335
    /**
336
     * @return Pulse[]
337
     */
338
    public function getPulses ()
339
    {
340
        $url = sprintf("%s/%d/pulses.json", parent::apiEndpoint(), $this->getId());
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (apiEndpoint() instead of getPulses()). Are you sure this is correct? If so, you might want to change this to $this->apiEndpoint().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
341
        $data = self::sendGet($url);
342
        $pulses = array();
343
344
        foreach ($data as $entry)
345
        {
346
            $this->pulseInjection($entry);
347
348
            $pulses[] = new Pulse($entry["pulse"]);
349
        }
350
351
        return $pulses;
352
    }
353
354
    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...
355
    {
356
        $url = sprintf("%s/%d/pulses.json", parent::apiEndpoint(), $this->getId());
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (apiEndpoint() instead of createPulse()). Are you sure this is correct? If so, you might want to change this to $this->apiEndpoint().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
357
        $postParams = array(
358
            "user_id" => $owner,
359
            "pulse" => array(
360
                "name" => $name
361
            )
362
        );
363
364
        self::setIfNotNullOrEmpty($postParams, "group_id", $group_id);
365
366
        $result = self::sendPost($url, $postParams);
367
        $this->pulseInjection($result);
368
369
        return (new Pulse($result["pulse"]));
370
    }
371
372 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...
373
    {
374
        // Inject some information so a Pulse object can survive on its own
375
        $result["pulse"]["group_id"] = $result["board_meta"]["group_id"];
376
        $result["pulse"]["column_structure"] = $this->getColumns();
377
        $result["pulse"]["raw_column_values"] = $result["column_values"];
378
    }
379
380
    // ================================================================================================================
381
    //   Board functions
382
    // ================================================================================================================
383
384 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...
385
    {
386
        $this->checkInvalid();
387
388
        $url = sprintf("%s/%s.json", parent::apiEndpoint(), $this->getId());
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (apiEndpoint() instead of archiveBoard()). Are you sure this is correct? If so, you might want to change this to $this->apiEndpoint().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
389
        self::sendDelete($url);
390
391
        $this->deletedObject = true;
392
    }
393
394
    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...
395
    {
396
        $url        = sprintf("%s.json", parent::apiEndpoint());
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (apiEndpoint() instead of createBoard()). Are you sure this is correct? If so, you might want to change this to $this->apiEndpoint().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
397
        $postParams = array(
398
            "user_id" => $user_id,
399
            "name"    => $name
400
        );
401
402
        self::setIfNotNullOrEmpty($postParams, "description", $description);
403
404
        $boardResult = self::sendPost($url, $postParams);
405
406
        return (new PulseBoard($boardResult));
407
    }
408
409
    public static function getBoards ($params = array())
410
    {
411
        $url = sprintf("%s.json", parent::apiEndpoint());
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (apiEndpoint() instead of getBoards()). Are you sure this is correct? If so, you might want to change this to $this->apiEndpoint().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
412
413
        return parent::fetchJsonArrayToObjectArray($url, "PulseBoard", $params);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (fetchJsonArrayToObjectArray() instead of getBoards()). Are you sure this is correct? If so, you might want to change this to $this->fetchJsonArrayToObjectArray().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
414
    }
415
}