Completed
Push — master ( 864c1e...2ce755 )
by Philip
07:20 queued 05:26
created

Client::issuesByFilter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Manavo\DoneDone;
4
5
class Client
6
{
7
8
    /**
9
     * @var string
10
     */
11
    private $teamName;
12
13
    /**
14
     * @var string
15
     */
16
    private $username;
17
18
    /**
19
     * @var string
20
     */
21
    private $passwordOrApiToken;
22
23
    /**
24
     * @var \GuzzleHttp\Client
25
     */
26
    private $guzzleClient;
27
28
    /**
29
     * @param string $teamName
30
     * @param string $username
31
     * @param string $passwordOrApiToken
32
     */
33 69
    public function __construct($teamName, $username, $passwordOrApiToken)
34
    {
35 69
        $this->teamName = $teamName;
36 69
        $this->username = $username;
37 69
        $this->passwordOrApiToken = $passwordOrApiToken;
38
39 69
        $this->setClient(new \GuzzleHttp\Client([
40
            'defaults' => [
41 69
                'auth' => [$username, $passwordOrApiToken]
42 69
            ]
43 69
        ]));
44 69
    }
45
46
    /**
47
     * Get a list of all priority levels
48
     *
49
     * @return array
50
     */
51
    public function priorityLevels()
52
    {
53
        return $this->get('priority_levels');
54
    }
55
56
    /**
57
     * Get a list of all projects
58
     *
59
     * @return array
60
     */
61
    public function projects()
62
    {
63
        return $this->get('projects');
64
    }
65
66
    /**
67
     * Get a list of all companies
68
     *
69
     * @return array
70
     */
71
    public function companies()
72
    {
73
        return $this->get('companies');
74
    }
75
76
    /**
77
     * Get all the issues
78
     *
79
     * @return array
80
     */
81
    public function issues()
82
    {
83
        return $this->get('issues/all');
84
    }
85
86
    /**
87
     * Get all the active issues
88
     *
89
     * @return array
90
     */
91
    public function activeIssues()
92
    {
93
        return $this->get('issues/all_active');
94
    }
95
96
    /**
97
     * Get all the closed and fixed issues
98
     *
99
     * @return array
100
     */
101
    public function closedAndFixedIssues()
102
    {
103
        return $this->get('issues/all_closed_and_fixed');
104
    }
105
106
    /**
107
     * Get all the issues by a filter
108
     *
109
     * @param int $filter
110
     *
111
     * @return array
112
     */
113
    public function issuesByFilter($filter)
114
    {
115
        return $this->get('issues/by_global_custom_filter/' . $filter);
116
    }
117
118
    /**
119
     * Get all the issues which are waiting on you
120
     *
121
     * @return array
122
     */
123
    public function issuesWaitingOnYou()
124
    {
125
        return $this->get('issues/waiting_on_you');
126
    }
127
128
    /**
129
     * Get all the issues which are waiting on them
130
     *
131
     * @return array
132
     */
133
    public function issuesWaitingOnThem()
134
    {
135
        return $this->get('issues/waiting_on_them');
136
    }
137
138
    /**
139
     * Get a list of all global filters
140
     *
141
     * @return array
142
     */
143
    public function globalFilters()
144
    {
145
        return $this->get('global_custom_filters');
146
    }
147
148
    /**
149
     * Get a list of all issue creation types
150
     *
151
     * @return array
152
     */
153
    public function issueCreationTypes()
154
    {
155
        return $this->get('issue_creation_types');
156
    }
157
158
    /**
159
     * Get a list of all issue sort types
160
     *
161
     * @return array
162
     */
163
    public function issueSortTypes()
164
    {
165
        return $this->get('issue_sort_types');
166
    }
167
168
    /**
169
     * @param int $id
170
     *
171
     * @return Project
172
     */
173 60
    public function project($id)
174
    {
175 60
        return new Project($this, $id);
176
    }
177
178
    /**
179
     * @param int $id
180
     *
181
     * @return Company
182
     */
183 6
    public function company($id)
184
    {
185 6
        return new Company($this, $id);
186
    }
187
188
    /**
189
     * Create a new company
190
     *
191
     * @param Company $company
192
     *
193
     * @return array
194
     */
195
    public function createCompany($company)
196
    {
197
        return $this->post('companies', $company->toArray());
198
    }
199
200
    /**
201
     * Override the default Guzzle client
202
     *
203
     * @param \GuzzleHttp\Client $client
204
     */
205 69
    public function setClient($client)
206
    {
207 69
        $this->guzzleClient = $client;
208 69
    }
209
210
    /**
211
     * Generate the full URL from the specified endpoint
212
     *
213
     * @param string $endpoint
214
     *
215
     * @return string
216
     */
217 60
    private function getUrl($endpoint)
218
    {
219 60
        return sprintf(
220 60
            'https://%s.mydonedone.com/issuetracker/api/v2/%s.json',
221 60
            $this->teamName,
222
            $endpoint
223 60
        );
224
    }
225
226
    /**
227
     * @param string $endpoint
228
     * @param array  $data
229
     *
230
     * @return array
231
     */
232 33
    public function get($endpoint, $data = [])
233
    {
234 33
        $url = $this->getUrl($endpoint);
235
236 33
        $response = $this->guzzleClient->get($url, [
237
            'query' => $data
238 33
        ]);
239
240 33
        if ($response) {
241
            return json_decode($response->getBody(), true);
242
        } else {
243 33
            return null;
244
        }
245
    }
246
247
    /**
248
     * @param string $endpoint
249
     * @param array  $data
250
     *
251
     * @return array
252
     */
253 9
    public function post($endpoint, $data = [])
254
    {
255 9
        $url = $this->getUrl($endpoint);
256
257 9
        $response = $this->guzzleClient->post($url, [
258
            'body' => $data
259 9
        ]);
260
261 9
        if ($response) {
262
            return json_decode($response->getBody(), true);
263
        } else {
264 9
            return null;
265
        }
266
    }
267
268
    /**
269
     * @param string $endpoint
270
     * @param array  $data
271
     *
272
     * @return array
273
     */
274 18
    public function put($endpoint, $data = [])
275
    {
276 18
        $url = $this->getUrl($endpoint);
277
278 18
        $response = $this->guzzleClient->put($url, [
279
            'body' => $data
280 18
        ]);
281
282 18
        if ($response) {
283
            return json_decode($response->getBody(), true);
284
        } else {
285 18
            return null;
286
        }
287
    }
288
289
}
290