Completed
Push — master ( 0ae2b7...d7633b )
by Daniel
03:14
created

DistillerParametersTrait::getOptimize()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 17
ccs 0
cts 13
cp 0
rs 8.8571
nc 6
cc 6
eloc 12
nop 0
crap 42
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\Devices;
9
10
use GravityMedia\Ghostscript\Enum\AutoRotatePages;
11
use GravityMedia\Ghostscript\Enum\Binding;
12
use GravityMedia\Ghostscript\Enum\PdfSettings;
13
14
/**
15
 * The 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 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
    public function getAutoRotatePages()
52
    {
53
        $value = $this->getArgumentValue('-dAutoRotatePages');
54
        if (null === $value) {
55
            switch ($this->getPdfSettings()) {
56
                case PdfSettings::EBOOK:
57
                    return AutoRotatePages::ALL;
58
                case PdfSettings::PRINTER:
59
                case PdfSettings::PREPRESS:
60
                    return AutoRotatePages::NONE;
61
                default:
62
                    return AutoRotatePages::PAGE_BY_PAGE;
63
            }
64
        }
65
66
        return substr($value, 1);
67
    }
68
69
    /**
70
     * Set auto rotate pages
71
     *
72
     * @param string $autoRotatePages
73
     *
74
     * @param \InvalidArgumentException
75
     *
76
     * @return $this
77
     */
78
    public function setAutoRotatePages($autoRotatePages)
79
    {
80
        $autoRotatePages = ltrim($autoRotatePages, '/');
81
        if (!in_array($autoRotatePages, AutoRotatePages::values())) {
82
            throw new \InvalidArgumentException('Invalid auto rotate pages argument');
83
        }
84
85
        $this->setArgument(sprintf('-dAutoRotatePages=/%s', $autoRotatePages));
86
87
        return $this;
88
    }
89
90
    /**
91
     * Get binding
92
     *
93
     * @return string
94
     */
95
    public function getBinding()
96
    {
97
        $value = $this->getArgumentValue('-dBinding');
98
        if (null === $value) {
99
            return Binding::LEFT;
100
        }
101
102
        return substr($value, 1);
103
    }
104
105
    /**
106
     * Set binding
107
     *
108
     * @param string $binding
109
     *
110
     * @param \InvalidArgumentException
111
     *
112
     * @return $this
113
     */
114
    public function setBinding($binding)
115
    {
116
        $binding = ltrim($binding, '/');
117
        if (!in_array($binding, Binding::values())) {
118
            throw new \InvalidArgumentException('Invalid binding argument');
119
        }
120
121
        $this->setArgument(sprintf('-dBinding=/%s', $binding));
122
123
        return $this;
124
    }
125
126
    /**
127
     * Get compatibility level
128
     *
129
     * @return float
130
     */
131 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
        $value = $this->getArgumentValue('-dCompatibilityLevel');
134
        if (null === $value) {
135
            switch ($this->getPdfSettings()) {
136
                case PdfSettings::SCREEN:
137
                    return 1.3;
138
                default:
139
                    return 1.4;
140
            }
141
        }
142
143
        return floatval($value);
144
    }
145
146
    /**
147
     * Set compatibility level
148
     *
149
     * @param float $compatibilityLevel
150
     *
151
     * @return $this
152
     */
153
    public function setCompatibilityLevel($compatibilityLevel)
154
    {
155
        $this->setArgument(sprintf('-dCompatibilityLevel=%s', $compatibilityLevel));
156
157
        return $this;
158
    }
159
160
    /**
161
     * Get core dist version
162
     *
163
     * @return int
164
     */
165
    public function getCoreDistVersion()
166
    {
167
        $value = $this->getArgumentValue('-dCoreDistVersion');
168
        if (null === $value) {
169
            return 4000;
170
        }
171
172
        return intval($value);
173
    }
174
175
    /**
176
     * Set core dist version
177
     *
178
     * @param int $coreDistVersion
179
     *
180
     * @return $this
181
     */
182
    public function setCoreDistVersion($coreDistVersion)
183
    {
184
        $this->setArgument(sprintf('-dCoreDistVersion=%s', $coreDistVersion));
185
186
        return $this;
187
    }
188
189
    /**
190
     * Whether to do thumbnails
191
     *
192
     * @return bool
193
     */
194
    public function getDoThumbnails()
195
    {
196
        $value = $this->getArgumentValue('-dDoThumbnails');
197
        if (null === $value) {
198
            switch ($this->getPdfSettings()) {
199
                case PdfSettings::PREPRESS;
0 ignored issues
show
Coding Style introduced by
case statements should not use curly braces.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
200
                    return true;
201
                default:
202
                    return false;
203
            }
204
        }
205
206
        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
    public function setDoThumbnails($doThumbnails)
217
    {
218
        $this->setArgument(sprintf('-dDoThumbnails=%s', $doThumbnails ? 'true' : 'false'));
219
220
        return $this;
221
    }
222
223
    /**
224
     * Get end page
225
     *
226
     * @return int
227
     */
228
    public function getEndPage()
229
    {
230
        $value = $this->getArgumentValue('-dEndPage');
231
        if (null === $value) {
232
            return -1;
233
        }
234
235
        return intval($value);
236
    }
237
238
    /**
239
     * Set end page
240
     *
241
     * @param int $endPage
242
     *
243
     * @return $this
244
     */
245
    public function setEndPage($endPage)
246
    {
247
        $this->setArgument(sprintf('-dEndPage=%s', $endPage));
248
249
        return $this;
250
    }
251
252
    /**
253
     * Get image memory
254
     *
255
     * @return int
256
     */
257
    public function getImageMemory()
258
    {
259
        $value = $this->getArgumentValue('-dImageMemory');
260
        if (null === $value) {
261
            return 524288;
262
        }
263
264
        return intval($value);
265
    }
266
267
    /**
268
     * Set image memory
269
     *
270
     * @param int $imageMemory
271
     *
272
     * @return $this
273
     */
274
    public function setImageMemory($imageMemory)
275
    {
276
        $this->setArgument(sprintf('-dImageMemory=%s', $imageMemory));
277
278
        return $this;
279
    }
280
281
    /**
282
     * Get off optimizations
283
     *
284
     * @return int
285
     */
286
    public function getOffOptimizations()
287
    {
288
        $value = $this->getArgumentValue('-dOffOptimizations');
289
        if (null === $value) {
290
            return 0;
291
        }
292
293
        return intval($value);
294
    }
295
296
    /**
297
     * Set off optimizations
298
     *
299
     * @param int $offOptimizations
300
     *
301
     * @return $this
302
     */
303
    public function setOffOptimizations($offOptimizations)
304
    {
305
        $this->setArgument(sprintf('-dOffOptimizations=%s', $offOptimizations));
306
307
        return $this;
308
    }
309
310
    /**
311
     * Whether to optimize
312
     *
313
     * @return bool
314
     */
315
    public function getOptimize()
316
    {
317
        $value = $this->getArgumentValue('-dOptimize');
318
        if (null === $value) {
319
            switch ($this->getPdfSettings()) {
320
                case PdfSettings::SCREEN:
321
                case PdfSettings::EBOOK:
322
                case PdfSettings::PRINTER:
323
                case PdfSettings::PREPRESS:
324
                    return true;
325
                default:
326
                    return false;
327
            }
328
        }
329
330
        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
    public function setOptimize($optimize)
341
    {
342
        $this->setArgument(sprintf('-dOptimize=%s', $optimize ? 'true' : 'false'));
343
344
        return $this;
345
    }
346
347
    /**
348
     * Get start page
349
     *
350
     * @return int
351
     */
352
    public function getStartPage()
353
    {
354
        $value = $this->getArgumentValue('-dStartPage');
355
        if (null === $value) {
356
            return 1;
357
        }
358
359
        return intval($value);
360
    }
361
362
    /**
363
     * Set start page
364
     *
365
     * @param int $startPage
366
     *
367
     * @return $this
368
     */
369
    public function setStartPage($startPage)
370
    {
371
        $this->setArgument(sprintf('-dStartPage=%s', $startPage));
372
373
        return $this;
374
    }
375
376
    /**
377
     * Whether to use flate compression
378
     *
379
     * @return bool
380
     */
381
    public function getUseFlateCompression()
382
    {
383
        $value = $this->getArgumentValue('-dUseFlateCompression');
384
        if (null === $value) {
385
            return true;
386
        }
387
388
        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
    public function setUseFlateCompression($useFlateCompression)
399
    {
400
        $this->setArgument(sprintf('-dUseFlateCompression=%s', $useFlateCompression ? 'true' : 'false'));
401
402
        return $this;
403
    }
404
}
405