Passed
Push — master ( 85a67e...fabbda )
by Jonathan
17:41
created

ReportingCloud::setVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 2
nc 1
nop 1
crap 1
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\AccountSettings as AccountSettingsPropertyMap;
21
use TxTextControl\ReportingCloud\PropertyMap\ApiKey as ApiKeyPropertyMap;
22
use TxTextControl\ReportingCloud\PropertyMap\IncorrectWord as IncorrectWordMap;
23
use TxTextControl\ReportingCloud\PropertyMap\TemplateInfo as TemplateInfoPropertyMap;
24
use TxTextControl\ReportingCloud\PropertyMap\TemplateList as TemplateListPropertyMap;
25
use TxTextControl\ReportingCloud\Validator\StaticValidator;
26
27
/**
28
 * ReportingCloud
29
 *
30
 * @package TxTextControl\ReportingCloud
31
 * @author  Jonathan Maron (@JonathanMaron)
32
 */
33
class ReportingCloud extends AbstractReportingCloud
34
{
35
    /**
36
     * DELETE Methods
37
     * -----------------------------------------------------------------------------------------------------------------
38
     */
39
40
    /**
41
     * Delete an API key
42
     *
43
     * @param string $key API key
44
     *
45
     * @return bool
46
     */
47 4
    public function deleteApiKey($key)
48
    {
49 4
        $ret = false;
50
51 4
        StaticValidator::execute($key, 'ApiKey');
52
53
        $options = [
54 4
            RequestOptions::QUERY => [
55 4
                'key' => $key,
56 2
            ],
57 2
        ];
58
59 4
        $response = $this->request('DELETE', $this->uri('/account/apikey'), $options);
60
61 4
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
62 4
            $ret = true;
63 2
        }
64
65 4
        return $ret;
66
    }
67
68
    /**
69
     * Delete a template in template storage
70
     *
71
     * @param string $templateName Template name
72
     *
73
     * @throws InvalidArgumentException
74
     *
75
     * @return bool
76
     */
77 22
    public function deleteTemplate($templateName)
78
    {
79 22
        $ret = false;
80
81 22
        StaticValidator::execute($templateName, 'TemplateName');
82
83
        $options = [
84 20
            RequestOptions::QUERY => [
85 20
                'templateName' => $templateName,
86 10
            ],
87 10
        ];
88
89 20
        $response = $this->request('DELETE', $this->uri('/templates/delete'), $options);
90
91 18
        if ($response instanceof Response && 204 === $response->getStatusCode()) {
92 18
            $ret = true;
93 9
        }
94
95 18
        return $ret;
96
    }
97
98
    /**
99
     * GET Methods
100
     * -----------------------------------------------------------------------------------------------------------------
101
     */
102
103
    /**
104
     * Return an associative array of API keys associated with the Reporting Cloud account
105
     *
106
     * @return array|null
107
     */
108 6
    public function getApiKeys()
109
    {
110 6
        $ret = null;
111
112 6
        $propertyMap = new ApiKeyPropertyMap();
113
114 6
        $records = $this->get('/account/apikeys');
115
116 6
        if (is_array($records) && count($records) > 0) {
117 6
            $ret = [];
118 6
            foreach ($records as $record) {
119 6
                $ret[] = $this->buildPropertyMapArray($record, $propertyMap);
120 3
            }
121 3
        }
122
123 6
        return $ret;
124
    }
125
126
    /**
127
     * Check a corpus of text for spelling errors.
128
     *
129
     * Return an array of misspelled words, if spelling errors are found in the corpus of text.
130
     *
131
     * Return null, if no misspelled words are found in the corpus of text.
132
     *
133
     * @param string $text     Corpus of text that should be spell checked
134
     * @param string $language Language of specified text
135
     *
136
     * @return array|null
137
     */
138 2
    public function proofingCheck($text, $language)
139
    {
140 2
        $ret = null;
141
142 2
        StaticValidator::execute($text, 'TypeString');
143 2
        StaticValidator::execute($language, 'Language');
144
145 2
        $propertyMap = new IncorrectWordMap();
146
147
        $query = [
148 2
            'text'     => $text,
149 2
            'language' => $language,
150 1
        ];
151
152 2
        $records = $this->get('/proofing/check', $query);
153
154 2
        if (is_array($records) && count($records) > 0) {
155 2
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
156 1
        }
157
158 2
        return $ret;
159
    }
160
161
    /**
162
     * Return an array of available dictionaries on the Reporting Cloud service
163
     *
164
     * @return array|null
165
     */
166 2
    public function getAvailableDictionaries()
167
    {
168 2
        $ret = null;
169
170 2
        $dictionaries = $this->get('/proofing/availabledictionaries');
171
172 2
        if (is_array($dictionaries) && count($dictionaries) > 0) {
173 2
            $ret = array_map('trim', $dictionaries);
174 1
        }
175
176 2
        return $ret;
177
    }
178
179
    /**
180
     * Return an array of suggestions for a misspelled word.
181
     *
182
     * @param string $word     Word that should be spell checked
183
     * @param string $language Language of specified text
184
     * @param int    $max      Maximum number of suggestions to return
185
     *
186
     * @return array|null
187
     */
188 2
    public function getProofingSuggestions($word, $language, $max = 10)
189
    {
190 2
        $ret = null;
191
192 2
        StaticValidator::execute($word, 'TypeString');
193 2
        StaticValidator::execute($language, 'Language');
194 2
        StaticValidator::execute($max, 'TypeInteger');
195
196
        $query = [
197 2
            'word'     => $word,
198 2
            'language' => $language,
199 2
            'max'      => $max,
200 1
        ];
201
202 2
        $records = $this->get('/proofing/suggestions', $query);
203
204 2
        if (is_array($records) && count($records) > 0) {
205 2
            $ret = array_map('trim', $records);
206 1
        }
207
208 2
        return $ret;
209
    }
210
211
    /**
212
     * Return an array of merge blocks and merge fields in a template file in template storage.
213
     *
214
     * @param string $templateName Template name
215
     *
216
     * @throws InvalidArgumentException
217
     *
218
     * @return array|null
219
     */
220 2
    public function getTemplateInfo($templateName)
221
    {
222 2
        $ret = null;
223
224 2
        $propertyMap = new TemplateInfoPropertyMap();
225
226 2
        StaticValidator::execute($templateName, 'TemplateName');
227
228
        $query = [
229 2
            'templateName' => $templateName,
230 1
        ];
231
232 2
        $records = $this->get('/templates/info', $query);
233
234 2
        if (is_array($records) && count($records) > 0) {
235 2
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
236 1
        }
237
238 2
        return $ret;
239
    }
240
241
    /**
242
     * Return an array of binary data.
243
     * Each record in the array is the binary data of a thumbnail image
244
     *
245
     * @param string $templateName Template name
246
     * @param int    $zoomFactor   Zoom factor
247
     * @param int    $fromPage     From page
248
     * @param int    $toPage       To page
249
     * @param string $imageFormat  Image format
250
     *
251
     * @throws InvalidArgumentException
252
     *
253
     * @return array|null
254
     */
255 12
    public function getTemplateThumbnails($templateName, $zoomFactor, $fromPage, $toPage, $imageFormat)
256
    {
257 12
        $ret = null;
258
259 12
        StaticValidator::execute($templateName, 'TemplateName');
260 10
        StaticValidator::execute($zoomFactor, 'ZoomFactor');
261 8
        StaticValidator::execute($fromPage, 'Page');
262 6
        StaticValidator::execute($toPage, 'Page');
263 4
        StaticValidator::execute($imageFormat, 'ImageFormat');
264
265
        $query = [
266 2
            'templateName' => $templateName,
267 2
            'zoomFactor'   => $zoomFactor,
268 2
            'fromPage'     => $fromPage,
269 2
            'toPage'       => $toPage,
270 2
            'imageFormat'  => $imageFormat,
271 1
        ];
272
273 2
        $records = $this->get('/templates/thumbnails', $query);
274
275 2
        if (is_array($records) && count($records) > 0) {
276 2
            $ret = array_map('base64_decode', $records);
277 1
        }
278
279 2
        return $ret;
280
    }
281
282
    /**
283
     * Return the number of templates in template storage
284
     *
285
     * @return int
286
     */
287 2
    public function getTemplateCount()
288
    {
289 2
        return (int) $this->get('/templates/count');
290
    }
291
292
    /**
293
     * Return an array properties for the templates in template storage
294
     *
295
     * @return array|null
296
     */
297 2
    public function getTemplateList()
298
    {
299 2
        $ret = null;
300
301 2
        $propertyMap = new TemplateListPropertyMap();
302
303 2
        $records = $this->get('/templates/list');
304
305 2
        if (is_array($records) && count($records) > 0) {
306 2
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
307 2
            array_walk($ret, function (&$record) {
308 2
                $key = 'modified';
309 2
                if (isset($record[$key])) {
310 2
                    $record[$key] = StaticFilter::execute($record[$key], 'DateTimeToTimestamp');
311 1
                }
312 2
            });
313 1
        }
314
315 2
        return $ret;
316
    }
317
318
    /**
319
     * Return the number of pages in a template in template storage
320
     *
321
     * @param string $templateName Template name
322
     *
323
     * @throws InvalidArgumentException
324
     *
325
     * @return int
326
     */
327 4
    public function getTemplatePageCount($templateName)
328
    {
329 4
        StaticValidator::execute($templateName, 'TemplateName');
330
331
        $query = [
332 2
            'templateName' => $templateName,
333 1
        ];
334
335 2
        return (int) $this->get('/templates/pagecount', $query);
336
    }
337
338
    /**
339
     * Return true, if the template exists in template storage
340
     *
341
     * @param string $templateName Template name
342
     *
343
     * @throws InvalidArgumentException
344
     *
345
     * @return bool
346
     */
347 4
    public function templateExists($templateName)
348
    {
349 4
        StaticValidator::execute($templateName, 'TemplateName');
350
351
        $query = [
352 2
            'templateName' => $templateName,
353 1
        ];
354
355 2
        return (bool) $this->get('/templates/exists', $query);
356
    }
357
358
    /**
359
     * Return an array of available fonts on the Reporting Cloud service
360
     *
361
     * @return array|null
362
     */
363 6
    public function getFontList()
364
    {
365 6
        $ret = null;
366
367 6
        $fonts = $this->get('/fonts/list');
368
369 4
        if (is_array($fonts) && count($fonts) > 0) {
370 4
            $ret = array_map('trim', $fonts);
371 2
        }
372
373 4
        return $ret;
374
    }
375
376
    /**
377
     * Return an array properties for the ReportingCloud account
378
     *
379
     * @throws \Zend\Filter\Exception\ExceptionInterface
380
     *
381
     * @return array|null
382
     */
383 2
    public function getAccountSettings()
384
    {
385 2
        $ret = null;
386
387 2
        $propertyMap = new AccountSettingsPropertyMap();
388
389 2
        $records = $this->get('/account/settings');
390
391 2
        if (is_array($records) && count($records) > 0) {
392 2
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
393 2
            $key = 'valid_until';
394 2
            if ($ret[$key]) {
395 2
                $ret[$key] = StaticFilter::execute($ret[$key], 'DateTimeToTimestamp');
396 1
            }
397 1
        }
398
399 2
        return $ret;
400
    }
401
402
    /**
403
     * Download the binary data of a template from template storage
404
     *
405
     * @param string $templateName Template name
406
     *
407
     * @throws InvalidArgumentException
408
     *
409
     * @return string|null
410
     */
411 4
    public function downloadTemplate($templateName)
412
    {
413 4
        $ret = null;
414
415 4
        StaticValidator::execute($templateName, 'TemplateName');
416
417
        $query = [
418 2
            'templateName' => $templateName,
419 1
        ];
420
421 2
        $data = $this->get('/templates/download', $query);
422
423 2
        if (null !== $data) {
424 2
            $ret = base64_decode($data);
425 1
        }
426
427 2
        return $ret;
428
    }
429
430
    /**
431
     * POST Methods
432
     * -----------------------------------------------------------------------------------------------------------------
433
     */
434
435
    /**
436
     * Upload a template to template storage
437
     *
438
     * @param string $templateFilename Template name
439
     *
440
     * @throws InvalidArgumentException
441
     *
442
     * @return bool
443
     */
444 26
    public function uploadTemplate($templateFilename)
445
    {
446 26
        $ret = false;
447
448 26
        StaticValidator::execute($templateFilename, 'TemplateExtension');
449 20
        StaticValidator::execute($templateFilename, 'FileExists');
450
451 18
        $templateFilename = realpath($templateFilename);
452 18
        $templateName     = basename($templateFilename);
453
454
        $query = [
455 18
            'templateName' => $templateName,
456 9
        ];
457
458 18
        $json = file_get_contents($templateFilename);
459 18
        $json = base64_encode($json);
460
461
        $options = [
462 18
            RequestOptions::QUERY => $query,
463 18
            RequestOptions::JSON  => $json,
464 9
        ];
465
466 18
        $response = $this->request('POST', $this->uri('/templates/upload'), $options);
467
468 18
        if ($response instanceof Response && 201 === $response->getStatusCode()) {
469 18
            $ret = true;
470 9
        }
471
472 18
        return $ret;
473
    }
474
475
    /**
476
     * Convert a document on the local file system to a different format
477
     *
478
     * @param string $documentFilename Document filename
479
     * @param string $returnFormat     Return format
480
     *
481
     * @throws InvalidArgumentException
482
     *
483
     * @return string|null
484
     */
485 12
    public function convertDocument($documentFilename, $returnFormat)
486
    {
487 12
        $ret = null;
488
489 12
        StaticValidator::execute($documentFilename, 'DocumentExtension');
490 6
        StaticValidator::execute($documentFilename, 'FileExists');
491 4
        StaticValidator::execute($returnFormat, 'ReturnFormat');
492
493
        $query = [
494 2
            'returnFormat' => $returnFormat,
495 1
        ];
496
497 2
        $documentFilename = realpath($documentFilename);
498
499 2
        $json = file_get_contents($documentFilename);
500 2
        $json = base64_encode($json);
501
502
        $options = [
503 2
            RequestOptions::QUERY => $query,
504 2
            RequestOptions::JSON  => $json,
505 1
        ];
506
507 2
        $response = $this->request('POST', $this->uri('/document/convert'), $options);
508
509 2
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
510 2
            $ret = base64_decode($response->getBody());
511 1
        }
512
513 2
        return $ret;
514
    }
515
516
    /**
517
     * Merge data into a template and return an array of binary data.
518
     * Each record in the array is the binary data of one document
519
     *
520
     * @param array  $mergeData        Array of merge data
521
     * @param string $returnFormat     Return format
522
     * @param string $templateName     Template name
523
     * @param string $templateFilename Template filename on local file system
524
     * @param bool   $append           Append flag
525
     * @param array  $mergeSettings    Array of merge settings
526
     *
527
     * @throws InvalidArgumentException
528
     *
529
     * @return array|null
530
     */
531 28
    public function mergeDocument(
532
        $mergeData,
533
        $returnFormat,
534
        $templateName = null,
535
        $templateFilename = null,
536
        $append = null,
537
        $mergeSettings = []
538
    ) {
539 28
        $ret = null;
540
541 28
        StaticValidator::execute($mergeData, 'TypeArray');
542 28
        StaticValidator::execute($returnFormat, 'ReturnFormat');
543
544 26
        if (null !== $templateName) {
545 4
            StaticValidator::execute($templateName, 'TemplateName');
546 1
        }
547
548 24
        if (null !== $templateFilename) {
549 22
            StaticValidator::execute($templateFilename, 'TemplateExtension');
550 16
            StaticValidator::execute($templateFilename, 'FileExists');
551 14
            $templateFilename = realpath($templateFilename);
552 7
        }
553
554 16
        if (null !== $append) {
555 14
            $append = StaticFilter::execute($append, 'BooleanToString');
556 6
        }
557
558 14
        StaticValidator::execute($mergeSettings, 'TypeArray');
559
560
        $query = [
561 12
            'returnFormat' => $returnFormat,
562 12
            'append'       => $append,
563 6
        ];
564
565 12
        if (null !== $templateName) {
566 2
            $query['templateName'] = $templateName;
567 1
        }
568
569
        $json = [
570 12
            'mergeData' => $mergeData,
571 6
        ];
572
573 12
        if (null !== $templateFilename) {
574 10
            $template         = file_get_contents($templateFilename);
575 10
            $template         = base64_encode($template);
576 10
            $json['template'] = $template;
577 5
        }
578
579 12
        if (count($mergeSettings) > 0) {
580 10
            $json['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings);
581 2
        }
582
583
        $options = [
584 6
            RequestOptions::QUERY => $query,
585 6
            RequestOptions::JSON  => $json,
586 3
        ];
587
588 6
        $response = $this->request('POST', $this->uri('/document/merge'), $options);
589
590 6
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
591 6
            $body = json_decode($response->getBody(), true);
592 6
            if (is_array($body) && count($body) > 0) {
593 6
                $ret = array_map('base64_decode', $body);
594 3
            }
595 3
        }
596
597 6
        return $ret;
598
    }
599
600
    /**
601
     * Perform find and replace in document and return binary data.
602
     *
603
     * @param array  $findAndReplaceData Array of find and replace data
604
     * @param string $returnFormat       Return format
605
     * @param string $templateName       Template name
606
     * @param string $templateFilename   Template filename on local file system
607
     * @param array  $mergeSettings      Array of merge settings
608
     *
609
     * @throws InvalidArgumentException
610
     *
611
     * @return string|null
612
     */
613 22
    public function findAndReplaceDocument(
614
        $findAndReplaceData,
615
        $returnFormat,
616
        $templateName = null,
617
        $templateFilename = null,
618
        $mergeSettings = []
619
    ) {
620 22
        $ret = null;
621
622 22
        StaticValidator::execute($findAndReplaceData, 'TypeArray');
623 22
        StaticValidator::execute($returnFormat, 'ReturnFormat');
624
625 20
        if (null !== $templateName) {
626 4
            StaticValidator::execute($templateName, 'TemplateName');
627 1
        }
628
629 18
        if (null !== $templateFilename) {
630 16
            StaticValidator::execute($templateFilename, 'TemplateExtension');
631 10
            StaticValidator::execute($templateFilename, 'FileExists');
632 8
            $templateFilename = realpath($templateFilename);
633 4
        }
634
635 10
        StaticValidator::execute($mergeSettings, 'TypeArray');
636
637
        $query = [
638 8
            'returnFormat' => $returnFormat,
639 4
        ];
640
641 8
        if (null !== $templateName) {
642 2
            $query['templateName'] = $templateName;
643 1
        }
644
645
        $json = [
646 8
            'findAndReplaceData' => $this->buildFindAndReplaceDataArray($findAndReplaceData),
647 4
        ];
648
649 8
        if (null !== $templateFilename) {
650 6
            $template         = file_get_contents($templateFilename);
651 6
            $template         = base64_encode($template);
652 6
            $json['template'] = $template;
653 3
        }
654
655 8
        if (count($mergeSettings) > 0) {
656 8
            $json['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings);
657 2
        }
658
659
        $options = [
660 4
            RequestOptions::QUERY => $query,
661 4
            RequestOptions::JSON  => $json,
662 2
        ];
663
664 4
        $response = $this->request('POST', $this->uri('/document/findandreplace'), $options);
665
666 4
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
667 4
            $ret = base64_decode($response->getBody());
668 2
        }
669
670 4
        return $ret;
671
    }
672
673
    /**
674
     * PUT Methods
675
     * -----------------------------------------------------------------------------------------------------------------
676
     */
677
678
    /**
679
     * Create an API key
680
     *
681
     * @return string|null
682
     */
683 6
    public function createApiKey()
684
    {
685 6
        $ret = null;
686
687 6
        $response = $this->request('PUT', $this->uri('/account/apikey'), []);
688
689 6
        if ($response instanceof Response && 201 === $response->getStatusCode()) {
690 6
            $ret = (string) json_decode($response->getBody(), true);
691 6
            StaticValidator::execute($ret, 'ApiKey');
692 3
        }
693
694 6
        return $ret;
695
    }
696
}
697