Passed
Push — master ( 72713e...d5a1de )
by Jonathan
09:55
created

GetTrait   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 384
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 38
dl 0
loc 384
ccs 138
cts 138
cp 1
rs 8.3999
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getProofingSuggestions() 0 21 3
A getTemplateInfo() 0 19 3
A proofingCheck() 0 21 3
A getApiKeys() 0 16 4
A getAvailableDictionaries() 0 11 3
A templateExists() 0 9 1
A getTemplateCount() 0 3 1
A getTemplatePageCount() 0 9 1
A getTemplateList() 0 19 4
B getTemplateThumbnails() 0 25 3
A getFontList() 0 11 3
A downloadTemplate() 0 17 2
A get() 0 15 3
A getAccountSettings() 0 17 4
1
<?php
2
3
/**
4
 * ReportingCloud PHP Wrapper
5
 *
6
 * PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH.
7
 *
8
 * @link      http://www.reporting.cloud to learn more about ReportingCloud
9
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
10
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
11
 * @copyright © 2018 Text Control GmbH
12
 */
13
14
namespace TxTextControl\ReportingCloud;
15
16
use GuzzleHttp\Psr7\Response;
17
use GuzzleHttp\RequestOptions;
18
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
19
use TxTextControl\ReportingCloud\Filter\StaticFilter;
20
use TxTextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap;
21
use TxTextControl\ReportingCloud\PropertyMap\AccountSettings as AccountSettingsPropertyMap;
22
use TxTextControl\ReportingCloud\PropertyMap\ApiKey as ApiKeyPropertyMap;
23
use TxTextControl\ReportingCloud\PropertyMap\IncorrectWord as IncorrectWordMap;
24
use TxTextControl\ReportingCloud\PropertyMap\TemplateInfo as TemplateInfoPropertyMap;
25
use TxTextControl\ReportingCloud\PropertyMap\TemplateList as TemplateListPropertyMap;
26
use TxTextControl\ReportingCloud\Validator\StaticValidator;
27
28
trait GetTrait
29
{
30
    /**
31
     * Construct URI with version number
32
     *
33
     * @param string $uri URI
34
     *
35
     * @return string
36
     */
37
    abstract protected function uri($uri);
38
39
    /**
40
     * Request the URI with options
41
     *
42
     * @param string $method  HTTP method
43
     * @param string $uri     URI
44
     * @param array  $options Options
45
     *
46
     * @return mixed|null|\Psr\Http\Message\ResponseInterface
47
     *
48
     * @throws RuntimeException
49
     */
50
    abstract protected function request($method, $uri, $options);
51
52
    /**
53
     * Using the passed propertyMap, recursively build array
54
     *
55
     * @param array       $array       Array
56
     * @param PropertyMap $propertyMap PropertyMap
57
     *
58
     * @return array
59
     */
60
    abstract protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap);
61
62
    /**
63
     * Return an associative array of API keys associated with the Reporting Cloud account
64
     *
65
     * @return array|null
66
     */
67 6
    public function getApiKeys()
68
    {
69 6
        $ret = null;
70
71 6
        $propertyMap = new ApiKeyPropertyMap();
72
73 6
        $records = $this->get('/account/apikeys');
74
75 6
        if (is_array($records) && count($records) > 0) {
76 6
            $ret = [];
77 6
            foreach ($records as $record) {
78 6
                $ret[] = $this->buildPropertyMapArray($record, $propertyMap);
79 3
            }
80 3
        }
81
82 6
        return $ret;
83
    }
84
85
    /**
86
     * Check a corpus of text for spelling errors.
87
     *
88
     * Return an array of misspelled words, if spelling errors are found in the corpus of text.
89
     *
90
     * Return null, if no misspelled words are found in the corpus of text.
91
     *
92
     * @param string $text     Corpus of text that should be spell checked
93
     * @param string $language Language of specified text
94
     *
95
     * @return array|null
96
     */
97 2
    public function proofingCheck($text, $language)
98
    {
99 2
        $ret = null;
100
101 2
        StaticValidator::execute($text, 'TypeString');
102 2
        StaticValidator::execute($language, 'Language');
103
104 2
        $propertyMap = new IncorrectWordMap();
105
106
        $query = [
107 2
            'text'     => $text,
108 2
            'language' => $language,
109 1
        ];
110
111 2
        $records = $this->get('/proofing/check', $query);
112
113 2
        if (is_array($records) && count($records) > 0) {
114 2
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
115 1
        }
116
117 2
        return $ret;
118
    }
119
120
    /**
121
     * Return an array of available dictionaries on the Reporting Cloud service
122
     *
123
     * @return array|null
124
     */
125 2
    public function getAvailableDictionaries()
126
    {
127 2
        $ret = null;
128
129 2
        $dictionaries = $this->get('/proofing/availabledictionaries');
130
131 2
        if (is_array($dictionaries) && count($dictionaries) > 0) {
132 2
            $ret = array_map('trim', $dictionaries);
133 1
        }
134
135 2
        return $ret;
136
    }
137
138
    /**
139
     * Return an array of suggestions for a misspelled word.
140
     *
141
     * @param string $word     Word that should be spell checked
142
     * @param string $language Language of specified text
143
     * @param int    $max      Maximum number of suggestions to return
144
     *
145
     * @return array|null
146
     */
147 2
    public function getProofingSuggestions($word, $language, $max = 10)
148
    {
149 2
        $ret = null;
150
151 2
        StaticValidator::execute($word, 'TypeString');
152 2
        StaticValidator::execute($language, 'Language');
153 2
        StaticValidator::execute($max, 'TypeInteger');
154
155
        $query = [
156 2
            'word'     => $word,
157 2
            'language' => $language,
158 2
            'max'      => $max,
159 1
        ];
160
161 2
        $records = $this->get('/proofing/suggestions', $query);
162
163 2
        if (is_array($records) && count($records) > 0) {
164 2
            $ret = array_map('trim', $records);
165 1
        }
166
167 2
        return $ret;
168
    }
169
170
    /**
171
     * Return an array of merge blocks and merge fields in a template file in template storage.
172
     *
173
     * @param string $templateName Template name
174
     *
175
     * @throws InvalidArgumentException
176
     *
177
     * @return array|null
178
     */
179 2
    public function getTemplateInfo($templateName)
180
    {
181 2
        $ret = null;
182
183 2
        $propertyMap = new TemplateInfoPropertyMap();
184
185 2
        StaticValidator::execute($templateName, 'TemplateName');
186
187
        $query = [
188 2
            'templateName' => $templateName,
189 1
        ];
190
191 2
        $records = $this->get('/templates/info', $query);
192
193 2
        if (is_array($records) && count($records) > 0) {
194 2
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
195 1
        }
196
197 2
        return $ret;
198
    }
199
200
    /**
201
     * Return an array of binary data.
202
     * Each record in the array is the binary data of a thumbnail image
203
     *
204
     * @param string $templateName Template name
205
     * @param int    $zoomFactor   Zoom factor
206
     * @param int    $fromPage     From page
207
     * @param int    $toPage       To page
208
     * @param string $imageFormat  Image format
209
     *
210
     * @throws InvalidArgumentException
211
     *
212
     * @return array|null
213
     */
214 12
    public function getTemplateThumbnails($templateName, $zoomFactor, $fromPage, $toPage, $imageFormat)
215
    {
216 12
        $ret = null;
217
218 12
        StaticValidator::execute($templateName, 'TemplateName');
219 10
        StaticValidator::execute($zoomFactor, 'ZoomFactor');
220 8
        StaticValidator::execute($fromPage, 'Page');
221 6
        StaticValidator::execute($toPage, 'Page');
222 4
        StaticValidator::execute($imageFormat, 'ImageFormat');
223
224
        $query = [
225 2
            'templateName' => $templateName,
226 2
            'zoomFactor'   => $zoomFactor,
227 2
            'fromPage'     => $fromPage,
228 2
            'toPage'       => $toPage,
229 2
            'imageFormat'  => $imageFormat,
230 1
        ];
231
232 2
        $records = $this->get('/templates/thumbnails', $query);
233
234 2
        if (is_array($records) && count($records) > 0) {
235 2
            $ret = array_map('base64_decode', $records);
236 1
        }
237
238 2
        return $ret;
239
    }
240
241
    /**
242
     * Return the number of templates in template storage
243
     *
244
     * @return int
245
     */
246 2
    public function getTemplateCount()
247
    {
248 2
        return (int) $this->get('/templates/count');
249
    }
250
251
    /**
252
     * Return an array properties for the templates in template storage
253
     *
254
     * @return array|null
255
     */
256 2
    public function getTemplateList()
257
    {
258 2
        $ret = null;
259
260 2
        $propertyMap = new TemplateListPropertyMap();
261
262 2
        $records = $this->get('/templates/list');
263
264 2
        if (is_array($records) && count($records) > 0) {
265 2
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
266 2
            array_walk($ret, function (&$record) {
267 2
                $key = 'modified';
268 2
                if (isset($record[$key])) {
269 2
                    $record[$key] = StaticFilter::execute($record[$key], 'DateTimeToTimestamp');
270 1
                }
271 2
            });
272 1
        }
273
274 2
        return $ret;
275
    }
276
277
    /**
278
     * Return the number of pages in a template in template storage
279
     *
280
     * @param string $templateName Template name
281
     *
282
     * @throws InvalidArgumentException
283
     *
284
     * @return int
285
     */
286 4
    public function getTemplatePageCount($templateName)
287
    {
288 4
        StaticValidator::execute($templateName, 'TemplateName');
289
290
        $query = [
291 2
            'templateName' => $templateName,
292 1
        ];
293
294 2
        return (int) $this->get('/templates/pagecount', $query);
295
    }
296
297
    /**
298
     * Return true, if the template exists in template storage
299
     *
300
     * @param string $templateName Template name
301
     *
302
     * @throws InvalidArgumentException
303
     *
304
     * @return bool
305
     */
306 4
    public function templateExists($templateName)
307
    {
308 4
        StaticValidator::execute($templateName, 'TemplateName');
309
310
        $query = [
311 2
            'templateName' => $templateName,
312 1
        ];
313
314 2
        return (bool) $this->get('/templates/exists', $query);
315
    }
316
317
    /**
318
     * Return an array of available fonts on the Reporting Cloud service
319
     *
320
     * @return array|null
321
     */
322 6
    public function getFontList()
323
    {
324 6
        $ret = null;
325
326 6
        $fonts = $this->get('/fonts/list');
327
328 4
        if (is_array($fonts) && count($fonts) > 0) {
329 4
            $ret = array_map('trim', $fonts);
330 2
        }
331
332 4
        return $ret;
333
    }
334
335
    /**
336
     * Return an array properties for the ReportingCloud account
337
     *
338
     * @throws \Zend\Filter\Exception\ExceptionInterface
339
     *
340
     * @return array|null
341
     */
342 2
    public function getAccountSettings()
343
    {
344 2
        $ret = null;
345
346 2
        $propertyMap = new AccountSettingsPropertyMap();
347
348 2
        $records = $this->get('/account/settings');
349
350 2
        if (is_array($records) && count($records) > 0) {
351 2
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
352 2
            $key = 'valid_until';
353 2
            if ($ret[$key]) {
354 2
                $ret[$key] = StaticFilter::execute($ret[$key], 'DateTimeToTimestamp');
355 1
            }
356 1
        }
357
358 2
        return $ret;
359
    }
360
361
    /**
362
     * Download the binary data of a template from template storage
363
     *
364
     * @param string $templateName Template name
365
     *
366
     * @throws InvalidArgumentException
367
     *
368
     * @return resource|null
369
     */
370 4
    public function downloadTemplate($templateName)
371
    {
372 4
        $ret = null;
373
374 4
        StaticValidator::execute($templateName, 'TemplateName');
375
376
        $query = [
377 2
            'templateName' => $templateName,
378 1
        ];
379
380 2
        $data = $this->get('/templates/download', $query);
381
382 2
        if (null !== $data) {
383 2
            $ret = base64_decode($data);
384 1
        }
385
386 2
        return $ret;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ret also could return the type string which is incompatible with the documented return type resource|null.
Loading history...
387
    }
388
389
    /**
390
     * Execute a GET request via REST client
391
     *
392
     * @param string $uri   URI
393
     * @param array  $query Query
394
     *
395
     * @return mixed|null
396
     */
397 32
    protected function get($uri, $query = [])
398
    {
399 32
        $ret = null;
400
401
        $options = [
402 32
            RequestOptions::QUERY => $query,
403 16
        ];
404
405 32
        $response = $this->request('GET', $this->uri($uri), $options);
406
407 30
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
408 30
            $ret = json_decode($response->getBody(), true);
409 15
        }
410
411 30
        return $ret;
412
    }
413
}
414