Completed
Push — master ( 7db5b7...e686f3 )
by Daniel
03:06
created

DistillerParametersTrait::isOptimize()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

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