Passed
Branch development-2.0 (a4adda)
by Jonathan
05:20
created

GetTrait   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 396
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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