Completed
Push — master ( b10734...824501 )
by Jonathan
09:23
created

ReportingCloud::getAccountSettings()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 20
Ratio 100 %

Code Coverage

Tests 12
CRAP Score 4.0466

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 20
loc 20
ccs 12
cts 14
cp 0.8571
rs 9.2
cc 4
eloc 11
nc 2
nop 0
crap 4.0466
1
<?php
2
3
/**
4
 * ReportingCloud PHP Wrapper
5
 *
6
 * Official wrapper (authored by Text Control GmbH, publisher of ReportingCloud) to access ReportingCloud in PHP.
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\PropertyMap\AccountSettings as AccountSettingsPropertyMap;
19
use TxTextControl\ReportingCloud\PropertyMap\TemplateInfo as TemplateInfoPropertyMap;
20
use TxTextControl\ReportingCloud\PropertyMap\TemplateList as TemplateListPropertyMap;
21
use TxTextControl\ReportingCloud\Validator\StaticValidator;
22
use TxTextControl\ReportingCloud\Filter\StaticFilter;
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|null
113
     */
114 1
    public function getTemplateCount()
115
    {
116 1
        return $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
            array_walk($ret, function (&$record) {
135 1
                $key = 'modified';
136 1
                if (isset($record[$key])) {
137 1
                    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 bool
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 properties for the ReportingCloud account
187
     *
188
     * @return array|null
189
     */
190 3 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...
191
    {
192 3
        $ret = null;
193
194 3
        $propertyMap = new AccountSettingsPropertyMap();
195
196 3
        $records = $this->get('/account/settings');
197
198 3
        if (is_array($records) && count($records) > 0) {
199 3
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
200 3
            array_walk($ret, function (&$record) {
201 3
                $key = 'valid_until';
202 3
                if (isset($record[$key])) {
203
                    StaticFilter::execute($record[$key], 'DateTimeToTimestamp');
204
                }
205 3
            });
206 3
        }
207
208 3
        return $ret;
209
    }
210
211
    /**
212
     * Download the binary data of a template from template storage
213
     *
214
     * @param string $templateName Template name
215
     *
216
     * @throws InvalidArgumentException
217
     *
218
     * @return null|resource
219
     */
220 2
    public function downloadTemplate($templateName)
221
    {
222 2
        $ret = null;
223
224 2
        StaticValidator::execute($templateName, 'TemplateName');
225
226
        $query = [
227 1
            'templateName' => $templateName,
228 1
        ];
229
230 1
        $data = $this->get('/templates/download', $query);
231
232 1
        if (null !== $data) {
233 1
            $ret = base64_decode($data);
234 1
        }
235
236 1
        return $ret;
237
    }
238
239
    /**
240
     * Execute a GET request via REST client
241
     *
242
     * @param string $uri   URI
243
     * @param array  $query Query
244
     *
245
     * @return mixed|null
246
     */
247 10
    protected function get($uri, $query = [])
248
    {
249 10
        $ret = null;
250
251
        $options = [
252 10
            RequestOptions::QUERY => $query,
253 10
        ];
254
255 10
        $response = $this->request('GET', $this->uri($uri), $options);
256
257 10
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
258 10
            $body = (string) $response->getBody();
259 10
            $ret  = json_decode($body, true);
260 10
        }
261
262 10
        return $ret;
263
    }
264
265
266
    /**
267
     * POST methods
268
     * =================================================================================================================
269
     */
270
271
    /**
272
     * Upload a template to template storage
273
     *
274
     * @param string $templateFilename Template name
275
     *
276
     * @throws InvalidArgumentException
277
     *
278
     * @return bool
279
     */
280 13 View Code Duplication
    public function uploadTemplate($templateFilename)
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...
281
    {
282 13
        $ret = false;
283
284 13
        StaticValidator::execute($templateFilename, 'TemplateExtension');
285 10
        StaticValidator::execute($templateFilename, 'FileExists');
286
287 9
        $templateFilename = realpath($templateFilename);
288 9
        $templateName     = basename($templateFilename);
289
290
        $query = [
291 9
            'templateName' => $templateName,
292 9
        ];
293
294 9
        $body = file_get_contents($templateFilename);
295 9
        $body = base64_encode($body);
296 9
        $body = json_encode($body);
297
298
        $options = [
299 9
            RequestOptions::QUERY => $query,
300 9
            RequestOptions::BODY  => $body,
301 9
        ];
302
303 9
        $response = $this->request('POST', $this->uri('/templates/upload'), $options);
304
305 9
        if ($response instanceof Response && 201 === $response->getStatusCode()) {
306 9
            $ret = true;
307 9
        }
308
309 9
        return $ret;
310
    }
311
312
    /**
313
     * Convert a document on the local file system to a different format
314
     *
315
     * @param string $documentFilename Document filename
316
     * @param string $returnFormat     Return format
317
     *
318
     * @throws InvalidArgumentException
319
     *
320
     * @return null|resource
321
     */
322 6 View Code Duplication
    public function convertDocument($documentFilename, $returnFormat)
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...
323
    {
324 6
        $ret = null;
325
326 6
        StaticValidator::execute($documentFilename, 'DocumentExtension');
327 3
        StaticValidator::execute($documentFilename, 'FileExists');
328 2
        StaticValidator::execute($returnFormat    , 'ReturnFormat');
329
330
        $query = [
331 1
            'returnFormat' => $returnFormat,
332 1
        ];
333
334 1
        $documentFilename = realpath($documentFilename);
335
336 1
        $body = file_get_contents($documentFilename);
337 1
        $body = base64_encode($body);
338 1
        $body = json_encode($body);
339
340
        $options = [
341 1
            RequestOptions::QUERY => $query,
342 1
            RequestOptions::BODY  => $body,
343 1
        ];
344
345 1
        $response = $this->request('POST', $this->uri('/document/convert'), $options);
346
347 1
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
348 1
            $body = (string) $response->getBody();
349 1
            $ret  = base64_decode($body);
350 1
        }
351
352 1
        return $ret;
353
    }
354
355
    /**
356
     * Merge data into a template and return an array of binary data.
357
     * Each record in the array is the binary data of one document
358
     *
359
     * @param array   $mergeData        Array of merge data
360
     * @param string  $returnFormat     Return format
361
     * @param string  $templateName     Template name
362
     * @param string  $templateFilename Template filename on local file system
363
     * @param boolean $append           Append flag
364
     * @param array   $mergeSettings    Array of merge settings
365
     *
366
     * @throws InvalidArgumentException
367
     *
368
     * @return null|string
369
     */
370 15
    public function mergeDocument($mergeData, $returnFormat, $templateName = null, $templateFilename = null,
371
                                    $append = null, $mergeSettings = [])
372
    {
373 15
        $ret = null;
374
375 15
        StaticValidator::execute($mergeData   , 'TypeArray');
376 15
        StaticValidator::execute($returnFormat, 'ReturnFormat');
377
378 14
        if (null !== $templateName) {
379 2
            StaticValidator::execute($templateName, 'TemplateName');
380 1
        }
381
382 13 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...
383 12
            StaticValidator::execute($templateFilename, 'TemplateExtension');
384 9
            StaticValidator::execute($templateFilename, 'FileExists');
385 8
            $templateFilename = realpath($templateFilename);
386 8
        }
387
388 9
        if (null !== $append) {
389 6
            $append = StaticFilter::execute($append, 'BooleanToString');
390 5
        }
391
392 8
        StaticValidator::execute($mergeSettings, 'TypeArray');
393
394
        $query = [
395 7
            'returnFormat' => $returnFormat,
396 7
            'append'       => $append,
397 7
        ];
398
399 7
        if (null !== $templateName) {
400 1
            $query['templateName'] = $templateName;
401 1
        }
402
403
        $body = [
404 7
            'mergeData' => $mergeData,
405 7
        ];
406
407 7 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...
408 6
            $template = file_get_contents($templateFilename);
409 6
            $template = base64_encode($template);
410 6
            $body['template'] = $template;
411 6
        }
412
413 7
        if (count($mergeSettings) > 0) {
414 4
            $body['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings);
415 2
        }
416
417 5
        $body = json_encode($body);
418
419
        $options = [
420 5
            RequestOptions::QUERY => $query,
421 5
            RequestOptions::BODY  => $body,
422 5
        ];
423
424 5
        $response = $this->request('POST', $this->uri('/document/merge'), $options);
425
426 5
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
427 5
            $body = (string) $response->getBody();
428 5
            $body = json_decode($body, true);
429 5 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...
430 5
                $ret = array_map('base64_decode', $body);
431 5
            }
432 5
        }
433
434 5
        return $ret;
435
    }
436
437
    /**
438
     * Perform find and replace in template and return binary data.
439
     *
440
     * @param array  $findAndReplaceData Array of find and replace data
441
     * @param string $returnFormat       Return format
442
     * @param string $templateName       Template name
443
     * @param string $templateFilename   Template filename on local file system
444
     * @param array  $mergeSettings      Array of merge settings
445
     *
446
     * @throws InvalidArgumentException
447
     *
448
     * @return null|string
449
     */
450 11
    public function findAndReplace($findAndReplaceData, $returnFormat, $templateName = null, $templateFilename = null,
451
                                   $mergeSettings = [])
452
    {
453 11
        $ret = null;
454
455 11
        StaticValidator::execute($findAndReplaceData, 'TypeArray');
456 11
        StaticValidator::execute($returnFormat      , 'ReturnFormat');
457
458 10
        if (null !== $templateName) {
459 3
            StaticValidator::execute($templateName, 'TemplateName');
460 1
        }
461
462 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...
463 8
            StaticValidator::execute($templateFilename, 'TemplateExtension');
464 5
            StaticValidator::execute($templateFilename, 'FileExists');
465 4
            $templateFilename = realpath($templateFilename);
466 4
        }
467
468 5
        StaticValidator::execute($mergeSettings, 'TypeArray');
469
470
        $query = [
471 4
            'returnFormat' => $returnFormat,
472 4
        ];
473
474 4
        if (null !== $templateName) {
475 1
            $query['templateName'] = $templateName;
476 1
        }
477
478
        $body = [
479 4
            'findAndReplaceData' => $this->buildFindAndReplaceDataArray($findAndReplaceData),
480 4
        ];
481
482 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...
483 3
            $template = file_get_contents($templateFilename);
484 3
            $template = base64_encode($template);
485 3
            $body['template'] = $template;
486 3
        }
487
488 4
        if (count($mergeSettings) > 0) {
489 4
            $body['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings);
490 2
        }
491
492 2
        $body = json_encode($body);
493
494
        $options = [
495 2
            RequestOptions::QUERY => $query,
496 2
            RequestOptions::BODY  => $body,
497 2
        ];
498
499 2
        $response = $this->request('POST', $this->uri('/document/findandreplace'), $options);
500
501 2
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
502 2
            $body = (string) $response->getBody();
503 2
            $ret  = base64_decode($body);
504 2
        }
505
506 2
        return $ret;
507
    }
508
509
510
    /**
511
     * DELETE methods
512
     * =================================================================================================================
513
     */
514
515
    /**
516
     * Delete a template in template storage
517
     *
518
     * @param string $templateName Template name
519
     *
520
     * @throws InvalidArgumentException
521
     *
522
     * @return bool
523
     */
524 11
    public function deleteTemplate($templateName)
525
    {
526 11
        $ret = false;
527
528 11
        StaticValidator::execute($templateName, 'TemplateName');
529
530
        $query = [
531 10
            'templateName' => $templateName,
532 10
        ];
533
534
        $options = [
535 10
            RequestOptions::QUERY => $query,
536 10
        ];
537
538 10
        $response = $this->request('DELETE', $this->uri('/templates/delete'), $options);
539
540 9
        if ($response instanceof Response && 204 === $response->getStatusCode()) {
541 9
            $ret = true;
542 9
        }
543
544 9
        return $ret;
545
    }
546
}