Passed
Branch development (0505ff)
by Jonathan
08:42
created

GetTrait   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 330
Duplicated Lines 27.58 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 34
c 0
b 0
f 0
lcom 1
cbo 7
dl 91
loc 330
rs 9.2
ccs 127
cts 127
cp 1

13 Methods

Rating   Name   Duplication   Size   Complexity  
A proofingCheck() 3 22 3
A get() 0 16 3
A getAvailableDictionaries() 12 12 3
A getProofingSuggestions() 0 22 3
A getTemplateInfo() 3 20 3
B getTemplateThumbnails() 3 26 3
A getTemplateCount() 0 4 1
A getTemplateList() 20 20 4
A getTemplatePageCount() 10 10 1
A templateExists() 10 10 1
A getFontList() 12 12 3
A getAccountSettings() 18 18 4
A downloadTemplate() 0 18 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\AccountSettings as AccountSettingsPropertyMap;
21
use TxTextControl\ReportingCloud\PropertyMap\IncorrectWord as IncorrectWordMap;
22
use TxTextControl\ReportingCloud\PropertyMap\TemplateInfo as TemplateInfoPropertyMap;
23
use TxTextControl\ReportingCloud\PropertyMap\TemplateList as TemplateListPropertyMap;
24
use TxTextControl\ReportingCloud\Validator\StaticValidator;
25
26
trait GetTrait
27
{
28
    /**
29
     * Check a corpus of text for spelling errors.
30
     *
31
     * Return an array of misspelled words, if spelling errors are found in the corpus of text.
32
     *
33
     * Return null, if no misspelled words are found in the corpus of text.
34
     *
35
     * @param string $text     Corpus of text that should be spell checked
36
     * @param string $language Language of specified text
37
     *
38
     * @return array|null
39
     */
40 1
    public function proofingCheck($text, $language)
41
    {
42 1
        $ret = null;
43
44 1
        StaticValidator::execute($text, 'TypeString');
45 1
        StaticValidator::execute($language, 'Language');
46
47 1
        $propertyMap = new IncorrectWordMap();
48
49
        $query = [
50 1
            'text'     => $text,
51 1
            'language' => $language,
52 1
        ];
53
54 1
        $records = $this->get('/proofing/check', $query);
55
56 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...
57 1
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
0 ignored issues
show
Bug introduced by
It seems like buildPropertyMapArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
58 1
        }
59
60 1
        return $ret;
61
    }
62
63
    /**
64
     * Execute a GET request via REST client
65
     *
66
     * @param string $uri   URI
67
     * @param array  $query Query
68
     *
69
     * @return mixed|null
70
     */
71 12
    protected function get($uri, $query = [])
72
    {
73 12
        $ret = null;
74
75
        $options = [
76 12
            RequestOptions::QUERY => $query,
77 12
        ];
78
79 12
        $response = $this->request('GET', $this->uri($uri), $options);
0 ignored issues
show
Bug introduced by
It seems like uri() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like request() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
80
81 12
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
82 12
            $ret = json_decode($response->getBody(), true);
83 12
        }
84
85 12
        return $ret;
86
    }
87
88
    /**
89
     * Return an array of available dictionaries on the Reporting Cloud service
90
     *
91
     * @return array|null
92
     */
93 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...
94
    {
95 1
        $ret = null;
96
97 1
        $dictionaries = $this->get('/proofing/availabledictionaries');
98
99 1
        if (is_array($dictionaries) && count($dictionaries) > 0) {
100 1
            $ret = array_map('trim', $dictionaries);
101 1
        }
102
103 1
        return $ret;
104
    }
105
106
    /**
107
     * Return an array of suggestions for a misspelled word.
108
     *
109
     * @param string $word     Word that should be spell checked
110
     * @param string $language Language of specified text
111
     * @param int    $max      Maximum number of suggestions to return
112
     *
113
     * @return array|null
114
     */
115 1
    public function getProofingSuggestions($word, $language, $max = 10)
116
    {
117 1
        $ret = null;
118
119 1
        StaticValidator::execute($word, 'TypeString');
120 1
        StaticValidator::execute($language, 'Language');
121 1
        StaticValidator::execute($max, 'TypeInteger');
122
123
        $query = [
124 1
            'word'     => $word,
125 1
            'language' => $language,
126 1
            'max'      => $max,
127 1
        ];
128
129 1
        $records = $this->get('/proofing/suggestions', $query);
130
131 1
        if (is_array($records) && count($records) > 0) {
132 1
            $ret = array_map('trim', $records);
133 1
        }
134
135 1
        return $ret;
136
    }
137
138
    /**
139
     * Return an array of merge blocks and merge fields in a template file in template storage.
140
     *
141
     * @param string $templateName Template name
142
     *
143
     * @throws InvalidArgumentException
144
     *
145
     * @return array|null
146
     */
147 1
    public function getTemplateInfo($templateName)
148
    {
149 1
        $ret = null;
150
151 1
        $propertyMap = new TemplateInfoPropertyMap();
152
153 1
        StaticValidator::execute($templateName, 'TemplateName');
154
155
        $query = [
156 1
            'templateName' => $templateName,
157 1
        ];
158
159 1
        $records = $this->get('/templates/info', $query);
160
161 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...
162 1
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
0 ignored issues
show
Bug introduced by
It seems like buildPropertyMapArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
163 1
        }
164
165 1
        return $ret;
166
    }
167
168
    /**
169
     * Return an array of binary data.
170
     * Each record in the array is the binary data of a thumbnail image
171
     *
172
     * @param string $templateName Template name
173
     * @param int    $zoomFactor   Zoom factor
174
     * @param int    $fromPage     From page
175
     * @param int    $toPage       To page
176
     * @param string $imageFormat  Image format
177
     *
178
     * @throws InvalidArgumentException
179
     *
180
     * @return array|null
181
     */
182 6
    public function getTemplateThumbnails($templateName, $zoomFactor, $fromPage, $toPage, $imageFormat)
183
    {
184 6
        $ret = null;
185
186 6
        StaticValidator::execute($templateName, 'TemplateName');
187 5
        StaticValidator::execute($zoomFactor, 'ZoomFactor');
188 4
        StaticValidator::execute($fromPage, 'Page');
189 3
        StaticValidator::execute($toPage, 'Page');
190 2
        StaticValidator::execute($imageFormat, 'ImageFormat');
191
192
        $query = [
193 1
            'templateName' => $templateName,
194 1
            'zoomFactor'   => $zoomFactor,
195 1
            'fromPage'     => $fromPage,
196 1
            'toPage'       => $toPage,
197 1
            'imageFormat'  => $imageFormat,
198 1
        ];
199
200 1
        $records = $this->get('/templates/thumbnails', $query);
201
202 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...
203 1
            $ret = array_map('base64_decode', $records);
204 1
        }
205
206 1
        return $ret;
207
    }
208
209
    /**
210
     * Return the number of templates in template storage
211
     *
212
     * @return int
213
     */
214 1
    public function getTemplateCount()
215
    {
216 1
        return (int) $this->get('/templates/count');
217
    }
218
219
    /**
220
     * Return an array properties for the templates in template storage
221
     *
222
     * @return array|null
223
     */
224 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...
225
    {
226 1
        $ret = null;
227
228 1
        $propertyMap = new TemplateListPropertyMap();
229
230 1
        $records = $this->get('/templates/list');
231
232 1
        if (is_array($records) && count($records) > 0) {
233 1
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
0 ignored issues
show
Bug introduced by
It seems like buildPropertyMapArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
234 1
            array_walk($ret, function (&$record) {
235 1
                $key = 'modified';
236 1
                if (isset($record[$key])) {
237 1
                    $record[$key] = StaticFilter::execute($record[$key], 'DateTimeToTimestamp');
238 1
                }
239 1
            });
240 1
        }
241
242 1
        return $ret;
243
    }
244
245
    /**
246
     * Return the number of pages in a template in template storage
247
     *
248
     * @param string $templateName Template name
249
     *
250
     * @throws InvalidArgumentException
251
     *
252
     * @return int
253
     */
254 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...
255
    {
256 2
        StaticValidator::execute($templateName, 'TemplateName');
257
258
        $query = [
259 1
            'templateName' => $templateName,
260 1
        ];
261
262 1
        return (int) $this->get('/templates/pagecount', $query);
263
    }
264
265
    /**
266
     * Return true, if the template exists in template storage
267
     *
268
     * @param string $templateName Template name
269
     *
270
     * @throws InvalidArgumentException
271
     *
272
     * @return bool
273
     */
274 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...
275
    {
276 2
        StaticValidator::execute($templateName, 'TemplateName');
277
278
        $query = [
279 1
            'templateName' => $templateName,
280 1
        ];
281
282 1
        return (bool) $this->get('/templates/exists', $query);
283
    }
284
285
    /**
286
     * Return an array of available fonts on the Reporting Cloud service
287
     *
288
     * @return array|null
289
     */
290 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...
291
    {
292 1
        $ret = null;
293
294 1
        $fonts = $this->get('/fonts/list');
295
296 1
        if (is_array($fonts) && count($fonts) > 0) {
297 1
            $ret = array_map('trim', $fonts);
298 1
        }
299
300 1
        return $ret;
301
    }
302
303
    /**
304
     * Return an array properties for the ReportingCloud account
305
     *
306
     * @return null
307
     * @throws \Zend\Filter\Exception\ExceptionInterface
308
     */
309 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...
310
    {
311 1
        $ret = null;
312
313 1
        $propertyMap = new AccountSettingsPropertyMap();
314
315 1
        $records = $this->get('/account/settings');
316
317 1
        if (is_array($records) && count($records) > 0) {
318 1
            $ret = $this->buildPropertyMapArray($records, $propertyMap);
0 ignored issues
show
Bug introduced by
It seems like buildPropertyMapArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
319 1
            $key = 'valid_until';
320 1
            if ($ret[$key]) {
321 1
                $ret[$key] = StaticFilter::execute($ret[$key], 'DateTimeToTimestamp');
322 1
            }
323 1
        }
324
325 1
        return $ret;
326
    }
327
328
    /**
329
     * Download the binary data of a template from template storage
330
     *
331
     * @param string $templateName Template name
332
     *
333
     * @throws InvalidArgumentException
334
     *
335
     * @return null|resource
336
     */
337 2
    public function downloadTemplate($templateName)
338
    {
339 2
        $ret = null;
340
341 2
        StaticValidator::execute($templateName, 'TemplateName');
342
343
        $query = [
344 1
            'templateName' => $templateName,
345 1
        ];
346
347 1
        $data = $this->get('/templates/download', $query);
348
349 1
        if (null !== $data) {
350 1
            $ret = base64_decode($data);
351 1
        }
352
353 1
        return $ret;
354
    }
355
}
356