ColorConversionTrait   B
last analyzed

Complexity

Total Complexity 51

Size/Duplication

Total Lines 423
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 51
lcom 2
cbo 4
dl 0
loc 423
ccs 128
cts 128
cp 1
rs 7.92
c 0
b 0
f 0

27 Methods

Rating   Name   Duplication   Size   Complexity  
getArgumentValue() 0 1 ?
setArgument() 0 1 ?
getPdfSettings() 0 1 ?
A getCalCmykProfile() 0 9 2
A setCalCmykProfile() 0 6 1
A getCalGrayProfile() 0 9 2
A setCalGrayProfile() 0 6 1
A getCalRgbProfile() 0 9 2
A setCalRgbProfile() 0 6 1
A setColorConversionStrategy() 0 11 2
A isConvertCmykImagesToRgb() 0 9 2
A setConvertCmykImagesToRgb() 0 6 2
A isConvertImagesToIndexed() 0 9 2
A setConvertImagesToIndexed() 0 6 2
A getDefaultRenderingIntent() 0 9 2
A setDefaultRenderingIntent() 0 11 2
A isPreserveHalftoneInfo() 0 9 2
A setPreserveHalftoneInfo() 0 6 2
A setPreserveOverprintSettings() 0 6 2
A getSRgbProfile() 0 9 2
A setSRgbProfile() 0 6 1
A getTransferFunctionInfo() 0 9 2
A setTransferFunctionInfo() 0 11 2
A setUcrAndBgInfo() 0 11 2
A getColorConversionStrategy() 0 17 5
A isPreserveOverprintSettings() 0 15 4
A getUcrAndBgInfo() 0 15 4

How to fix   Complexity   

Complex Class

Complex classes like ColorConversionTrait 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 ColorConversionTrait, 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\DistillerParameters;
9
10
use GravityMedia\Ghostscript\Enum\ColorConversionStrategy;
11
use GravityMedia\Ghostscript\Enum\DefaultRenderingIntent;
12
use GravityMedia\Ghostscript\Enum\PdfSettings;
13
use GravityMedia\Ghostscript\Enum\TransferFunctionInfo;
14
use GravityMedia\Ghostscript\Enum\UcrAndBgInfo;
15
16
/**
17
 * The color conversion distiller parameters trait.
18
 *
19
 * @package GravityMedia\Ghostscript\Device\DistillerParameters
20
 *
21
 * @link    http://ghostscript.com/doc/current/Ps2pdf.htm
22
 */
23
trait ColorConversionTrait
24
{
25
    /**
26
     * Get argument value
27
     *
28
     * @param string $name
29
     *
30
     * @return null|string
31
     */
32
    abstract protected function getArgumentValue($name);
33
34
    /**
35
     * Set argument
36
     *
37
     * @param string $argument
38
     *
39
     * @return $this
40
     */
41
    abstract protected function setArgument($argument);
42
43
    /**
44
     * Get PDF settings
45
     *
46
     * @return string
47
     */
48
    abstract public function getPdfSettings();
49
50
    /**
51
     * Get cal CMYK profile
52
     *
53
     * @return null|string
54
     */
55 2
    public function getCalCmykProfile()
56
    {
57 2
        $value = $this->getArgumentValue('-dCalCMYKProfile');
58 2
        if (null === $value) {
59 2
            return null;
60
        }
61
62 2
        return trim($value, '()');
63
    }
64
65
    /**
66
     * Set cal CMYK profile
67
     *
68
     * @param string $valCmykProfile
69
     *
70
     * @return $this
71
     */
72 2
    public function setCalCmykProfile($valCmykProfile)
73
    {
74 2
        $this->setArgument(sprintf('-dCalCMYKProfile=(%s)', $valCmykProfile));
75
76 2
        return $this;
77
    }
78
79
    /**
80
     * Get cal gray profile
81
     *
82
     * @return null|string
83
     */
84 2
    public function getCalGrayProfile()
85
    {
86 2
        $value = $this->getArgumentValue('-dCalGrayProfile');
87 2
        if (null === $value) {
88 2
            return null;
89
        }
90
91 2
        return trim($value, '()');
92
    }
93
94
    /**
95
     * Set cal gray profile
96
     *
97
     * @param string $valGrayProfile
98
     *
99
     * @return $this
100
     */
101 2
    public function setCalGrayProfile($valGrayProfile)
102
    {
103 2
        $this->setArgument(sprintf('-dCalGrayProfile=(%s)', $valGrayProfile));
104
105 2
        return $this;
106
    }
107
108
    /**
109
     * Get cal RGB profile
110
     *
111
     * @return null|string
112
     */
113 2
    public function getCalRgbProfile()
114
    {
115 2
        $value = $this->getArgumentValue('-dCalRGBProfile');
116 2
        if (null === $value) {
117 2
            return null;
118
        }
119
120 2
        return trim($value, '()');
121
    }
122
123
    /**
124
     * Set cal RGB profile
125
     *
126
     * @param string $valRgbProfile
127
     *
128
     * @return $this
129
     */
130 2
    public function setCalRgbProfile($valRgbProfile)
131
    {
132 2
        $this->setArgument(sprintf('-dCalRGBProfile=(%s)', $valRgbProfile));
133
134 2
        return $this;
135
    }
136
137
    /**
138
     * Get color conversion strategy
139
     *
140
     * @return string
141
     */
142 10
    public function getColorConversionStrategy()
143
    {
144 10
        $value = $this->getArgumentValue('-dColorConversionStrategy');
145 10
        if (null === $value) {
146 10
            switch ($this->getPdfSettings()) {
147 10
                case PdfSettings::SCREEN:
148 9
                case PdfSettings::EBOOK:
149 4
                    return ColorConversionStrategy::SRGB;
150 6
                case PdfSettings::PRINTER:
151 2
                    return ColorConversionStrategy::USE_DEVICE_INDEPENDENT_COLOR;
152 2
                default:
153 4
                    return ColorConversionStrategy::LEAVE_COLOR_UNCHANGED;
154 2
            }
155
        }
156
157 10
        return ltrim($value, '/');
158
    }
159
160
    /**
161
     * Set color conversion strategy
162
     *
163
     * @param string $colorConversionStrategy
164
     *
165
     * @throws \InvalidArgumentException
166
     *
167
     * @return $this
168
     */
169 12
    public function setColorConversionStrategy($colorConversionStrategy)
170
    {
171 12
        $colorConversionStrategy = ltrim($colorConversionStrategy, '/');
172 12
        if (!in_array($colorConversionStrategy, ColorConversionStrategy::values())) {
173 2
            throw new \InvalidArgumentException('Invalid color conversion strategy argument');
174
        }
175
176 10
        $this->setArgument(sprintf('-dColorConversionStrategy=/%s', $colorConversionStrategy));
177
178 10
        return $this;
179
    }
180
181
    /**
182
     * Whether to convert CMYK images to RGB
183
     *
184
     * @return bool
185
     */
186 2
    public function isConvertCmykImagesToRgb()
187
    {
188 2
        $value = $this->getArgumentValue('-dConvertCMYKImagesToRGB');
189 2
        if (null === $value) {
190 2
            return false;
191
        }
192
193 2
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
194
    }
195
196
    /**
197
     * Set convert convert CMYK images to RGB flag
198
     *
199
     * @param bool $convertCmykImagesToRgb
200
     *
201
     * @return $this
202
     */
203 2
    public function setConvertCmykImagesToRgb($convertCmykImagesToRgb)
204
    {
205 2
        $this->setArgument(sprintf('-dConvertCMYKImagesToRGB=%s', $convertCmykImagesToRgb ? 'true' : 'false'));
206
207 2
        return $this;
208
    }
209
210
    /**
211
     * Whether to convert images to indexed
212
     *
213
     * @return bool
214
     */
215 2
    public function isConvertImagesToIndexed()
216
    {
217 2
        $value = $this->getArgumentValue('-dConvertImagesToIndexed');
218 2
        if (null === $value) {
219 2
            return false;
220
        }
221
222 2
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
223
    }
224
225
    /**
226
     * Set convert images to indexed flag
227
     *
228
     * @param bool $convertImagesToIndexed
229
     *
230
     * @return $this
231
     */
232 2
    public function setConvertImagesToIndexed($convertImagesToIndexed)
233
    {
234 2
        $this->setArgument(sprintf('-dConvertImagesToIndexed=%s', $convertImagesToIndexed ? 'true' : 'false'));
235
236 2
        return $this;
237
    }
238
239
    /**
240
     * Get default rendering intent
241
     *
242
     * @return string
243
     */
244 12
    public function getDefaultRenderingIntent()
245
    {
246 12
        $value = $this->getArgumentValue('-dDefaultRenderingIntent');
247 12
        if (null === $value) {
248 2
            return DefaultRenderingIntent::__DEFAULT;
249
        }
250
251 10
        return ltrim($value, '/');
252
    }
253
254
    /**
255
     * Set default rendering intent
256
     *
257
     * @param string $defaultRenderingIntent
258
     *
259
     * @throws \InvalidArgumentException
260
     *
261
     * @return $this
262
     */
263 12
    public function setDefaultRenderingIntent($defaultRenderingIntent)
264
    {
265 12
        $defaultRenderingIntent = ltrim($defaultRenderingIntent, '/');
266 12
        if (!in_array($defaultRenderingIntent, DefaultRenderingIntent::values())) {
267 2
            throw new \InvalidArgumentException('Invalid default rendering intent argument');
268
        }
269
270 10
        $this->setArgument(sprintf('-dDefaultRenderingIntent=/%s', $defaultRenderingIntent));
271
272 10
        return $this;
273
    }
274
275
    /**
276
     * Whether to preserve halftone info
277
     *
278
     * @return bool
279
     */
280 2
    public function isPreserveHalftoneInfo()
281
    {
282 2
        $value = $this->getArgumentValue('-dPreserveHalftoneInfo');
283 2
        if (null === $value) {
284 2
            return false;
285
        }
286
287 2
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
288
    }
289
290
    /**
291
     * Set preserve halftone info flag
292
     *
293
     * @param bool $preserveHalftoneInfo
294
     *
295
     * @return $this
296
     */
297 2
    public function setPreserveHalftoneInfo($preserveHalftoneInfo)
298
    {
299 2
        $this->setArgument(sprintf('-dPreserveHalftoneInfo=%s', $preserveHalftoneInfo ? 'true' : 'false'));
300
301 2
        return $this;
302
    }
303
304
    /**
305
     * Whether to preserve overprint settings
306
     *
307
     * @return bool
308
     */
309 10
    public function isPreserveOverprintSettings()
310
    {
311 10
        $value = $this->getArgumentValue('-dPreserveOverprintSettings');
312 10
        if (null === $value) {
313 10
            switch ($this->getPdfSettings()) {
314 10
                case PdfSettings::PRINTER:
315 9
                case PdfSettings::PREPRESS:
316 4
                    return true;
317 3
                default:
318 6
                    return false;
319 3
            }
320
        }
321
322 10
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
323
    }
324
325
    /**
326
     * Set preserve overprint settings flag
327
     *
328
     * @param bool $preserveOverprintSettings
329
     *
330
     * @return $this
331
     */
332 10
    public function setPreserveOverprintSettings($preserveOverprintSettings)
333
    {
334 10
        $this->setArgument(sprintf('-dPreserveOverprintSettings=%s', $preserveOverprintSettings ? 'true' : 'false'));
335
336 10
        return $this;
337
    }
338
339
    /**
340
     * Get sRGB profile
341
     *
342
     * @return null|string
343
     */
344 2
    public function getSRgbProfile()
345
    {
346 2
        $value = $this->getArgumentValue('-dsRGBProfile');
347 2
        if (null === $value) {
348 2
            return null;
349
        }
350
351 2
        return trim($value, '()');
352
    }
353
354
    /**
355
     * Set sRGB profile
356
     *
357
     * @param string $sRgbProfile
358
     *
359
     * @return $this
360
     */
361 2
    public function setSRgbProfile($sRgbProfile)
362
    {
363 2
        $this->setArgument(sprintf('-dsRGBProfile=(%s)', $sRgbProfile));
364
365 2
        return $this;
366
    }
367
368
    /**
369
     * Get transfer function info
370
     *
371
     * @return string
372
     */
373 8
    public function getTransferFunctionInfo()
374
    {
375 8
        $value = $this->getArgumentValue('-dTransferFunctionInfo');
376 8
        if (null === $value) {
377 2
            return TransferFunctionInfo::PRESERVE;
378
        }
379
380 6
        return ltrim($value, '/');
381
    }
382
383
    /**
384
     * Set transfer function info
385
     *
386
     * @param string $transferFunctionInfo
387
     *
388
     * @throws \InvalidArgumentException
389
     *
390
     * @return $this
391
     */
392 8
    public function setTransferFunctionInfo($transferFunctionInfo)
393
    {
394 8
        $transferFunctionInfo = ltrim($transferFunctionInfo, '/');
395 8
        if (!in_array($transferFunctionInfo, TransferFunctionInfo::values())) {
396 2
            throw new \InvalidArgumentException('Invalid transfer function info argument');
397
        }
398
399 6
        $this->setArgument(sprintf('-dTransferFunctionInfo=/%s', $transferFunctionInfo));
400
401 6
        return $this;
402
    }
403
404
    /**
405
     * Get UCR and BG info
406
     *
407
     * @return string
408
     */
409 10
    public function getUcrAndBgInfo()
410
    {
411 10
        $value = $this->getArgumentValue('-dUCRandBGInfo');
412 10
        if (null === $value) {
413 10
            switch ($this->getPdfSettings()) {
414 10
                case PdfSettings::PRINTER:
415 9
                case PdfSettings::PREPRESS:
416 4
                    return UcrAndBgInfo::PRESERVE;
417 3
                default:
418 6
                    return UcrAndBgInfo::REMOVE;
419 3
            }
420
        }
421
422 10
        return ltrim($value, '/');
423
    }
424
425
    /**
426
     * Set UCR and BG info
427
     *
428
     * @param string $ucrAndBgInfo
429
     *
430
     * @throws \InvalidArgumentException
431
     *
432
     * @return $this
433
     */
434 12
    public function setUcrAndBgInfo($ucrAndBgInfo)
435
    {
436 12
        $ucrAndBgInfo = ltrim($ucrAndBgInfo, '/');
437 12
        if (!in_array($ucrAndBgInfo, UcrAndBgInfo::values())) {
438 2
            throw new \InvalidArgumentException('Invalid UCR and BG info argument');
439
        }
440
441 10
        $this->setArgument(sprintf('-dUCRandBGInfo=/%s', $ucrAndBgInfo));
442
443 10
        return $this;
444
    }
445
}
446