Passed
Branch master (81c7db)
by Jonathan
10:52 queued 01:40
created

GetTrait::getTemplateThumbnails()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3

Importance

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