Passed
Branch append-document-1.0 (2fd487)
by Jonathan
17:18
created

PostTrait   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 350
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 30
eloc 113
dl 0
loc 350
ccs 138
cts 138
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A uploadTemplate() 0 12 1
A uploadTemplateFromBase64() 0 21 3
C mergeDocument() 0 67 11
A convertDocument() 0 27 3
B findAndReplaceDocument() 0 58 8
A appendDocument() 0 31 4
1
<?php
2
3
/**
4
 * ReportingCloud PHP Wrapper
5
 *
6
 * PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH.
7
 *
8
 * @link      http://www.reporting.cloud to learn more about ReportingCloud
9
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
10
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
11
 * @copyright © 2018 Text Control GmbH
12
 */
13
14
namespace TxTextControl\ReportingCloud;
15
16
use GuzzleHttp\Psr7\Response;
17
use GuzzleHttp\RequestOptions;
18
use TxTextControl\ReportingCloud\Filter\StaticFilter;
19
use TxTextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap;
20
use TxTextControl\ReportingCloud\Validator\StaticValidator;
21
22
/**
23
 * Trait PostTrait
24
 *
25
 * @package TxTextControl\ReportingCloud
26
 * @author  Jonathan Maron (@JonathanMaron)
27
 */
28
trait PostTrait
29
{
30
    /**
31
     * Abstract Methods
32
     * -----------------------------------------------------------------------------------------------------------------
33
     */
34
35
    /**
36
     * Construct URI with version number
37
     *
38
     * @param string $uri URI
39
     *
40
     * @return string
41
     */
42
    abstract protected function uri($uri);
43
44
    /**
45
     * Request the URI with options
46
     *
47
     * @param string $method  HTTP method
48
     * @param string $uri     URI
49
     * @param array  $options Options
50
     *
51
     * @return mixed|null|\Psr\Http\Message\ResponseInterface
52
     *
53
     * @throws RuntimeException
54
     */
55
    abstract protected function request($method, $uri, $options);
56
57
    /**
58
     * Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays)
59
     *
60
     * @param array $array FindAndReplaceData array
61
     *
62
     * @return array
63
     */
64
    abstract protected function buildFindAndReplaceDataArray(array $array);
65
66
    /**
67
     * Using passed mergeSettings array, build array for backend
68
     *
69
     * @param array $array MergeSettings array
70
     *
71
     * @return array
72
     */
73
    abstract protected function buildMergeSettingsArray(array $array);
74
75
    /**
76
     * Using the passed propertyMap, recursively build array
77
     *
78
     * @param array       $array       Array
79
     * @param PropertyMap $propertyMap PropertyMap
80
     *
81
     * @return array
82
     */
83
    abstract protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap);
84
85
    /**
86
     * POST Methods
87
     * -----------------------------------------------------------------------------------------------------------------
88
     */
89
90
    /**
91
     * Upload a base64 encoded template to template storage
92
     *
93
     * @param string $data         Template encoded as base64
94
     * @param string $templateName Template name
95
     *
96
     * @return bool
97
     * @throws RuntimeException
98
     */
99 18
    public function uploadTemplateFromBase64($data, $templateName)
100
    {
101 18
        $ret = false;
102
103 18
        StaticValidator::execute($data, 'Base64Data');
104 18
        StaticValidator::execute($templateName, 'TemplateExtension');
105
106
        $options = [
107 18
            RequestOptions::QUERY => [
108 18
                'templateName' => $templateName,
109 9
            ],
110 18
            RequestOptions::JSON  => $data,
111 9
        ];
112
113 18
        $response = $this->request('POST', $this->uri('/templates/upload'), $options);
114
115 18
        if ($response instanceof Response && 201 === $response->getStatusCode()) {
116 18
            $ret = true;
117 9
        }
118
119 18
        return $ret;
120
    }
121
122
    /**
123
     * Upload a template to template storage
124
     *
125
     * @param string $templateFilename Template name
126
     *
127
     * @return bool
128
     * @throws RuntimeException
129
     */
130 26
    public function uploadTemplate($templateFilename)
131
    {
132 26
        StaticValidator::execute($templateFilename, 'TemplateExtension');
133 20
        StaticValidator::execute($templateFilename, 'FileExists');
134
135 18
        $templateFilename = realpath($templateFilename);
136 18
        $templateName     = basename($templateFilename);
137
138 18
        $data = file_get_contents($templateFilename);
139 18
        $data = base64_encode($data);
140
141 18
        return $this->uploadTemplateFromBase64($data, $templateName);
142
    }
143
144
    /**
145
     * Convert a document on the local file system to a different format
146
     *
147
     * @param string $documentFilename Document filename
148
     * @param string $returnFormat     Return format
149
     *
150
     * @return bool|null|string
151
     * @throws RuntimeException
152
     */
153 12
    public function convertDocument($documentFilename, $returnFormat)
154
    {
155 12
        $ret = null;
156
157 12
        StaticValidator::execute($documentFilename, 'DocumentExtension');
158 6
        StaticValidator::execute($documentFilename, 'FileExists');
159 4
        StaticValidator::execute($returnFormat, 'ReturnFormat');
160
161 2
        $documentFilename = realpath($documentFilename);
162
163 2
        $json = file_get_contents($documentFilename);
164 2
        $json = base64_encode($json);
165
166
        $options = [
167 2
            RequestOptions::QUERY => [
168 2
                'returnFormat' => $returnFormat,
169 1
            ],
170 2
            RequestOptions::JSON  => $json,
171 1
        ];
172
173 2
        $response = $this->request('POST', $this->uri('/document/convert'), $options);
174
175 2
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
176 2
            $ret = base64_decode($response->getBody());
177 1
        }
178
179 2
        return $ret;
180
    }
181
182
    /**
183
     * Merge data into a template and return an array of binary data.
184
     * Each record in the array is the binary data of one document
185
     *
186
     * @param array  $mergeData        Array of merge data
187
     * @param string $returnFormat     Return format
188
     * @param string $templateName     Template name
189
     * @param string $templateFilename Template filename on local file system
190
     * @param bool   $append           Append flag
191
     * @param array  $mergeSettings    Array of merge settings
192
     *
193
     * @return array|null
194
     * @throws RuntimeException
195
     * @throws \Zend\Filter\Exception\ExceptionInterface
196
     */
197 28
    public function mergeDocument(
198
        $mergeData,
199
        $returnFormat,
200
        $templateName = null,
201
        $templateFilename = null,
202
        $append = null,
203
        $mergeSettings = []
204
    ) {
205 28
        $ret = null;
206
207 28
        StaticValidator::execute($mergeData, 'TypeArray');
208 28
        StaticValidator::execute($returnFormat, 'ReturnFormat');
209
210 26
        if (null !== $templateName) {
211 4
            StaticValidator::execute($templateName, 'TemplateName');
212 1
        }
213
214 24
        if (null !== $templateFilename) {
215 22
            StaticValidator::execute($templateFilename, 'TemplateExtension');
216 16
            StaticValidator::execute($templateFilename, 'FileExists');
217 14
            $templateFilename = realpath($templateFilename);
218 7
        }
219
220 16
        if (null !== $append) {
221 14
            $append = StaticFilter::execute($append, 'BooleanToString');
222 6
        }
223
224 14
        StaticValidator::execute($mergeSettings, 'TypeArray');
225
226
        $query = [
227 12
            'returnFormat' => $returnFormat,
228 12
            'append'       => $append,
229 6
        ];
230
231 12
        if (null !== $templateName) {
232 2
            $query['templateName'] = $templateName;
233 1
        }
234
235
        $json = [
236 12
            'mergeData' => $mergeData,
237 6
        ];
238
239 12
        if (null !== $templateFilename) {
240 10
            $template         = file_get_contents($templateFilename);
241 10
            $template         = base64_encode($template);
242 10
            $json['template'] = $template;
243 5
        }
244
245 12
        if (count($mergeSettings) > 0) {
246 10
            $json['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings);
247 2
        }
248
249
        $options = [
250 6
            RequestOptions::QUERY => $query,
251 6
            RequestOptions::JSON  => $json,
252 3
        ];
253
254 6
        $response = $this->request('POST', $this->uri('/document/merge'), $options);
255
256 6
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
257 6
            $body = json_decode($response->getBody(), true);
258 6
            if (is_array($body) && count($body) > 0) {
259 6
                $ret = array_map('base64_decode', $body);
260 3
            }
261 3
        }
262
263 6
        return $ret;
264
    }
265
266
    /**
267
     * Combine documents to appending them, divided by a new section, paragraph or nothing
268
     *
269
     * @param array  $documentsData
270
     * @param string $returnFormat
271
     * @param array  $documentSettings
272
     *
273
     * @return bool|null|string
274
     */
275 2
    public function appendDocument($documentsData, $returnFormat, $documentSettings = [])
276
    {
277 2
        $ret = null;
278
279 2
        StaticValidator::execute($documentsData, 'TypeArray');
280 2
        StaticValidator::execute($returnFormat, 'ReturnFormat');
281
282
        $query = [
283 2
            'returnFormat' => $returnFormat,
284 1
        ];
285
286
        $json = [
287 2
            'documents' => $this->buildDocumentsArray($documentsData),
0 ignored issues
show
Bug introduced by
It seems like buildDocumentsArray() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

287
            'documents' => $this->/** @scrutinizer ignore-call */ buildDocumentsArray($documentsData),
Loading history...
288 1
        ];
289
290 2
        if (count($documentSettings) > 0) {
291 2
            $json['documentSettings'] = $this->buildDocumentSettingsArray($documentSettings);
0 ignored issues
show
Bug introduced by
It seems like buildDocumentSettingsArray() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

291
            /** @scrutinizer ignore-call */ 
292
            $json['documentSettings'] = $this->buildDocumentSettingsArray($documentSettings);
Loading history...
292 1
        }
293
294
        $options = [
295 2
            RequestOptions::QUERY => $query,
296 2
            RequestOptions::JSON  => $json,
297 1
        ];
298
299 2
        $response = $this->request('POST', $this->uri('/document/append'), $options);
300
301 2
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
302 2
            $ret = base64_decode($response->getBody());
303 1
        }
304
305 2
        return $ret;
306
    }
307
308
    /**
309
     * Perform find and replace in document and return binary data.
310
     *
311
     * @param array  $findAndReplaceData Array of find and replace data
312
     * @param string $returnFormat       Return format
313
     * @param string $templateName       Template name
314
     * @param string $templateFilename   Template filename on local file system
315
     * @param array  $mergeSettings      Array of merge settings
316
     *
317
     * @return bool|null|string
318
     * @throws RuntimeException
319
     */
320 22
    public function findAndReplaceDocument(
321
        $findAndReplaceData,
322
        $returnFormat,
323
        $templateName = null,
324
        $templateFilename = null,
325
        $mergeSettings = []
326
    ) {
327 22
        $ret = null;
328
329 22
        StaticValidator::execute($findAndReplaceData, 'TypeArray');
330 22
        StaticValidator::execute($returnFormat, 'ReturnFormat');
331
332 20
        if (null !== $templateName) {
333 4
            StaticValidator::execute($templateName, 'TemplateName');
334 1
        }
335
336 18
        if (null !== $templateFilename) {
337 16
            StaticValidator::execute($templateFilename, 'TemplateExtension');
338 10
            StaticValidator::execute($templateFilename, 'FileExists');
339 8
            $templateFilename = realpath($templateFilename);
340 4
        }
341
342 10
        StaticValidator::execute($mergeSettings, 'TypeArray');
343
344
        $query = [
345 8
            'returnFormat' => $returnFormat,
346 4
        ];
347
348 8
        if (null !== $templateName) {
349 2
            $query['templateName'] = $templateName;
350 1
        }
351
352
        $json = [
353 8
            'findAndReplaceData' => $this->buildFindAndReplaceDataArray($findAndReplaceData),
354 4
        ];
355
356 8
        if (null !== $templateFilename) {
357 6
            $template         = file_get_contents($templateFilename);
358 6
            $template         = base64_encode($template);
359 6
            $json['template'] = $template;
360 3
        }
361
362 8
        if (count($mergeSettings) > 0) {
363 8
            $json['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings);
364 2
        }
365
366
        $options = [
367 4
            RequestOptions::QUERY => $query,
368 4
            RequestOptions::JSON  => $json,
369 2
        ];
370
371 4
        $response = $this->request('POST', $this->uri('/document/findandreplace'), $options);
372
373 4
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
374 4
            $ret = base64_decode($response->getBody());
375 2
        }
376
377 4
        return $ret;
378
    }
379
}
380