Completed
Push — master ( 305f92...861a0b )
by Philip
10:08 queued 07:56
created

Client   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 285
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 23.53%

Importance

Changes 16
Bugs 3 Features 0
Metric Value
wmc 24
c 16
b 3
f 0
lcom 1
cbo 4
dl 0
loc 285
ccs 16
cts 68
cp 0.2353
rs 10

21 Methods

Rating   Name   Duplication   Size   Complexity  
A project() 0 4 1
A company() 0 4 1
A setClient() 0 4 1
A __construct() 0 12 1
A priorityLevels() 0 4 1
A projects() 0 4 1
A companies() 0 4 1
A issues() 0 4 1
A activeIssues() 0 4 1
A closedAndFixedIssues() 0 4 1
A issuesByFilter() 0 4 1
A issuesWaitingOnYou() 0 4 1
A issuesWaitingOnThem() 0 4 1
A globalFilters() 0 4 1
A issueCreationTypes() 0 4 1
A issueSortTypes() 0 4 1
A createCompany() 0 4 1
A put() 0 14 2
A getUrl() 0 8 1
A get() 0 14 2
A post() 0 14 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 9
    public function __construct($teamName, $username, $passwordOrApiToken)
34
    {
35 9
        $this->teamName = $teamName;
36 9
        $this->username = $username;
37 9
        $this->passwordOrApiToken = $passwordOrApiToken;
38
39 9
        $this->setClient(new \GuzzleHttp\Client([
40
            'defaults' => [
41 9
                'auth' => [$username, $passwordOrApiToken]
42 9
            ]
43 9
        ]));
44 9
    }
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 3
    public function project($id)
174
    {
175 3
        return new Project($this, $id);
176
    }
177
178
    /**
179
     * @param int $id
180
     *
181
     * @return Company
182
     */
183 3
    public function company($id)
184
    {
185 3
        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 9
    public function setClient($client)
206
    {
207 9
        $this->guzzleClient = $client;
208 9
    }
209
210
    /**
211
     * Generate the full URL from the specified endpoint
212
     *
213
     * @param string $endpoint
214
     *
215
     * @return string
216
     */
217
    private function getUrl($endpoint)
218
    {
219
        return sprintf(
220
            'https://%s.mydonedone.com/issuetracker/api/v2/%s.json',
221
            $this->teamName,
222
            $endpoint
223
        );
224
    }
225
226
    /**
227
     * @param string $endpoint
228
     * @param array  $data
229
     *
230
     * @return array
231
     */
232
    public function get($endpoint, $data = [])
233
    {
234
        $url = $this->getUrl($endpoint);
235
236
        $response = $this->guzzleClient->get($url, [
237
            'query' => $data
238
        ]);
239
240
        if ($response) {
241
            return json_decode($response->getBody(), true);
242
        } else {
243
            return null;
244
        }
245
    }
246
247
    /**
248
     * @param string $endpoint
249
     * @param array  $data
250
     *
251
     * @return array
252
     */
253
    public function post($endpoint, $data = [])
254
    {
255
        $url = $this->getUrl($endpoint);
256
257
        $response = $this->guzzleClient->post($url, [
258
            'body' => $data
259
        ]);
260
261
        if ($response) {
262
            return json_decode($response->getBody(), true);
263
        } else {
264
            return null;
265
        }
266
    }
267
268
    /**
269
     * @param string $endpoint
270
     * @param array  $data
271
     *
272
     * @return array
273
     */
274
    public function put($endpoint, $data = [])
275
    {
276
        $url = $this->getUrl($endpoint);
277
278
        $response = $this->guzzleClient->put($url, [
279
            'body' => $data
280
        ]);
281
282
        if ($response) {
283
            return json_decode($response->getBody(), true);
284
        } else {
285
            return null;
286
        }
287
    }
288
289
}
290