Completed
Pull Request — master (#16)
by
unknown
05:32
created

Members::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Trello\Api\Board;
4
5
use Trello\Api\AbstractApi;
6
use Trello\Api\Member;
7
use Trello\Exception\InvalidArgumentException;
8
9
/**
10
 * Trello Board Members API
11
 * @link https://trello.com/docs/api/board
12
 *
13
 * Fully implemented.
14
 */
15
class Members extends AbstractApi
16
{
17
    /**
18
     * Base path of board members api
19
     * @var string
20
     */
21
    protected $path = 'boards/#id#/members';
22
23
    /**
24
     * Get a given board's members
25
     * @link https://trello.com/docs/api/board/#get-1-boards-board-id-members
26
     *
27
     * @param string $id     the board's id
28
     * @param array  $params optional parameters
29
     *
30
     * @return array
31
     */
32 1
    public function all($id, array $params = array())
33
    {
34 1
        return $this->get($this->getPath($id), $params);
35
    }
36
37
    /**
38
     * Remove a given member from a given board
39
     * @link https://trello.com/docs/api/board/#get-1-boards-board-id-members
40
     *
41
     * @param string $id       the board's id
42
     * @param string $memberId the member's id
43
     *
44
     * @return array
45
     */
46 1
    public function remove($id, $memberId)
47
    {
48 1
        return $this->delete($this->getPath($id).'/'.$memberId);
49
    }
50
    
51
    /**
52
     * Add a given member from a given board
53
     * @link https://trello.com/docs/api/board/#get-1-boards-board-id-members
54
     *
55
     * @param string $id       the board's id
56
     * @param string $memberId the member's id
57
     *
58
     * @return array
59
     */
60 3
    public function add($id, $memberId,  $role = 'normal')
61
    {
62 3
        $params = array(
63 3
            'idMember'    => $memberId,
64
            'type'     => $role,
65 3
        );
66
        
67
        return $this->put($this->getPath($id).'/'.$memberId, $params);
68
    }
69
70
    /**
71
     * Filter members related to a given board
72
     * @link https://trello.com/docs/api/board/#get-1-boards-board-id-members-filter
73
     *
74
     * @param string       $id     the board's id
75
     * @param string|array $filter array of / one of 'none', 'normal', 'admins', 'owners', 'all'
76
     *
77
     * @return array
78 1
     */
79
    public function filter($id, $filter = 'all')
80 1
    {
81
        $allowed = array('none', 'normal', 'admins', 'owners', 'all');
82
        $filters = $this->validateAllowedParameters($allowed, $filter, 'filter');
83
84
        return $this->get($this->getPath($id).'/'.implode(',', $filters));
85
    }
86
87
    /**
88
     * Get a member's cards related to a given board
89
     * @link https://trello.com/docs/api/board/#get-1-boards-board-id-members-filter
90
     *
91
     * @param string $id       the board's id
92
     * @param string $memberId the member's id
93
     * @param array  $params   optional parameters
94 2
     *
95
     * @return array
96 2
     */
97
    public function cards($id, $memberId, array $params = array())
98 2
    {
99 1
        return $this->get($this->getPath($id).'/'.rawurlencode($memberId).'/cards', $params);
100 1
    }
101 1
102 1
    /**
103
     * Add member to a given board
104
     * @link https://trello.com/docs/api/board/#put-1-boards-board-id-members
105
     *
106 1
     * @param string $id       the board's id
107 1
     * @param string $email    the member's email address
108 1
     * @param string $fullName the member's full name
109 1
     * @param string $role     one of 'normal', 'observer' or 'admin'
110
     *
111 1
     * @return array
112
     */
113 View Code Duplication
    public function invite($id, $email, $fullName, $role = 'normal')
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...
114
    {
115
        $roles = array('normal', 'observer', 'admin');
116
117
        if (!in_array($role, $roles)) {
118
            throw new InvalidArgumentException(sprintf(
119
                'The "role" parameter must be one of "%s".',
120
                implode(", ", $roles)
121
            ));
122
        }
123 1
124
        $params = array(
125 1
            'email'    => $email,
126
            'fullName' => $fullName,
127
            'type'     => $role,
128
        );
129
130
        return $this->put($this->getPath($id), $params);
131
    }
132
133
    /**
134
     * Get members invited to a given board
135
     * @link https://trello.com/docs/api/board/#get-1-boards-board-id-membersinvited
136
     *
137 2
     * @param string $id     the board's id
138
     * @param array  $params optional parameters
139 2
     *
140
     * @return array
141 1
     */
142
    public function getInvitedMembers($id, array $params = array())
143
    {
144
        return $this->get($this->getPath($id).'Invited', $params);
145
    }
146
147
    /**
148
     * Get a field related to a member invited to a given board
149
     * @link https://trello.com/docs/api/board/#get-1-boards-board-id-membersinvited-field
150
     *
151
     * @param string $id    the board's id
152
     * @param string $field the member's field name
153 2
     *
154
     * @return array
155 2
     */
156
    public function getInvitedMembersField($id, $field)
157 2
    {
158 1
        $this->validateAllowedParameters(Member::$fields, $field, 'field');
159 1
160 1
        return $this->get($this->getPath($id).'Invited/'.rawurlencode($field));
161 1
    }
162
163
    /**
164
     * Set the role of a user or an organization on a given board
165 1
     * @link https://trello.com/docs/api/board/index.html#put-1-boards-board-id-members-idmember
166 1
     *
167 1
     * @param string $id                   the board's id
168
     * @param string $memberOrOrganization the member's id, user name or an organization name
169 1
     *
170
     * @return array
171
     */
172 View Code Duplication
    public function setRole($id, $memberOrOrganization, $role)
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...
173
    {
174
        $roles = array('normal', 'observer', 'admin');
175
176
        if (!in_array($role, $roles)) {
177
            throw new InvalidArgumentException(sprintf(
178
                'The "role" parameter must be one of "%s".',
179
                implode(", ", $roles)
180
            ));
181
        }
182
183
        $params = array(
184
            'idMember' => $memberOrOrganization,
185
            'type' => $role,
186
        );
187
188
        return $this->post($this->getPath($id).'/'.rawurlencode($memberOrOrganization), $params);
189
    }
190
}
191