Passed
Push — master ( e72220...d47cbb )
by Jonathan
12:45
created

GetTrait::get()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

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