Passed
Pull Request — master (#7)
by Jonathan
06:00
created

ReportingCloud::proofingCheck()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 3
Ratio 13.64 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 3
loc 22
ccs 13
cts 13
cp 1
rs 9.2
cc 3
eloc 12
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 © 2017 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\TemplateInfo as TemplateInfoPropertyMap;
22
use TxTextControl\ReportingCloud\PropertyMap\TemplateList as TemplateListPropertyMap;
23
use TxTextControl\ReportingCloud\PropertyMap\IncorrectWord as IncorrectWordMap;
24
use TxTextControl\ReportingCloud\Validator\StaticValidator;
25
26
/**
27
 * ReportingCloud
28
 *
29
 * @package TxTextControl\ReportingCloud
30
 * @author  Jonathan Maron (@JonathanMaron)
31
 */
32
class ReportingCloud extends AbstractReportingCloud
33
{
34
    /**
35
     * GET methods
36
     * =================================================================================================================
37
     */
38
39
    /**
40
     * Check a corpus of text for spelling errors.
41
     *
42
     * Return an array of misspelled words, if spelling errors are found in the corpus of text.
43
     *
44
     * Return null, if no misspelled words are found in the corpus of text.
45
     *
46
     * @param string $text Corpus of text that should be spell checked
47
     * @param string $language Language of specified text
48
     *
49
     * @return array|null
50
     */
51 1
    public function proofingCheck($text, $language)
52
    {
53 1
        $ret = null;
54
55 1
        StaticValidator::execute($text, 'TypeString');
56 1
        StaticValidator::execute($language, 'Language');
57
58 1
        $propertyMap = new IncorrectWordMap();
59
60
        $query = [
61 1
            'text'     => $text,
62 1
            'language' => $language,
63 1
        ];
64
65 1
        $records = $this->get('/proofing/check', $query);
66
67 1 View Code Duplication
        if (is_array($records) && count($records) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68 1
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
69 1
        }
70
71 1
        return $ret;
72
    }
73
74
    /**
75
     * Return an array of available dictionaries on the Reporting Cloud service
76
     *
77
     * @return array|null
78
     */
79 1 View Code Duplication
    public function getAvailableDictionaries()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81 1
        $ret = null;
82
83 1
        $dictionaries = $this->get('/proofing/availabledictionaries');
84
85 1
        if (is_array($dictionaries) && count($dictionaries) > 0) {
86 1
            $ret = array_map('trim', $dictionaries);
87 1
        }
88
89 1
        return $ret;
90
    }
91
92
    /**
93
     * Return an array of suggestions for a misspelled word.
94
     *
95
     * @param string $word Word that should be spell checked
96
     * @param string $language Language of specified text
97
     * @param int $max Maximum number of suggestions to return
98
     *
99
     * @return array|null
100
     */
101 1
    public function getProofingSuggestions($word, $language, $max = 10)
102
    {
103 1
        $ret = null;
104
105 1
        StaticValidator::execute($word, 'TypeString');
106 1
        StaticValidator::execute($language, 'Language');
107 1
        StaticValidator::execute($max, 'TypeInteger');
108
109
        $query = [
110 1
            'word'     => $word,
111 1
            'language' => $language,
112 1
            'max'      => $max,
113 1
        ];
114
115 1
        $records = $this->get('/proofing/suggestions', $query);
116
117 1
        if (is_array($records) && count($records) > 0) {
118 1
            $ret = array_map('trim', $records);
119 1
        }
120
121 1
        return $ret;
122
    }
123
124
    /**
125
     * Return an array of merge blocks and merge fields in a template file in template storage.
126
     *
127
     * @param string $templateName Template name
128
     *
129
     * @throws InvalidArgumentException
130
     *
131
     * @return array|null
132
     */
133 1
    public function getTemplateInfo($templateName)
134
    {
135 1
        $ret = null;
136
137 1
        $propertyMap = new TemplateInfoPropertyMap();
138
139 1
        StaticValidator::execute($templateName, 'TemplateName');
140
141
        $query = [
142 1
            'templateName' => $templateName,
143 1
        ];
144
145 1
        $records = $this->get('/templates/info', $query);
146
147 1 View Code Duplication
        if (is_array($records) && count($records) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148 1
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
149 1
        }
150
151 1
        return $ret;
152
    }
153
154
    /**
155
     * Return an array of binary data.
156
     * Each record in the array is the binary data of a thumbnail image
157
     *
158
     * @param string $templateName Template name
159
     * @param integer $zoomFactor Zoom factor
160
     * @param integer $fromPage From page
161
     * @param integer $toPage To page
162
     * @param string $imageFormat Image format
163
     *
164
     * @throws InvalidArgumentException
165
     *
166
     * @return array|null
167
     */
168 6
    public function getTemplateThumbnails($templateName, $zoomFactor, $fromPage, $toPage, $imageFormat)
169
    {
170 6
        $ret = null;
171
172 6
        StaticValidator::execute($templateName, 'TemplateName');
173 5
        StaticValidator::execute($zoomFactor, 'ZoomFactor');
174 4
        StaticValidator::execute($fromPage, 'Page');
175 3
        StaticValidator::execute($toPage, 'Page');
176 2
        StaticValidator::execute($imageFormat, 'ImageFormat');
177
178
        $query = [
179 1
            'templateName' => $templateName,
180 1
            'zoomFactor'   => $zoomFactor,
181 1
            'fromPage'     => $fromPage,
182 1
            'toPage'       => $toPage,
183 1
            'imageFormat'  => $imageFormat,
184 1
        ];
185
186 1
        $records = $this->get('/templates/thumbnails', $query);
187
188 1 View Code Duplication
        if (is_array($records) && count($records) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
189 1
            $ret = array_map('base64_decode', $records);
190 1
        }
191
192 1
        return $ret;
193
    }
194
195
    /**
196
     * Return the number of templates in template storage
197
     *
198
     * @return integer
199
     */
200 1
    public function getTemplateCount()
201
    {
202 1
        return (integer) $this->get('/templates/count');
203
    }
204
205
    /**
206
     * Return an array properties for the templates in template storage
207
     *
208
     * @return array|null
209
     */
210 1 View Code Duplication
    public function getTemplateList()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
211
    {
212 1
        $ret = null;
213
214 1
        $propertyMap = new TemplateListPropertyMap();
215
216 1
        $records = $this->get('/templates/list');
217
218 1
        if (is_array($records) && count($records) > 0) {
219 1
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
220 1
            array_walk($ret, function (&$record) {
221 1
                $key = 'modified';
222 1
                if (isset($record[$key])) {
223 1
                    $record[$key] = StaticFilter::execute($record[$key], 'DateTimeToTimestamp');
224 1
                }
225 1
            });
226 1
        }
227
228 1
        return $ret;
229
    }
230
231
    /**
232
     * Return the number of pages in a template in template storage
233
     *
234
     * @param string $templateName Template name
235
     *
236
     * @throws InvalidArgumentException
237
     *
238
     * @return integer
239
     */
240 2 View Code Duplication
    public function getTemplatePageCount($templateName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
241
    {
242 2
        StaticValidator::execute($templateName, 'TemplateName');
243
244
        $query = [
245 1
            'templateName' => $templateName,
246 1
        ];
247
248 1
        return (integer) $this->get('/templates/pagecount', $query);
249
    }
250
251
    /**
252
     * Return true, if the template exists in template storage
253
     *
254
     * @param string $templateName Template name
255
     *
256
     * @throws InvalidArgumentException
257
     *
258
     * @return bool
259
     */
260 2 View Code Duplication
    public function templateExists($templateName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
261
    {
262 2
        StaticValidator::execute($templateName, 'TemplateName');
263
264
        $query = [
265 1
            'templateName' => $templateName,
266 1
        ];
267
268 1
        return (boolean) $this->get('/templates/exists', $query);
269
    }
270
271
    /**
272
     * Return an array of available fonts on the Reporting Cloud service
273
     *
274
     * @return array|null
275
     */
276 1 View Code Duplication
    public function getFontList()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
277
    {
278 1
        $ret = null;
279
280 1
        $fonts = $this->get('/fonts/list');
281
282 1
        if (is_array($fonts) && count($fonts) > 0) {
283 1
            $ret = array_map('trim', $fonts);
284 1
        }
285
286 1
        return $ret;
287
    }
288
289
    /**
290
     * Return an array properties for the ReportingCloud account
291
     *
292
     * @return array|null
293
     */
294 1 View Code Duplication
    public function getAccountSettings()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
295
    {
296 1
        $ret = null;
297
298 1
        $propertyMap = new AccountSettingsPropertyMap();
299
300 1
        $records = $this->get('/account/settings');
301
302 1
        if (is_array($records) && count($records) > 0) {
303 1
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
304 1
            $key = 'valid_until';
305 1
            if ($ret[$key]) {
306 1
                $ret[$key] = StaticFilter::execute($ret[$key], 'DateTimeToTimestamp');
307 1
            }
308 1
        }
309
310 1
        return $ret;
311
    }
312
313
    /**
314
     * Download the binary data of a template from template storage
315
     *
316
     * @param string $templateName Template name
317
     *
318
     * @throws InvalidArgumentException
319
     *
320
     * @return null|resource
321
     */
322 2
    public function downloadTemplate($templateName)
323
    {
324 2
        $ret = null;
325
326 2
        StaticValidator::execute($templateName, 'TemplateName');
327
328
        $query = [
329 1
            'templateName' => $templateName,
330 1
        ];
331
332 1
        $data = $this->get('/templates/download', $query);
333
334 1
        if (null !== $data) {
335 1
            $ret = base64_decode($data);
336 1
        }
337
338 1
        return $ret;
339
    }
340
341
    /**
342
     * Execute a GET request via REST client
343
     *
344
     * @param string $uri URI
345
     * @param array $query Query
346
     *
347
     * @return mixed|null
348
     */
349 12
    protected function get($uri, $query = [])
350
    {
351 12
        $ret = null;
352
353
        $options = [
354 12
            RequestOptions::QUERY => $query,
355 12
        ];
356
357 12
        $response = $this->request('GET', $this->uri($uri), $options);
358
359 12
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
360 12
            $ret = json_decode($response->getBody(), true);
361 12
        }
362
363 12
        return $ret;
364
    }
365
366
367
    /**
368
     * POST methods
369
     * =================================================================================================================
370
     */
371
372
    /**
373
     * Upload a template to template storage
374
     *
375
     * @param string $templateFilename Template name
376
     *
377
     * @throws InvalidArgumentException
378
     *
379
     * @return bool
380
     */
381 13
    public function uploadTemplate($templateFilename)
382
    {
383 13
        $ret = false;
384
385 13
        StaticValidator::execute($templateFilename, 'TemplateExtension');
386 10
        StaticValidator::execute($templateFilename, 'FileExists');
387
388 9
        $templateFilename = realpath($templateFilename);
389 9
        $templateName     = basename($templateFilename);
390
391
        $query = [
392 9
            'templateName' => $templateName,
393 9
        ];
394
395 9
        $json = file_get_contents($templateFilename);
396 9
        $json = base64_encode($json);
397
398
        $options = [
399 9
            RequestOptions::QUERY => $query,
400 9
            RequestOptions::JSON  => $json,
401 9
        ];
402
403 9
        $response = $this->request('POST', $this->uri('/templates/upload'), $options);
404
405 9
        if ($response instanceof Response && 201 === $response->getStatusCode()) {
406 9
            $ret = true;
407 9
        }
408
409 9
        return $ret;
410
    }
411
412
    /**
413
     * Convert a document on the local file system to a different format
414
     *
415
     * @param string $documentFilename Document filename
416
     * @param string $returnFormat Return format
417
     *
418
     * @throws InvalidArgumentException
419
     *
420
     * @return null|resource
421
     */
422 6
    public function convertDocument($documentFilename, $returnFormat)
423
    {
424 6
        $ret = null;
425
426 6
        StaticValidator::execute($documentFilename, 'DocumentExtension');
427 3
        StaticValidator::execute($documentFilename, 'FileExists');
428 2
        StaticValidator::execute($returnFormat, 'ReturnFormat');
429
430
        $query = [
431 1
            'returnFormat' => $returnFormat,
432 1
        ];
433
434 1
        $documentFilename = realpath($documentFilename);
435
436 1
        $json = file_get_contents($documentFilename);
437 1
        $json = base64_encode($json);
438
439
        $options = [
440 1
            RequestOptions::QUERY => $query,
441 1
            RequestOptions::JSON  => $json,
442 1
        ];
443
444 1
        $response = $this->request('POST', $this->uri('/document/convert'), $options);
445
446 1
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
447 1
            $ret = base64_decode($response->getBody());
448 1
        }
449
450 1
        return $ret;
451
    }
452
453
    /**
454
     * Merge data into a template and return an array of binary data.
455
     * Each record in the array is the binary data of one document
456
     *
457
     * @param array $mergeData Array of merge data
458
     * @param string $returnFormat Return format
459
     * @param string $templateName Template name
460
     * @param string $templateFilename Template filename on local file system
461
     * @param boolean $append Append flag
462
     * @param array $mergeSettings Array of merge settings
463
     *
464
     * @throws InvalidArgumentException
465
     *
466
     * @return null|string
467
     */
468 13
    public function mergeDocument($mergeData, $returnFormat, $templateName = null, $templateFilename = null, $append = null, $mergeSettings = [])
469
    {
470 13
        $ret = null;
471
472 13
        StaticValidator::execute($mergeData, 'TypeArray');
473 13
        StaticValidator::execute($returnFormat, 'ReturnFormat');
474
475 12
        if (null !== $templateName) {
476 2
            StaticValidator::execute($templateName, 'TemplateName');
477 1
        }
478
479 11 View Code Duplication
        if (null !== $templateFilename) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
480 10
            StaticValidator::execute($templateFilename, 'TemplateExtension');
481 7
            StaticValidator::execute($templateFilename, 'FileExists');
482 6
            $templateFilename = realpath($templateFilename);
483 6
        }
484
485 7
        if (null !== $append) {
486 6
            $append = StaticFilter::execute($append, 'BooleanToString');
487 5
        }
488
489 6
        StaticValidator::execute($mergeSettings, 'TypeArray');
490
491
        $query = [
492 5
            'returnFormat' => $returnFormat,
493 5
            'append'       => $append,
494 5
        ];
495
496 5
        if (null !== $templateName) {
497 1
            $query['templateName'] = $templateName;
498 1
        }
499
500
        $json = [
501 5
            'mergeData' => $mergeData,
502 5
        ];
503
504 5 View Code Duplication
        if (null !== $templateFilename) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
505 4
            $template         = file_get_contents($templateFilename);
506 4
            $template         = base64_encode($template);
507 4
            $json['template'] = $template;
508 4
        }
509
510 5
        if (count($mergeSettings) > 0) {
511 4
            $json['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings);
512 2
        }
513
514
        $options = [
515 3
            RequestOptions::QUERY => $query,
516 3
            RequestOptions::JSON  => $json,
517 3
        ];
518
519 3
        $response = $this->request('POST', $this->uri('/document/merge'), $options);
520
521 3
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
522 3
            $body = json_decode($response->getBody(), true);
523 3 View Code Duplication
            if (is_array($body) && count($body) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
524 3
                $ret = array_map('base64_decode', $body);
525 3
            }
526 3
        }
527
528 3
        return $ret;
529
    }
530
531
    /**
532
     * Perform find and replace in document and return binary data.
533
     *
534
     * @param array $findAndReplaceData Array of find and replace data
535
     * @param string $returnFormat Return format
536
     * @param string $templateName Template name
537
     * @param string $templateFilename Template filename on local file system
538
     * @param array $mergeSettings Array of merge settings
539
     *
540
     * @throws InvalidArgumentException
541
     *
542
     * @return null|string
543
     */
544 11
    public function findAndReplaceDocument($findAndReplaceData, $returnFormat, $templateName = null, $templateFilename = null, $mergeSettings = [])
545
    {
546 11
        $ret = null;
547
548 11
        StaticValidator::execute($findAndReplaceData, 'TypeArray');
549 11
        StaticValidator::execute($returnFormat, 'ReturnFormat');
550
551 10
        if (null !== $templateName) {
552 2
            StaticValidator::execute($templateName, 'TemplateName');
553 1
        }
554
555 9 View Code Duplication
        if (null !== $templateFilename) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
556 8
            StaticValidator::execute($templateFilename, 'TemplateExtension');
557 5
            StaticValidator::execute($templateFilename, 'FileExists');
558 4
            $templateFilename = realpath($templateFilename);
559 4
        }
560
561 5
        StaticValidator::execute($mergeSettings, 'TypeArray');
562
563
        $query = [
564 4
            'returnFormat' => $returnFormat,
565 4
        ];
566
567 4
        if (null !== $templateName) {
568 1
            $query['templateName'] = $templateName;
569 1
        }
570
571
        $json = [
572 4
            'findAndReplaceData' => $this->buildFindAndReplaceDataArray($findAndReplaceData),
573 4
        ];
574
575 4 View Code Duplication
        if (null !== $templateFilename) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
576 3
            $template         = file_get_contents($templateFilename);
577 3
            $template         = base64_encode($template);
578 3
            $json['template'] = $template;
579 3
        }
580
581 4
        if (count($mergeSettings) > 0) {
582 4
            $json['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings);
583 2
        }
584
585
        $options = [
586 2
            RequestOptions::QUERY => $query,
587 2
            RequestOptions::JSON  => $json,
588 2
        ];
589
590 2
        $response = $this->request('POST', $this->uri('/document/findandreplace'), $options);
591
592 2
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
593 2
            $ret = base64_decode($response->getBody());
594 2
        }
595
596 2
        return $ret;
597
    }
598
599
600
    /**
601
     * DELETE methods
602
     * =================================================================================================================
603
     */
604
605
    /**
606
     * Delete a template in template storage
607
     *
608
     * @param string $templateName Template name
609
     *
610
     * @throws InvalidArgumentException
611
     *
612
     * @return bool
613
     */
614 11
    public function deleteTemplate($templateName)
615
    {
616 11
        $ret = false;
617
618 11
        StaticValidator::execute($templateName, 'TemplateName');
619
620
        $query = [
621 10
            'templateName' => $templateName,
622 10
        ];
623
624
        $options = [
625 10
            RequestOptions::QUERY => $query,
626 10
        ];
627
628 10
        $response = $this->request('DELETE', $this->uri('/templates/delete'), $options);
629
630 9
        if ($response instanceof Response && 204 === $response->getStatusCode()) {
631 9
            $ret = true;
632 9
        }
633
634 9
        return $ret;
635
    }
636
}