Completed
Push — master ( 14668b...b8937b )
by Zbigniew
08:42
created

AbstractApi   C

Complexity

Total Complexity 22

Size/Duplication

Total Lines 276
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 19

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 19
dl 0
loc 276
ccs 110
cts 110
cp 1
rs 6.875
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 2
A getContactResource() 0 9 1
A getUserResource() 0 9 1
A getGroupResource() 0 9 1
A getInvitationResource() 0 9 1
A getAccountResource() 0 9 1
A getWorkflowResource() 0 9 1
A getCustomFieldResource() 0 9 1
A getFolderResource() 0 9 1
A getTaskResource() 0 9 1
A getCommentResource() 0 9 1
A getDependencyResource() 0 9 1
A getTimelogResource() 0 9 1
A getAttachmentResource() 0 9 1
A getVersionResource() 0 9 1
A getIdResource() 0 9 1
A getColorResource() 0 9 1
A normalizeParams() 0 10 4
1
<?php
2
3
/*
4
 * This file is part of the zibios/wrike-php-library package.
5
 *
6
 * (c) Zbigniew Ślązak
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zibios\WrikePhpLibrary;
13
14
use Zibios\WrikePhpLibrary\Client\ClientInterface;
15
use Zibios\WrikePhpLibrary\Resource\AccountResource;
16
use Zibios\WrikePhpLibrary\Resource\AttachmentResource;
17
use Zibios\WrikePhpLibrary\Resource\ColorResource;
18
use Zibios\WrikePhpLibrary\Resource\CommentResource;
19
use Zibios\WrikePhpLibrary\Resource\ContactResource;
20
use Zibios\WrikePhpLibrary\Resource\CustomFieldResource;
21
use Zibios\WrikePhpLibrary\Resource\DependencyResource;
22
use Zibios\WrikePhpLibrary\Resource\FolderResource;
23
use Zibios\WrikePhpLibrary\Resource\GroupResource;
24
use Zibios\WrikePhpLibrary\Resource\IdResource;
25
use Zibios\WrikePhpLibrary\Resource\InvitationResource;
26
use Zibios\WrikePhpLibrary\Resource\TaskResource;
27
use Zibios\WrikePhpLibrary\Resource\TimelogResource;
28
use Zibios\WrikePhpLibrary\Resource\UserResource;
29
use Zibios\WrikePhpLibrary\Resource\VersionResource;
30
use Zibios\WrikePhpLibrary\Resource\WorkflowResource;
31
use Zibios\WrikePhpLibrary\Transformer\ApiExceptionTransformerInterface;
32
use Zibios\WrikePhpLibrary\Transformer\ResponseTransformerInterface;
33
use Zibios\WrikePhpLibrary\Validator\AccessTokenValidator;
34
35
/**
36
 * Abstract for General Wrike Api.
37
 *
38
 * Entry point for all Wrike API operations.
39
 *
40
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
41
 */
42
abstract class AbstractApi implements ApiInterface
43
{
44
    const BASE_URI = 'https://www.wrike.com/api/v3/';
45
46
    /**
47
     * @var ClientInterface
48
     */
49
    protected $client;
50
51
    /**
52
     * @var string
53
     */
54
    protected $accessToken = '';
55
56
    /**
57
     * @var ResponseTransformerInterface
58
     */
59
    protected $responseTransformer;
60
61
    /**
62
     * @var ApiExceptionTransformerInterface
63
     */
64
    protected $apiExceptionTransformer;
65
66
    /**
67
     * Api constructor.
68
     *
69
     * @param ClientInterface                  $client
70
     * @param ResponseTransformerInterface     $responseTransformer
71
     * @param ApiExceptionTransformerInterface $apiExceptionTransformer
72
     * @param string                           $accessToken
73
     *
74
     * @throws \InvalidArgumentException
75
     */
76 31
    public function __construct(
77
        ClientInterface $client,
78
        ResponseTransformerInterface $responseTransformer,
79
        ApiExceptionTransformerInterface $apiExceptionTransformer,
80
        $accessToken = ''
81
    ) {
82 31
        AccessTokenValidator::assertIsValidOrEmpty($accessToken);
83
84 31
        if ($responseTransformer->isSupportedResponseFormat($client->getResponseFormat()) === false) {
85 1
            throw new \InvalidArgumentException('Client not compatible with response transformer!');
86
        }
87
88 31
        $this->client = $client;
89 31
        $this->responseTransformer = $responseTransformer;
90 31
        $this->apiExceptionTransformer = $apiExceptionTransformer;
91 31
        $this->accessToken = $accessToken;
92 31
    }
93
94
    /**
95
     * @return ContactResource
96
     */
97 1
    public function getContactResource()
98
    {
99 1
        return new ContactResource(
100 1
            $this->client,
101 1
            $this->responseTransformer,
102 1
            $this->apiExceptionTransformer,
103 1
            $this->accessToken
104
        );
105
    }
106
107
    /**
108
     * @return UserResource
109
     */
110 1
    public function getUserResource()
111
    {
112 1
        return new UserResource(
113 1
            $this->client,
114 1
            $this->responseTransformer,
115 1
            $this->apiExceptionTransformer,
116 1
            $this->accessToken
117
        );
118
    }
119
120
    /**
121
     * @return GroupResource
122
     */
123 1
    public function getGroupResource()
124
    {
125 1
        return new GroupResource(
126 1
            $this->client,
127 1
            $this->responseTransformer,
128 1
            $this->apiExceptionTransformer,
129 1
            $this->accessToken
130
        );
131
    }
132
133
    /**
134
     * @return InvitationResource
135
     */
136 1
    public function getInvitationResource()
137
    {
138 1
        return new InvitationResource(
139 1
            $this->client,
140 1
            $this->responseTransformer,
141 1
            $this->apiExceptionTransformer,
142 1
            $this->accessToken
143
        );
144
    }
145
146
    /**
147
     * @return AccountResource
148
     */
149 1
    public function getAccountResource()
150
    {
151 1
        return new AccountResource(
152 1
            $this->client,
153 1
            $this->responseTransformer,
154 1
            $this->apiExceptionTransformer,
155 1
            $this->accessToken
156
        );
157
    }
158
159
    /**
160
     * @return WorkflowResource
161
     */
162 1
    public function getWorkflowResource()
163
    {
164 1
        return new WorkflowResource(
165 1
            $this->client,
166 1
            $this->responseTransformer,
167 1
            $this->apiExceptionTransformer,
168 1
            $this->accessToken
169
        );
170
    }
171
172
    /**
173
     * @return CustomFieldResource
174
     */
175 1
    public function getCustomFieldResource()
176
    {
177 1
        return new CustomFieldResource(
178 1
            $this->client,
179 1
            $this->responseTransformer,
180 1
            $this->apiExceptionTransformer,
181 1
            $this->accessToken
182
        );
183
    }
184
185
    /**
186
     * @return FolderResource
187
     */
188 1
    public function getFolderResource()
189
    {
190 1
        return new FolderResource(
191 1
            $this->client,
192 1
            $this->responseTransformer,
193 1
            $this->apiExceptionTransformer,
194 1
            $this->accessToken
195
        );
196
    }
197
198
    /**
199
     * @return TaskResource
200
     */
201 1
    public function getTaskResource()
202
    {
203 1
        return new TaskResource(
204 1
            $this->client,
205 1
            $this->responseTransformer,
206 1
            $this->apiExceptionTransformer,
207 1
            $this->accessToken
208
        );
209
    }
210
211
    /**
212
     * @return CommentResource
213
     */
214 1
    public function getCommentResource()
215
    {
216 1
        return new CommentResource(
217 1
            $this->client,
218 1
            $this->responseTransformer,
219 1
            $this->apiExceptionTransformer,
220 1
            $this->accessToken
221
        );
222
    }
223
224
    /**
225
     * @return DependencyResource
226
     */
227 1
    public function getDependencyResource()
228
    {
229 1
        return new DependencyResource(
230 1
            $this->client,
231 1
            $this->responseTransformer,
232 1
            $this->apiExceptionTransformer,
233 1
            $this->accessToken
234
        );
235
    }
236
237
    /**
238
     * @return TimelogResource
239
     */
240 1
    public function getTimelogResource()
241
    {
242 1
        return new TimelogResource(
243 1
            $this->client,
244 1
            $this->responseTransformer,
245 1
            $this->apiExceptionTransformer,
246 1
            $this->accessToken
247
        );
248
    }
249
250
    /**
251
     * @return AttachmentResource
252
     */
253 1
    public function getAttachmentResource()
254
    {
255 1
        return new AttachmentResource(
256 1
            $this->client,
257 1
            $this->responseTransformer,
258 1
            $this->apiExceptionTransformer,
259 1
            $this->accessToken
260
        );
261
    }
262
263
    /**
264
     * @return VersionResource
265
     */
266 1
    public function getVersionResource()
267
    {
268 1
        return new VersionResource(
269 1
            $this->client,
270 1
            $this->responseTransformer,
271 1
            $this->apiExceptionTransformer,
272 1
            $this->accessToken
273
        );
274
    }
275
276
    /**
277
     * @return IdResource
278
     */
279 1
    public function getIdResource()
280
    {
281 1
        return new IdResource(
282 1
            $this->client,
283 1
            $this->responseTransformer,
284 1
            $this->apiExceptionTransformer,
285 1
            $this->accessToken
286
        );
287
    }
288
289
    /**
290
     * @return ColorResource
291
     */
292 1
    public function getColorResource()
293
    {
294 1
        return new ColorResource(
295 1
            $this->client,
296 1
            $this->responseTransformer,
297 1
            $this->apiExceptionTransformer,
298 1
            $this->accessToken
299
        );
300
    }
301
302
    /**
303
     * @param array $params
304
     *
305
     * @return array
306
     */
307 1
    public function normalizeParams(array $params)
308
    {
309 1
        foreach ($params as $key => $value) {
310 1
            if (is_string($value) === false && is_resource($value) === false) {
311 1
                $params[$key] = json_encode($value);
312
            }
313
        }
314
315 1
        return $params;
316
    }
317
}
318