Completed
Push — master ( 26f10b...2de37e )
by Jonathan
07:00
created

ReportingCloud::deleteTemplate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
ccs 12
cts 12
cp 1
rs 9.2
cc 3
eloc 11
nc 2
nop 1
crap 3
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
        $json = file_get_contents($templateFilename);
295 9
        $json = base64_encode($json);
296
297
        $options = [
298 9
            RequestOptions::QUERY => $query,
299 9
            RequestOptions::JSON  => $json,
300 9
        ];
301
302 9
        $response = $this->request('POST', $this->uri('/templates/upload'), $options);
303
304 9
        if ($response instanceof Response && 201 === $response->getStatusCode()) {
305 9
            $ret = true;
306 9
        }
307
308 9
        return $ret;
309
    }
310
311
    /**
312
     * Convert a document on the local file system to a different format
313
     *
314
     * @param string $documentFilename Document filename
315
     * @param string $returnFormat     Return format
316
     *
317
     * @throws InvalidArgumentException
318
     *
319
     * @return null|resource
320
     */
321 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...
322
    {
323 6
        $ret = null;
324
325 6
        StaticValidator::execute($documentFilename, 'DocumentExtension');
326 3
        StaticValidator::execute($documentFilename, 'FileExists');
327 2
        StaticValidator::execute($returnFormat    , 'ReturnFormat');
328
329
        $query = [
330 1
            'returnFormat' => $returnFormat,
331 1
        ];
332
333 1
        $documentFilename = realpath($documentFilename);
334
335 1
        $json = file_get_contents($documentFilename);
336 1
        $json = base64_encode($json);
337
338
        $options = [
339 1
            RequestOptions::QUERY => $query,
340 1
            RequestOptions::JSON  => $json,
341 1
        ];
342
343 1
        $response = $this->request('POST', $this->uri('/document/convert'), $options);
344
345 1
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
346 1
            $body = (string) $response->getBody();
347 1
            $ret  = base64_decode($body);
348 1
        }
349
350 1
        return $ret;
351
    }
352
353
    /**
354
     * Merge data into a template and return an array of binary data.
355
     * Each record in the array is the binary data of one document
356
     *
357
     * @param array   $mergeData        Array of merge data
358
     * @param string  $returnFormat     Return format
359
     * @param string  $templateName     Template name
360
     * @param string  $templateFilename Template filename on local file system
361
     * @param boolean $append           Append flag
362
     * @param array   $mergeSettings    Array of merge settings
363
     *
364
     * @throws InvalidArgumentException
365
     *
366
     * @return null|string
367
     */
368 15
    public function mergeDocument($mergeData, $returnFormat, $templateName = null, $templateFilename = null,
369
                                    $append = null, $mergeSettings = [])
370
    {
371 15
        $ret = null;
372
373 15
        StaticValidator::execute($mergeData   , 'TypeArray');
374 15
        StaticValidator::execute($returnFormat, 'ReturnFormat');
375
376 14
        if (null !== $templateName) {
377 2
            StaticValidator::execute($templateName, 'TemplateName');
378 1
        }
379
380 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...
381 12
            StaticValidator::execute($templateFilename, 'TemplateExtension');
382 9
            StaticValidator::execute($templateFilename, 'FileExists');
383 8
            $templateFilename = realpath($templateFilename);
384 8
        }
385
386 9
        if (null !== $append) {
387 6
            $append = StaticFilter::execute($append, 'BooleanToString');
388 5
        }
389
390 8
        StaticValidator::execute($mergeSettings, 'TypeArray');
391
392
        $query = [
393 7
            'returnFormat' => $returnFormat,
394 7
            'append'       => $append,
395 7
        ];
396
397 7
        if (null !== $templateName) {
398 1
            $query['templateName'] = $templateName;
399 1
        }
400
401
        $json = [
402 7
            'mergeData' => $mergeData,
403 7
        ];
404
405 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...
406 6
            $template = file_get_contents($templateFilename);
407 6
            $template = base64_encode($template);
408 6
            $json['template'] = $template;
409 6
        }
410
411 7
        if (count($mergeSettings) > 0) {
412 4
            $json['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings);
413 2
        }
414
415
        $options = [
416 5
            RequestOptions::QUERY => $query,
417 5
            RequestOptions::JSON  => $json,
418 5
        ];
419
420 5
        $response = $this->request('POST', $this->uri('/document/merge'), $options);
421
422 5
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
423 5
            $body = (string) $response->getBody();
424 5
            $body = json_decode($body, true);
425 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...
426 5
                $ret = array_map('base64_decode', $body);
427 5
            }
428 5
        }
429
430 5
        return $ret;
431
    }
432
433
    /**
434
     * Perform find and replace in template and return binary data.
435
     *
436
     * @param array  $findAndReplaceData Array of find and replace data
437
     * @param string $returnFormat       Return format
438
     * @param string $templateName       Template name
439
     * @param string $templateFilename   Template filename on local file system
440
     * @param array  $mergeSettings      Array of merge settings
441
     *
442
     * @throws InvalidArgumentException
443
     *
444
     * @return null|string
445
     */
446 11
    public function findAndReplace($findAndReplaceData, $returnFormat, $templateName = null, $templateFilename = null,
447
                                   $mergeSettings = [])
448
    {
449 11
        $ret = null;
450
451 11
        StaticValidator::execute($findAndReplaceData, 'TypeArray');
452 11
        StaticValidator::execute($returnFormat      , 'ReturnFormat');
453
454 10
        if (null !== $templateName) {
455 2
            StaticValidator::execute($templateName, 'TemplateName');
456 2
        }
457
458 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...
459 8
            StaticValidator::execute($templateFilename, 'TemplateExtension');
460 5
            StaticValidator::execute($templateFilename, 'FileExists');
461 4
            $templateFilename = realpath($templateFilename);
462 4
        }
463
464 5
        StaticValidator::execute($mergeSettings, 'TypeArray');
465
466
        $query = [
467 4
            'returnFormat' => $returnFormat,
468 4
        ];
469
470 4
        if (null !== $templateName) {
471 1
            $query['templateName'] = $templateName;
472 1
        }
473
474
        $json = [
475 4
            'findAndReplaceData' => $this->buildFindAndReplaceDataArray($findAndReplaceData),
476 4
        ];
477
478 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...
479 3
            $template = file_get_contents($templateFilename);
480 3
            $template = base64_encode($template);
481 3
            $json['template'] = $template;
482 3
        }
483
484 4
        if (count($mergeSettings) > 0) {
485 4
            $json['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings);
486 2
        }
487
488
        $options = [
489 2
            RequestOptions::QUERY => $query,
490 2
            RequestOptions::JSON  => $json,
491 2
        ];
492
493 2
        $response = $this->request('POST', $this->uri('/document/findandreplace'), $options);
494
495 2
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
496 2
            $body = (string) $response->getBody();
497 2
            $ret  = base64_decode($body);
498 2
        }
499
500 2
        return $ret;
501
    }
502
503
504
    /**
505
     * DELETE methods
506
     * =================================================================================================================
507
     */
508
509
    /**
510
     * Delete a template in template storage
511
     *
512
     * @param string $templateName Template name
513
     *
514
     * @throws InvalidArgumentException
515
     *
516
     * @return bool
517
     */
518 11
    public function deleteTemplate($templateName)
519
    {
520 11
        $ret = false;
521
522 11
        StaticValidator::execute($templateName, 'TemplateName');
523
524
        $query = [
525 10
            'templateName' => $templateName,
526 10
        ];
527
528
        $options = [
529 10
            RequestOptions::QUERY => $query,
530 10
        ];
531
532 10
        $response = $this->request('DELETE', $this->uri('/templates/delete'), $options);
533
534 9
        if ($response instanceof Response && 204 === $response->getStatusCode()) {
535 9
            $ret = true;
536 9
        }
537
538 9
        return $ret;
539
    }
540
}