Completed
Pull Request — master (#16)
by
unknown
13:26 queued 04:20
created

Labels::removeById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Trello\Api\Card;
4
5
use Trello\Api\AbstractApi;
6
use Trello\Exception\InvalidArgumentException;
7
8
/**
9
 * Trello Card Labels API
10
 * @link https://trello.com/docs/api/card
11
 *
12
 * Fully implemented.
13
 */
14
class Labels extends AbstractApi
15
{
16
    protected $path = 'cards/#id#/labels';
17
18
    /**
19
     * Set a given card's labels
20
     * @link https://trello.com/docs/api/card/#put-1-cards-card-id-or-shortlink-labels
21
     *
22
     * @param string $id     the card's id or short link
23
     * @param array  $labels the labels
24
     *
25
     * @return array card info
26
     *
27
     * @throws InvalidArgumentException If a label does not exist
28
     */
29 2
    public function set($id, array $labels)
30
    {
31 2
        foreach ($labels as $label) {
32 2
            if (!in_array($label, array('all', 'green', 'yellow', 'orange', 'red', 'purple', 'blue'))) {
33 1
                throw new InvalidArgumentException(sprintf('Label "%s" does not exist.', $label));
34
            }
35 1
        }
36
37 1
        $labels = implode(',', $labels);
38
39 1
        return $this->put($this->getPath($id), array('value' => $labels));
40
    }
41
    
42
        /**
43
     * Set a given card's label by labelId
44
     * @link https://trello.com/docs/api/card/#put-1-cards-card-id-or-shortlink-labels
45
     *
46
     * @param string $id     the card's id or short link
47
     * @param string $labelId the label id
48
     *
49
     * @return array card info
50
     *
51
     * @throws InvalidArgumentException If a label does not exist
52
     */
53 2
    public function setById($id, $labelId)
54
    {
55 2
        //first set the right path based on the documentation;
56 1
        $this->path = 'cards/#id#/idLabels';
57
58
        return $this->post($this->getPath($id), array('value' => $labelId));
59 1
    }
60
61
    /**
62
     * Remove a given label from a given card
63
     * @link https://trello.com/docs/api/card/#delete-1-cards-card-id-or-shortlink-labels-color
64
     *
65
     * @param string $id    the card's id or short link
66
     * @param string $label the label to remove
67
     *
68
     * @return array card info
69
     *
70
     * @throws InvalidArgumentException If a label does not exist
71
     */
72 View Code Duplication
    public function remove($id, $label)
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...
73
    {
74
        if (!in_array($label, array('green', 'yellow', 'orange', 'red', 'purple', 'blue'))) {
75
            throw new InvalidArgumentException(sprintf('Label "%s" does not exist.', $label));
76
        }
77
78
        return $this->delete($this->getPath($id).'/'.rawurlencode($label));
79
    }
80
    
81
   /**
82
     * Remove a given label from a given card based on the label id
83
     * @link https://developers.trello.com/advanced-reference/card#delete-1-cards-card-id-or-shortlink-idlabels-idlabel
84
     *
85
     * @param string $id    the card's id or short link
86
     * @param string $labelId the label id to remove
87
     *
88
     * @return array card info
89
     *
90
     * @throws InvalidArgumentException If a label does not exist
91
     */
92
    public function removeById($id, $labelId)
93
    {
94
        $this->path = 'cards/#id#/idLabels';
95
        return $this->delete($this->getPath($id).'/'.rawurlencode($labelId));
96
    }
97
}
98