DistillerParametersTrait   B
last analyzed

Complexity

Total Complexity 47

Size/Duplication

Total Lines 386
Duplicated Lines 3.63 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 47
lcom 2
cbo 2
dl 14
loc 386
ccs 120
cts 120
cp 1
rs 8.64
c 0
b 0
f 0

25 Methods

Rating   Name   Duplication   Size   Complexity  
getArgumentValue() 0 1 ?
setArgument() 0 1 ?
getPdfSettings() 0 1 ?
A setAutoRotatePages() 0 11 2
A getBinding() 0 9 2
A setBinding() 0 11 2
A setCompatibilityLevel() 0 6 1
A getCoreDistVersion() 0 9 2
A setCoreDistVersion() 0 6 1
A setDoThumbnails() 0 6 2
A getEndPage() 0 9 2
A setEndPage() 0 6 1
A getImageMemory() 0 9 2
A setImageMemory() 0 6 1
A getOffOptimizations() 0 9 2
A setOffOptimizations() 0 6 1
A setOptimize() 0 6 2
A getStartPage() 0 9 2
A setStartPage() 0 6 1
A isUseFlateCompression() 0 9 2
A setUseFlateCompression() 0 6 2
A getAutoRotatePages() 0 17 5
A getCompatibilityLevel() 14 14 3
A isDoThumbnails() 0 14 3
A isOptimize() 0 17 6

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like DistillerParametersTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use DistillerParametersTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * This file is part of the Ghostscript package
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Ghostscript\Device;
9
10
use GravityMedia\Ghostscript\Enum\AutoRotatePages;
11
use GravityMedia\Ghostscript\Enum\Binding;
12
use GravityMedia\Ghostscript\Enum\PdfSettings;
13
14
/**
15
 * The general distiller parameters trait.
16
 *
17
 * @package GravityMedia\Ghostscript\Devices
18
 *
19
 * @link    http://ghostscript.com/doc/current/Ps2pdf.htm
20
 */
21
trait DistillerParametersTrait
22
{
23
    /**
24
     * Get argument value
25
     *
26
     * @param string $name
27
     *
28
     * @return null|string
29
     */
30
    abstract protected function getArgumentValue($name);
31
32
    /**
33
     * Set argument
34
     *
35
     * @param string $argument
36
     *
37
     * @return $this
38
     */
39
    abstract protected function setArgument($argument);
40
41
    /**
42
     * Get PDF settings
43
     *
44
     * @return string
45
     */
46
    abstract public function getPdfSettings();
47
48
    /**
49
     * Get auto rotate pages
50
     *
51
     * @return string
52
     */
53 10
    public function getAutoRotatePages()
54
    {
55 10
        $value = $this->getArgumentValue('-dAutoRotatePages');
56 10
        if (null === $value) {
57 10
            switch ($this->getPdfSettings()) {
58 10
                case PdfSettings::EBOOK:
59 2
                    return AutoRotatePages::ALL;
60 8
                case PdfSettings::PRINTER:
61 7
                case PdfSettings::PREPRESS:
62 4
                    return AutoRotatePages::NONE;
63 2
                default:
64 4
                    return AutoRotatePages::PAGE_BY_PAGE;
65 2
            }
66
        }
67
68 10
        return ltrim($value, '/');
69
    }
70
71
    /**
72
     * Set auto rotate pages
73
     *
74
     * @param string $autoRotatePages
75
     *
76
     * @param \InvalidArgumentException
77
     *
78
     * @return $this
79
     */
80 12
    public function setAutoRotatePages($autoRotatePages)
81
    {
82 12
        $autoRotatePages = ltrim($autoRotatePages, '/');
83 12
        if (!in_array($autoRotatePages, AutoRotatePages::values())) {
84 2
            throw new \InvalidArgumentException('Invalid auto rotate pages argument');
85
        }
86
87 10
        $this->setArgument(sprintf('-dAutoRotatePages=/%s', $autoRotatePages));
88
89 10
        return $this;
90
    }
91
92
    /**
93
     * Get binding
94
     *
95
     * @return string
96
     */
97 2
    public function getBinding()
98
    {
99 2
        $value = $this->getArgumentValue('-dBinding');
100 2
        if (null === $value) {
101 2
            return Binding::LEFT;
102
        }
103
104 2
        return ltrim($value, '/');
105
    }
106
107
    /**
108
     * Set binding
109
     *
110
     * @param string $binding
111
     *
112
     * @param \InvalidArgumentException
113
     *
114
     * @return $this
115
     */
116 4
    public function setBinding($binding)
117
    {
118 4
        $binding = ltrim($binding, '/');
119 4
        if (!in_array($binding, Binding::values())) {
120 2
            throw new \InvalidArgumentException('Invalid binding argument');
121
        }
122
123 2
        $this->setArgument(sprintf('-dBinding=/%s', $binding));
124
125 2
        return $this;
126
    }
127
128
    /**
129
     * Get compatibility level
130
     *
131
     * @return float
132
     */
133 10 View Code Duplication
    public function getCompatibilityLevel()
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...
134
    {
135 10
        $value = $this->getArgumentValue('-dCompatibilityLevel');
136 10
        if (null === $value) {
137 10
            switch ($this->getPdfSettings()) {
138 10
                case PdfSettings::SCREEN:
139 2
                    return 1.3;
140 4
                default:
141 8
                    return 1.4;
142 4
            }
143
        }
144
145 10
        return floatval($value);
146
    }
147
148
    /**
149
     * Set compatibility level
150
     *
151
     * @param float $compatibilityLevel
152
     *
153
     * @return $this
154
     */
155 10
    public function setCompatibilityLevel($compatibilityLevel)
156
    {
157 10
        $this->setArgument(sprintf('-dCompatibilityLevel=%s', $compatibilityLevel));
158
159 10
        return $this;
160
    }
161
162
    /**
163
     * Get core dist version
164
     *
165
     * @return int
166
     */
167 2
    public function getCoreDistVersion()
168
    {
169 2
        $value = $this->getArgumentValue('-dCoreDistVersion');
170 2
        if (null === $value) {
171 2
            return 4000;
172
        }
173
174 2
        return intval($value);
175
    }
176
177
    /**
178
     * Set core dist version
179
     *
180
     * @param int $coreDistVersion
181
     *
182
     * @return $this
183
     */
184 2
    public function setCoreDistVersion($coreDistVersion)
185
    {
186 2
        $this->setArgument(sprintf('-dCoreDistVersion=%s', $coreDistVersion));
187
188 2
        return $this;
189
    }
190
191
    /**
192
     * Whether to do thumbnails
193
     *
194
     * @return bool
195
     */
196 10
    public function isDoThumbnails()
197
    {
198 10
        $value = $this->getArgumentValue('-dDoThumbnails');
199 10
        if (null === $value) {
200 10
            switch ($this->getPdfSettings()) {
201 10
                case PdfSettings::PREPRESS:
202 2
                    return true;
203 4
                default:
204 8
                    return false;
205 4
            }
206
        }
207
208 10
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
209
    }
210
211
    /**
212
     * Set do thumbnails flag
213
     *
214
     * @param bool $doThumbnails
215
     *
216
     * @return $this
217
     */
218 10
    public function setDoThumbnails($doThumbnails)
219
    {
220 10
        $this->setArgument(sprintf('-dDoThumbnails=%s', $doThumbnails ? 'true' : 'false'));
221
222 10
        return $this;
223
    }
224
225
    /**
226
     * Get end page
227
     *
228
     * @return int
229
     */
230 2
    public function getEndPage()
231
    {
232 2
        $value = $this->getArgumentValue('-dEndPage');
233 2
        if (null === $value) {
234 2
            return -1;
235
        }
236
237 2
        return intval($value);
238
    }
239
240
    /**
241
     * Set end page
242
     *
243
     * @param int $endPage
244
     *
245
     * @return $this
246
     */
247 2
    public function setEndPage($endPage)
248
    {
249 2
        $this->setArgument(sprintf('-dEndPage=%s', $endPage));
250
251 2
        return $this;
252
    }
253
254
    /**
255
     * Get image memory
256
     *
257
     * @return int
258
     */
259 2
    public function getImageMemory()
260
    {
261 2
        $value = $this->getArgumentValue('-dImageMemory');
262 2
        if (null === $value) {
263 2
            return 524288;
264
        }
265
266 2
        return intval($value);
267
    }
268
269
    /**
270
     * Set image memory
271
     *
272
     * @param int $imageMemory
273
     *
274
     * @return $this
275
     */
276 2
    public function setImageMemory($imageMemory)
277
    {
278 2
        $this->setArgument(sprintf('-dImageMemory=%s', $imageMemory));
279
280 2
        return $this;
281
    }
282
283
    /**
284
     * Get off optimizations
285
     *
286
     * @return int
287
     */
288 2
    public function getOffOptimizations()
289
    {
290 2
        $value = $this->getArgumentValue('-dOffOptimizations');
291 2
        if (null === $value) {
292 2
            return 0;
293
        }
294
295 2
        return intval($value);
296
    }
297
298
    /**
299
     * Set off optimizations
300
     *
301
     * @param int $offOptimizations
302
     *
303
     * @return $this
304
     */
305 2
    public function setOffOptimizations($offOptimizations)
306
    {
307 2
        $this->setArgument(sprintf('-dOffOptimizations=%s', $offOptimizations));
308
309 2
        return $this;
310
    }
311
312
    /**
313
     * Whether to optimize
314
     *
315
     * @return bool
316
     */
317 10
    public function isOptimize()
318
    {
319 10
        $value = $this->getArgumentValue('-dOptimize');
320 10
        if (null === $value) {
321 10
            switch ($this->getPdfSettings()) {
322 10
                case PdfSettings::SCREEN:
323 9
                case PdfSettings::EBOOK:
324 8
                case PdfSettings::PRINTER:
325 7
                case PdfSettings::PREPRESS:
326 8
                    return true;
327 1
                default:
328 2
                    return false;
329 1
            }
330
        }
331
332 10
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
333
    }
334
335
    /**
336
     * Set optimize flag
337
     *
338
     * @param bool $optimize
339
     *
340
     * @return $this
341
     */
342 10
    public function setOptimize($optimize)
343
    {
344 10
        $this->setArgument(sprintf('-dOptimize=%s', $optimize ? 'true' : 'false'));
345
346 10
        return $this;
347
    }
348
349
    /**
350
     * Get start page
351
     *
352
     * @return int
353
     */
354 2
    public function getStartPage()
355
    {
356 2
        $value = $this->getArgumentValue('-dStartPage');
357 2
        if (null === $value) {
358 2
            return 1;
359
        }
360
361 2
        return intval($value);
362
    }
363
364
    /**
365
     * Set start page
366
     *
367
     * @param int $startPage
368
     *
369
     * @return $this
370
     */
371 2
    public function setStartPage($startPage)
372
    {
373 2
        $this->setArgument(sprintf('-dStartPage=%s', $startPage));
374
375 2
        return $this;
376
    }
377
378
    /**
379
     * Whether to use flate compression
380
     *
381
     * @return bool
382
     */
383 2
    public function isUseFlateCompression()
384
    {
385 2
        $value = $this->getArgumentValue('-dUseFlateCompression');
386 2
        if (null === $value) {
387 2
            return true;
388
        }
389
390 2
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
391
    }
392
393
    /**
394
     * Set use flate compression flag
395
     *
396
     * @param bool $useFlateCompression
397
     *
398
     * @return $this
399
     */
400 2
    public function setUseFlateCompression($useFlateCompression)
401
    {
402 2
        $this->setArgument(sprintf('-dUseFlateCompression=%s', $useFlateCompression ? 'true' : 'false'));
403
404 2
        return $this;
405
    }
406
}
407