Test Failed
Branch development (5babc9)
by Jonathan
04:47
created

GetTrait::getAvailableDictionaries()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
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 © 2018 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\AbstractPropertyMap as PropertyMap;
21
use TxTextControl\ReportingCloud\PropertyMap\AccountSettings as AccountSettingsPropertyMap;
22
use TxTextControl\ReportingCloud\PropertyMap\ApiKey as ApiKeyPropertyMap;
23
use TxTextControl\ReportingCloud\PropertyMap\IncorrectWord as IncorrectWordMap;
24
use TxTextControl\ReportingCloud\PropertyMap\TemplateInfo as TemplateInfoPropertyMap;
25
use TxTextControl\ReportingCloud\PropertyMap\TemplateList as TemplateListPropertyMap;
26
use TxTextControl\ReportingCloud\Validator\StaticValidator;
27
28
trait GetTrait
29
{
30
    abstract protected function uri($uri);
31
32
    abstract protected function request($method, $uri, $options);
33
34
    abstract protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap);
35
36
    /**
37
     * Return an associative array of API keys associated with the Reporting Cloud account
38
     *
39
     * @return array|null
40
     */
41
    public function getApiKeys()
42
    {
43
        $ret = null;
44
45
        $propertyMap = new ApiKeyPropertyMap();
46
47 1
        $records = $this->get('/account/apikeys');
48
49 1
        if (is_array($records) && count($records) > 0) {
50
            $ret = [];
51 1
            foreach ($records as $record) {
52 1
                if (isset($record['key'])) {
53
                    StaticValidator::execute($record['key'], 'ApiKey');
54 1
                }
55
                if (isset($record['active'])) {
56
                    StaticValidator::execute($record['active'], 'TypeBoolean');
57 1
                }
58 1
                $ret[] = $this->buildPropertyMapArray($record, $propertyMap);
59 1
            }
60
        }
61 1
62
        return $ret;
63 1
    }
64 1
65 1
    /**
66
     * Check a corpus of text for spelling errors.
67 1
     *
68
     * Return an array of misspelled words, if spelling errors are found in the corpus of text.
69
     *
70
     * Return null, if no misspelled words are found in the corpus of text.
71
     *
72
     * @param string $text     Corpus of text that should be spell checked
73
     * @param string $language Language of specified text
74
     *
75 1
     * @return array|null
76
     */
77 1
    public function proofingCheck($text, $language)
78
    {
79 1
        $ret = null;
80
81 1
        StaticValidator::execute($text, 'TypeString');
82 1
        StaticValidator::execute($language, 'Language');
83 1
84
        $propertyMap = new IncorrectWordMap();
85 1
86
        $query = [
87
            'text'     => $text,
88
            'language' => $language,
89
        ];
90
91
        $records = $this->get('/proofing/check', $query);
92
93 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...
94
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
95
        }
96
97 1
        return $ret;
98
    }
99 1
100
    /**
101 1
     * Return an array of available dictionaries on the Reporting Cloud service
102 1
     *
103 1
     * @return array|null
104
     */
105 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...
106 1
    {
107 1
        $ret = null;
108 1
109 1
        $dictionaries = $this->get('/proofing/availabledictionaries');
110
111 1
        if (is_array($dictionaries) && count($dictionaries) > 0) {
112
            $ret = array_map('trim', $dictionaries);
113 1
        }
114 1
115 1
        return $ret;
116
    }
117 1
118
    /**
119
     * Return an array of suggestions for a misspelled word.
120
     *
121
     * @param string $word     Word that should be spell checked
122
     * @param string $language Language of specified text
123
     * @param int    $max      Maximum number of suggestions to return
124
     *
125
     * @return array|null
126
     */
127
    public function getProofingSuggestions($word, $language, $max = 10)
128
    {
129 1
        $ret = null;
130
131 1
        StaticValidator::execute($word, 'TypeString');
132
        StaticValidator::execute($language, 'Language');
133 1
        StaticValidator::execute($max, 'TypeInteger');
134
135 1
        $query = [
136
            'word'     => $word,
137
            'language' => $language,
138 1
            'max'      => $max,
139 1
        ];
140
141 1
        $records = $this->get('/proofing/suggestions', $query);
142
143 1
        if (is_array($records) && count($records) > 0) {
144 1
            $ret = array_map('trim', $records);
145 1
        }
146
147 1
        return $ret;
148
    }
149
150
    /**
151
     * Return an array of merge blocks and merge fields in a template file in template storage.
152
     *
153
     * @param string $templateName Template name
154
     *
155
     * @throws InvalidArgumentException
156
     *
157
     * @return array|null
158
     */
159
    public function getTemplateInfo($templateName)
160
    {
161
        $ret = null;
162
163
        $propertyMap = new TemplateInfoPropertyMap();
164 6
165
        StaticValidator::execute($templateName, 'TemplateName');
166 6
167
        $query = [
168 6
            'templateName' => $templateName,
169 5
        ];
170 4
171 3
        $records = $this->get('/templates/info', $query);
172 2
173 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...
174
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
175 1
        }
176 1
177 1
        return $ret;
178 1
    }
179 1
180 1
    /**
181
     * Return an array of binary data.
182 1
     * Each record in the array is the binary data of a thumbnail image
183
     *
184 1
     * @param string $templateName Template name
185 1
     * @param int    $zoomFactor   Zoom factor
186 1
     * @param int    $fromPage     From page
187
     * @param int    $toPage       To page
188 1
     * @param string $imageFormat  Image format
189
     *
190
     * @throws InvalidArgumentException
191
     *
192
     * @return array|null
193
     */
194
    public function getTemplateThumbnails($templateName, $zoomFactor, $fromPage, $toPage, $imageFormat)
195
    {
196 1
        $ret = null;
197
198 1
        StaticValidator::execute($templateName, 'TemplateName');
199
        StaticValidator::execute($zoomFactor, 'ZoomFactor');
200
        StaticValidator::execute($fromPage, 'Page');
201
        StaticValidator::execute($toPage, 'Page');
202
        StaticValidator::execute($imageFormat, 'ImageFormat');
203
204
        $query = [
205
            'templateName' => $templateName,
206 1
            'zoomFactor'   => $zoomFactor,
207
            'fromPage'     => $fromPage,
208 1
            'toPage'       => $toPage,
209
            'imageFormat'  => $imageFormat,
210 1
        ];
211
212 1
        $records = $this->get('/templates/thumbnails', $query);
213
214 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...
215 1
            $ret = array_map('base64_decode', $records);
216 1
        }
217 1
218 1
        return $ret;
219 1
    }
220 1
221 1
    /**
222 1
     * Return the number of templates in template storage
223
     *
224 1
     * @return int
225
     */
226
    public function getTemplateCount()
227
    {
228
        return (int) $this->get('/templates/count');
229
    }
230
231
    /**
232
     * Return an array properties for the templates in template storage
233
     *
234
     * @return array|null
235
     */
236 2 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...
237
    {
238 2
        $ret = null;
239
240
        $propertyMap = new TemplateListPropertyMap();
241 1
242 1
        $records = $this->get('/templates/list');
243
244 1
        if (is_array($records) && count($records) > 0) {
245
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
246
            array_walk($ret, function (&$record) {
247
                $key = 'modified';
248
                if (isset($record[$key])) {
249
                    $record[$key] = StaticFilter::execute($record[$key], 'DateTimeToTimestamp');
250
                }
251
            });
252
        }
253
254
        return $ret;
255
    }
256 2
257
    /**
258 2
     * Return the number of pages in a template in template storage
259
     *
260
     * @param string $templateName Template name
261 1
     *
262 1
     * @throws InvalidArgumentException
263
     *
264 1
     * @return int
265
     */
266 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...
267
    {
268
        StaticValidator::execute($templateName, 'TemplateName');
269
270
        $query = [
271
            'templateName' => $templateName,
272 1
        ];
273
274 1
        return (int) $this->get('/templates/pagecount', $query);
275
    }
276 1
277
    /**
278 1
     * Return true, if the template exists in template storage
279 1
     *
280 1
     * @param string $templateName Template name
281
     *
282 1
     * @throws InvalidArgumentException
283
     *
284
     * @return bool
285
     */
286 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...
287
    {
288
        StaticValidator::execute($templateName, 'TemplateName');
289
290
        $query = [
291 1
            'templateName' => $templateName,
292
        ];
293 1
294
        return (bool) $this->get('/templates/exists', $query);
295 1
    }
296
297 1
    /**
298
     * Return an array of available fonts on the Reporting Cloud service
299 1
     *
300 1
     * @return array|null
301 1
     */
302 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...
303 1
    {
304 1
        $ret = null;
305 1
306
        $fonts = $this->get('/fonts/list');
307 1
308
        if (is_array($fonts) && count($fonts) > 0) {
309
            $ret = array_map('trim', $fonts);
310
        }
311
312
        return $ret;
313
    }
314
315
    /**
316
     * Return an array properties for the ReportingCloud account
317
     *
318
     * @return null
319 2
     * @throws \Zend\Filter\Exception\ExceptionInterface
320
     */
321 2 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...
322
    {
323 2
        $ret = null;
324
325
        $propertyMap = new AccountSettingsPropertyMap();
326 1
327 1
        $records = $this->get('/account/settings');
328
329 1
        if (is_array($records) && count($records) > 0) {
330
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
331 1
            $key = 'valid_until';
332 1
            if ($ret[$key]) {
333 1
                $ret[$key] = StaticFilter::execute($ret[$key], 'DateTimeToTimestamp');
334
            }
335 1
        }
336
337
        return $ret;
338
    }
339
340
    /**
341
     * Download the binary data of a template from template storage
342
     *
343
     * @param string $templateName Template name
344
     *
345
     * @throws InvalidArgumentException
346 12
     *
347
     * @return null|resource
348 12
     */
349
    public function downloadTemplate($templateName)
350
    {
351 12
        $ret = null;
352 12
353
        StaticValidator::execute($templateName, 'TemplateName');
354 12
355
        $query = [
356 12
            'templateName' => $templateName,
357 12
        ];
358 12
359
        $data = $this->get('/templates/download', $query);
360 12
361
        if (null !== $data) {
362
            $ret = base64_decode($data);
363
        }
364
365
        return $ret;
366
    }
367
368
    /**
369
     * Execute a GET request via REST client
370
     *
371
     * @param string $uri   URI
372
     * @param array  $query Query
373
     *
374
     * @return mixed|null
375
     */
376
    protected function get($uri, $query = [])
377
    {
378
        $ret = null;
379
380
        $options = [
381
            RequestOptions::QUERY => $query,
382
        ];
383
384
        $response = $this->request('GET', $this->uri($uri), $options);
385
386
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
387
            $ret = json_decode($response->getBody(), true);
388
        }
389
390
        return $ret;
391
    }
392
}
393