Test Failed
Push — master ( 300ee4...9f249c )
by Jonathan
05:51
created

ReportingCloud::getFontList()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 6
nc 2
nop 0
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 © 2016 Text Control GmbH
12
 */
13
namespace TxTextControl\ReportingCloud;
14
15
use GuzzleHttp\Psr7\Response;
16
use GuzzleHttp\RequestOptions;
17
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
18
use TxTextControl\ReportingCloud\Filter\StaticFilter;
19
use TxTextControl\ReportingCloud\PropertyMap\AccountSettings as AccountSettingsPropertyMap;
20
use TxTextControl\ReportingCloud\PropertyMap\TemplateInfo as TemplateInfoPropertyMap;
21
use TxTextControl\ReportingCloud\PropertyMap\TemplateList as TemplateListPropertyMap;
22
use TxTextControl\ReportingCloud\Validator\StaticValidator;
23
24
/**
25
 * ReportingCloud
26
 *
27
 * @package TxTextControl\ReportingCloud
28
 * @author  Jonathan Maron (@JonathanMaron)
29
 */
30
class ReportingCloud extends AbstractReportingCloud
31
{
32
33
    /**
34
     * GET methods
35
     * =================================================================================================================
36
     */
37
38
    /**
39
     * Return an array of merge blocks and merge fields in a template file in template storage.
40
     *
41
     * @param string  $templateName Template name
42
     *
43
     * @throws InvalidArgumentException
44
     *
45
     * @return array|null
46
     */
47 1
    public function getTemplateInfo($templateName)
48
    {
49 1
        $ret = null;
50
51 1
        $propertyMap = new TemplateInfoPropertyMap();
52
53 1
        StaticValidator::execute($templateName, 'TemplateName');
54
55
        $query = [
56 1
            'templateName' => $templateName,
57 1
        ];
58
59 1
        $records = $this->get('/templates/info', $query);
60
61 1
        if (is_array($records) && count($records) > 0) {
62 1
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
63 1
        }
64
65 1
        return $ret;
66
    }
67
68
    /**
69
     * Return an array of binary data.
70
     * Each record in the array is the binary data of a thumbnail image
71
     *
72
     * @param string  $templateName Template name
73
     * @param integer $zoomFactor   Zoom factor
74
     * @param integer $fromPage     From page
75
     * @param integer $toPage       To page
76
     * @param string  $imageFormat  Image format
77
     *
78
     * @throws InvalidArgumentException
79
     *
80
     * @return array|null
81
     */
82 6
    public function getTemplateThumbnails($templateName, $zoomFactor, $fromPage, $toPage, $imageFormat)
83
    {
84 6
        $ret = null;
85
86 6
        StaticValidator::execute($templateName, 'TemplateName');
87 5
        StaticValidator::execute($zoomFactor  , 'ZoomFactor');
88 4
        StaticValidator::execute($fromPage    , 'Page');
89 3
        StaticValidator::execute($toPage      , 'Page');
90 2
        StaticValidator::execute($imageFormat , 'ImageFormat');
91
92
        $query = [
93 1
            'templateName' => $templateName,
94 1
            'zoomFactor'   => $zoomFactor,
95 1
            'fromPage'     => $fromPage,
96 1
            'toPage'       => $toPage,
97 1
            'imageFormat'  => $imageFormat,
98 1
        ];
99
100 1
        $records = $this->get('/templates/thumbnails', $query);
101
102 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...
103 1
            $ret = array_map('base64_decode', $records);
104 1
        }
105
106 1
        return $ret;
107
    }
108
109
    /**
110
     * Return the number of templates in template storage
111
     *
112
     * @return integer
113
     */
114 1
    public function getTemplateCount()
115
    {
116 1
        return (integer) $this->get('/templates/count');
117
    }
118
119
    /**
120
     * Return an array properties for the templates in template storage
121
     *
122
     * @return array|null
123
     */
124 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...
125
    {
126 1
        $ret = null;
127
128 1
        $propertyMap = new TemplateListPropertyMap();
129
130 1
        $records = $this->get('/templates/list');
131
132 1
        if (is_array($records) && count($records) > 0) {
133 1
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
134 1
            array_walk($ret, function (&$record) {
135 1
                $key = 'modified';
136 1
                if (isset($record[$key])) {
137 1
                    $record[$key] = StaticFilter::execute($record[$key], 'DateTimeToTimestamp');
138 1
                }
139 1
            });
140 1
        }
141
142 1
        return $ret;
143
    }
144
145
    /**
146
     * Return the number of pages in a template in template storage
147
     *
148
     * @param string $templateName Template name
149
     *
150
     * @throws InvalidArgumentException
151
     *
152
     * @return integer
153
     */
154 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...
155
    {
156 2
        StaticValidator::execute($templateName, 'TemplateName');
157
158
        $query = [
159 1
            'templateName' => $templateName,
160 1
        ];
161
162 1
        return (integer) $this->get('/templates/pagecount', $query);
163
    }
164
165
    /**
166
     * Return true, if the template exists in template storage
167
     *
168
     * @param string $templateName Template name
169
     *
170
     * @throws InvalidArgumentException
171
     *
172
     * @return bool
173
     */
174 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...
175
    {
176 2
        StaticValidator::execute($templateName, 'TemplateName');
177
178
        $query = [
179 1
            'templateName' => $templateName,
180 1
        ];
181
182 1
        return (boolean) $this->get('/templates/exists', $query);
183
    }
184
185
    /**
186
     * Return an array of available fonts on the Reporting Cloud service
187
     *
188
     * @return array|null
189
     */
190 1
    public function getFontList()
191
    {
192 1
        $ret = null;
193
194 1
        $fonts = $this->get('/fonts/list');
195
196 1
        if (is_array($fonts) && count($fonts) > 0) {
197
            $ret = array_map('trim', $fonts);
198 1
        }
199 1
200 1
        return $ret;
201 1
    }
202 1
203 1
    /**
204 1
     * Return an array properties for the ReportingCloud account
205
     *
206 1
     * @return array|null
207
     */
208 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...
209
    {
210
        $ret = null;
211
212
        $propertyMap = new AccountSettingsPropertyMap();
213
214
        $records = $this->get('/account/settings');
215
216
        if (is_array($records) && count($records) > 0) {
217
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
218 2
            $key = 'valid_until';
219
            if ($ret[$key]) {
220 2
                $ret[$key] = StaticFilter::execute($ret[$key], 'DateTimeToTimestamp');
221
            }
222 2
        }
223
224
        return $ret;
225 1
    }
226 1
227
    /**
228 1
     * Download the binary data of a template from template storage
229
     *
230 1
     * @param string $templateName Template name
231 1
     *
232 1
     * @throws InvalidArgumentException
233
     *
234 1
     * @return null|resource
235
     */
236
    public function downloadTemplate($templateName)
237
    {
238
        $ret = null;
239
240
        StaticValidator::execute($templateName, 'TemplateName');
241
242
        $query = [
243
            'templateName' => $templateName,
244
        ];
245 8
246
        $data = $this->get('/templates/download', $query);
247 8
248
        if (null !== $data) {
249
            $ret = base64_decode($data);
250 8
        }
251 8
252
        return $ret;
253 8
    }
254
255 8
    /**
256 8
     * Execute a GET request via REST client
257 8
     *
258
     * @param string $uri   URI
259 8
     * @param array  $query Query
260
     *
261
     * @return mixed|null
262
     */
263
    protected function get($uri, $query = [])
264
    {
265
        $ret = null;
266
267
        $options = [
268
            RequestOptions::QUERY => $query,
269
        ];
270
271
        $response = $this->request('GET', $this->uri($uri), $options);
272
273
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
274
            $ret  = json_decode($response->getBody(), true);
275
        }
276
277 13
        return $ret;
278
    }
279 13
280
281 13
    /**
282 10
     * POST methods
283
     * =================================================================================================================
284 9
     */
285 9
286
    /**
287
     * Upload a template to template storage
288 9
     *
289 9
     * @param string $templateFilename Template name
290
     *
291 9
     * @throws InvalidArgumentException
292 9
     *
293
     * @return bool
294
     */
295 9
    public function uploadTemplate($templateFilename)
296 9
    {
297 9
        $ret = false;
298
299 9
        StaticValidator::execute($templateFilename, 'TemplateExtension');
300
        StaticValidator::execute($templateFilename, 'FileExists');
301 9
302 9
        $templateFilename = realpath($templateFilename);
303 9
        $templateName     = basename($templateFilename);
304
305 9
        $query = [
306
            'templateName' => $templateName,
307
        ];
308
309
        $json = file_get_contents($templateFilename);
310
        $json = base64_encode($json);
311
312
        $options = [
313
            RequestOptions::QUERY => $query,
314
            RequestOptions::JSON  => $json,
315
        ];
316
317
        $response = $this->request('POST', $this->uri('/templates/upload'), $options);
318 6
319
        if ($response instanceof Response && 201 === $response->getStatusCode()) {
320 6
            $ret = true;
321
        }
322 6
323 3
        return $ret;
324 2
    }
325
326
    /**
327 1
     * Convert a document on the local file system to a different format
328 1
     *
329
     * @param string $documentFilename Document filename
330 1
     * @param string $returnFormat     Return format
331
     *
332 1
     * @throws InvalidArgumentException
333 1
     *
334
     * @return null|resource
335
     */
336 1
    public function convertDocument($documentFilename, $returnFormat)
337 1
    {
338 1
        $ret = null;
339
340 1
        StaticValidator::execute($documentFilename, 'DocumentExtension');
341
        StaticValidator::execute($documentFilename, 'FileExists');
342 1
        StaticValidator::execute($returnFormat    , 'ReturnFormat');
343 1
344 1
        $query = [
345
            'returnFormat' => $returnFormat,
346 1
        ];
347
348
        $documentFilename = realpath($documentFilename);
349
350
        $json = file_get_contents($documentFilename);
351
        $json = base64_encode($json);
352
353
        $options = [
354
            RequestOptions::QUERY => $query,
355
            RequestOptions::JSON  => $json,
356
        ];
357
358
        $response = $this->request('POST', $this->uri('/document/convert'), $options);
359
360
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
361
            $ret  = base64_decode($response->getBody());
362
        }
363
364 13
        return $ret;
365
    }
366
367 13
    /**
368
     * Merge data into a template and return an array of binary data.
369 13
     * Each record in the array is the binary data of one document
370 13
     *
371
     * @param array   $mergeData        Array of merge data
372 12
     * @param string  $returnFormat     Return format
373 2
     * @param string  $templateName     Template name
374 1
     * @param string  $templateFilename Template filename on local file system
375
     * @param boolean $append           Append flag
376 11
     * @param array   $mergeSettings    Array of merge settings
377 10
     *
378 7
     * @throws InvalidArgumentException
379 6
     *
380 6
     * @return null|string
381
     */
382 7
    public function mergeDocument($mergeData, $returnFormat, $templateName = null, $templateFilename = null,
383 6
                                    $append = null, $mergeSettings = [])
384 5
    {
385
        $ret = null;
386 6
387
        StaticValidator::execute($mergeData   , 'TypeArray');
388
        StaticValidator::execute($returnFormat, 'ReturnFormat');
389 5
390 5
        if (null !== $templateName) {
391 5
            StaticValidator::execute($templateName, 'TemplateName');
392
        }
393 5
394 1 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...
395 1
            StaticValidator::execute($templateFilename, 'TemplateExtension');
396
            StaticValidator::execute($templateFilename, 'FileExists');
397
            $templateFilename = realpath($templateFilename);
398 5
        }
399 5
400
        if (null !== $append) {
401 5
            $append = StaticFilter::execute($append, 'BooleanToString');
402 4
        }
403 4
404 4
        StaticValidator::execute($mergeSettings, 'TypeArray');
405 4
406
        $query = [
407 5
            'returnFormat' => $returnFormat,
408 4
            'append'       => $append,
409 2
        ];
410
411
        if (null !== $templateName) {
412 3
            $query['templateName'] = $templateName;
413 3
        }
414 3
415
        $json = [
416 3
            'mergeData' => $mergeData,
417
        ];
418 3
419 3 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...
420 3
            $template = file_get_contents($templateFilename);
421 3
            $template = base64_encode($template);
422 3
            $json['template'] = $template;
423 3
        }
424
425 3
        if (count($mergeSettings) > 0) {
426
            $json['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings);
427
        }
428
429
        $options = [
430
            RequestOptions::QUERY => $query,
431
            RequestOptions::JSON  => $json,
432
        ];
433
434
        $response = $this->request('POST', $this->uri('/document/merge'), $options);
435
436
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
437
            $body = json_decode($response->getBody(), true);
438 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...
439
                $ret = array_map('base64_decode', $body);
440
            }
441 11
        }
442
443
        return $ret;
444 11
    }
445
446 11
    /**
447 11
     * Perform find and replace in document and return binary data.
448
     *
449 10
     * @param array  $findAndReplaceData Array of find and replace data
450 2
     * @param string $returnFormat       Return format
451 1
     * @param string $templateName       Template name
452
     * @param string $templateFilename   Template filename on local file system
453 9
     * @param array  $mergeSettings      Array of merge settings
454 8
     *
455 5
     * @throws InvalidArgumentException
456 4
     *
457 4
     * @return null|string
458
     */
459 5
    public function findAndReplaceDocument($findAndReplaceData, $returnFormat, $templateName = null,
460
                                           $templateFilename = null, $mergeSettings = [])
461
    {
462 4
        $ret = null;
463 4
464
        StaticValidator::execute($findAndReplaceData, 'TypeArray');
465 4
        StaticValidator::execute($returnFormat      , 'ReturnFormat');
466 1
467 1
        if (null !== $templateName) {
468
            StaticValidator::execute($templateName, 'TemplateName');
469
        }
470 4
471 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...
472
            StaticValidator::execute($templateFilename, 'TemplateExtension');
473 4
            StaticValidator::execute($templateFilename, 'FileExists');
474 3
            $templateFilename = realpath($templateFilename);
475 3
        }
476 3
477 3
        StaticValidator::execute($mergeSettings, 'TypeArray');
478
479 4
        $query = [
480 4
            'returnFormat' => $returnFormat,
481 2
        ];
482
483
        if (null !== $templateName) {
484 2
            $query['templateName'] = $templateName;
485 2
        }
486 2
487
        $json = [
488 2
            'findAndReplaceData' => $this->buildFindAndReplaceDataArray($findAndReplaceData),
489
        ];
490 2
491 2 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...
492 2
            $template = file_get_contents($templateFilename);
493
            $template = base64_encode($template);
494 2
            $json['template'] = $template;
495
        }
496
497
        if (count($mergeSettings) > 0) {
498
            $json['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings);
499
        }
500
501
        $options = [
502
            RequestOptions::QUERY => $query,
503
            RequestOptions::JSON  => $json,
504
        ];
505
506
        $response = $this->request('POST', $this->uri('/document/findandreplace'), $options);
507
508
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
509
            $ret  = base64_decode($response->getBody());
510
        }
511
512 11
        return $ret;
513
    }
514 11
515
516 11
    /**
517
     * DELETE methods
518
     * =================================================================================================================
519 10
     */
520 10
521
    /**
522
     * Delete a template in template storage
523 10
     *
524 10
     * @param string $templateName Template name
525
     *
526 10
     * @throws InvalidArgumentException
527
     *
528 9
     * @return bool
529 9
     */
530 9
    public function deleteTemplate($templateName)
531
    {
532 9
        $ret = false;
533
534
        StaticValidator::execute($templateName, 'TemplateName');
535
536
        $query = [
537
            'templateName' => $templateName,
538
        ];
539
540
        $options = [
541
            RequestOptions::QUERY => $query,
542
        ];
543
544
        $response = $this->request('DELETE', $this->uri('/templates/delete'), $options);
545
546
        if ($response instanceof Response && 204 === $response->getStatusCode()) {
547
            $ret = true;
548
        }
549
550
        return $ret;
551
    }
552
}