Completed
Push — master ( 569933...98080f )
by Philip
03:39
created

Client::priorityLevels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 108
    public function __construct($teamName, $username, $passwordOrApiToken)
34
    {
35 108
        $this->teamName = $teamName;
36 108
        $this->username = $username;
37 108
        $this->passwordOrApiToken = $passwordOrApiToken;
38
39 108
        $this->setClient(new \GuzzleHttp\Client([
40
            'defaults' => [
41 108
                'auth' => [$username, $passwordOrApiToken]
42 108
            ]
43 108
        ]));
44 108
    }
45
46
    /**
47
     * Get a list of all priority levels
48
     *
49
     * @return array
50
     */
51 3
    public function priorityLevels()
52
    {
53 3
        return $this->get('priority_levels');
54
    }
55
56
    /**
57
     * Get a list of all projects
58
     *
59
     * @return array
60
     */
61 3
    public function projects()
62
    {
63 3
        return $this->get('projects');
64
    }
65
66
    /**
67
     * Get a list of all companies
68
     *
69
     * @return array
70
     */
71 3
    public function companies()
72
    {
73 3
        return $this->get('companies');
74
    }
75
76
    /**
77
     * Get all the issues
78
     *
79
     * @return array
80
     */
81 3
    public function issues()
82
    {
83 3
        return $this->get('issues/all');
84
    }
85
86
    /**
87
     * Get all the active issues
88
     *
89
     * @return array
90
     */
91 3
    public function activeIssues()
92
    {
93 3
        return $this->get('issues/all_active');
94
    }
95
96
    /**
97
     * Get all the closed and fixed issues
98
     *
99
     * @return array
100
     */
101 3
    public function closedAndFixedIssues()
102
    {
103 3
        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 3
    public function issuesByFilter($filter)
114
    {
115 3
        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 3
    public function issuesWaitingOnYou()
124
    {
125 3
        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 3
    public function issuesWaitingOnThem()
134
    {
135 3
        return $this->get('issues/waiting_on_them');
136
    }
137
138
    /**
139
     * Get a list of all global filters
140
     *
141
     * @return array
142
     */
143 3
    public function globalFilters()
144
    {
145 3
        return $this->get('global_custom_filters');
146
    }
147
148
    /**
149
     * Get a list of all issue creation types
150
     *
151
     * @return array
152
     */
153 3
    public function issueCreationTypes()
154
    {
155 3
        return $this->get('issue_creation_types');
156
    }
157
158
    /**
159
     * Get a list of all issue sort types
160
     *
161
     * @return array
162
     */
163 3
    public function issueSortTypes()
164
    {
165 3
        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 3
    public function createCompany($company)
196
    {
197 3
        return $this->post('companies', $company->toArray());
198
    }
199
200
    /**
201
     * Override the default Guzzle client
202
     *
203
     * @param \GuzzleHttp\Client $client
204
     */
205 108
    public function setClient($client)
206
    {
207 108
        $this->guzzleClient = $client;
208 108
    }
209
210
    /**
211
     * Generate the full URL from the specified endpoint
212
     *
213
     * @param string $endpoint
214
     *
215
     * @return string
216
     */
217 99
    private function getUrl($endpoint)
218
    {
219 99
        return sprintf(
220 99
            'https://%s.mydonedone.com/issuetracker/api/v2/%s.json',
221 99
            $this->teamName,
222
            $endpoint
223 99
        );
224
    }
225
226
    /**
227
     * @param string $endpoint
228
     * @param array  $data
229
     *
230
     * @return array
231
     */
232 69
    public function get($endpoint, $data = [])
233
    {
234 69
        $url = $this->getUrl($endpoint);
235
236 69
        $response = $this->guzzleClient->get($url, [
237
            'query' => $data
238 69
        ]);
239
240 69
        return $response->json();
241
    }
242
243
    /**
244
     * @param string $endpoint
245
     * @param array  $data
246
     *
247
     * @return array
248
     */
249 12
    public function post($endpoint, $data = [])
250
    {
251 12
        $url = $this->getUrl($endpoint);
252
253 12
        $response = $this->guzzleClient->post($url, [
254
            'body' => $data
255 12
        ]);
256
257 12
        return $response->json();
258
    }
259
260
    /**
261
     * @param string $endpoint
262
     * @param array  $data
263
     *
264
     * @return array
265
     */
266 18
    public function put($endpoint, $data = [])
267
    {
268 18
        $url = $this->getUrl($endpoint);
269
270 18
        $response = $this->guzzleClient->put($url, [
271
            'body' => $data
272 18
        ]);
273
274 18
        return $response->json();
275
    }
276
277
}
278