Passed
Branch development (83e590)
by Jonathan
04:53
created

GetTrait::getApiKeys()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

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