MonoImageCompressionTrait   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 290
Duplicated Lines 10 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 34
lcom 2
cbo 2
dl 29
loc 290
ccs 87
cts 87
cp 1
rs 9.68
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
getArgumentValue() 0 1 ?
setArgument() 0 1 ?
getPdfSettings() 0 1 ?
A isAntiAliasMonoImages() 0 9 2
A setAntiAliasMonoImages() 0 6 2
A setDownsampleMonoImages() 0 6 2
A isEncodeMonoImages() 0 9 2
A setEncodeMonoImages() 0 6 2
A getMonoImageDepth() 0 9 2
A setMonoImageDepth() 0 6 1
A getMonoImageDownsampleThreshold() 0 9 2
A setMonoImageDownsampleThreshold() 0 6 1
A setMonoImageDownsampleType() 0 11 2
A getMonoImageFilter() 0 9 2
A setMonoImageFilter() 0 11 2
A setMonoImageResolution() 0 6 1
A isDownsampleMonoImages() 0 15 4
A getMonoImageDownsampleType() 14 14 3
A getMonoImageResolution() 15 15 4

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
 * 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\ImageDownsampleType;
11
use GravityMedia\Ghostscript\Enum\MonoImageFilter;
12
use GravityMedia\Ghostscript\Enum\PdfSettings;
13
14
/**
15
 * The monochrome image compression distiller parameters trait.
16
 *
17
 * @package GravityMedia\Ghostscript\Device\DistillerParameters
18
 *
19
 * @link    http://ghostscript.com/doc/current/Ps2pdf.htm
20
 */
21
trait MonoImageCompressionTrait
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
     * Whether to anti alias monochrome images
50
     *
51
     * @return bool
52
     */
53 2
    public function isAntiAliasMonoImages()
54
    {
55 2
        $value = $this->getArgumentValue('-dAntiAliasMonoImages');
56 2
        if (null === $value) {
57 2
            return false;
58
        }
59
60 2
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
61
    }
62
63
    /**
64
     * Set anti alias monochrome images flag
65
     *
66
     * @param bool $antiAliasMonoImages
67
     *
68
     * @return $this
69
     */
70 2
    public function setAntiAliasMonoImages($antiAliasMonoImages)
71
    {
72 2
        $this->setArgument(sprintf('-dAntiAliasMonoImages=%s', $antiAliasMonoImages ? 'true' : 'false'));
73
74 2
        return $this;
75
    }
76
77
    /**
78
     * Whether to downsample monochrome images
79
     *
80
     * @return bool
81
     */
82 10
    public function isDownsampleMonoImages()
83
    {
84 10
        $value = $this->getArgumentValue('-dDownsampleMonoImages');
85 10
        if (null === $value) {
86 10
            switch ($this->getPdfSettings()) {
87 10
                case PdfSettings::SCREEN:
88 9
                case PdfSettings::EBOOK:
89 4
                    return true;
90 3
                default:
91 6
                    return false;
92 3
            }
93
        }
94
95 10
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
96
    }
97
98
    /**
99
     * Set downsample monochrome images flag
100
     *
101
     * @param bool $downsampleMonoImages
102
     *
103
     * @return $this
104
     */
105 10
    public function setDownsampleMonoImages($downsampleMonoImages)
106
    {
107 10
        $this->setArgument(sprintf('-dDownsampleMonoImages=%s', $downsampleMonoImages ? 'true' : 'false'));
108
109 10
        return $this;
110
    }
111
112
    /**
113
     * Whether to encode monochrome images
114
     *
115
     * @return bool
116
     */
117 2
    public function isEncodeMonoImages()
118
    {
119 2
        $value = $this->getArgumentValue('-dEncodeMonoImages');
120 2
        if (null === $value) {
121 2
            return true;
122
        }
123
124 2
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
125
    }
126
127
    /**
128
     * Set encode monochrome images flag
129
     *
130
     * @param bool $encodeMonoImages
131
     *
132
     * @return $this
133
     */
134 2
    public function setEncodeMonoImages($encodeMonoImages)
135
    {
136 2
        $this->setArgument(sprintf('-dEncodeMonoImages=%s', $encodeMonoImages ? 'true' : 'false'));
137
138 2
        return $this;
139
    }
140
141
    /**
142
     * Get monochrome image depth
143
     *
144
     * @return int
145
     */
146 2
    public function getMonoImageDepth()
147
    {
148 2
        $value = $this->getArgumentValue('-dMonoImageDepth');
149 2
        if (null === $value) {
150 2
            return -1;
151
        }
152
153 2
        return intval($value);
154
    }
155
156
    /**
157
     * Set monochrome image depth
158
     *
159
     * @param int $monoImageDepth
160
     *
161
     * @return $this
162
     */
163 2
    public function setMonoImageDepth($monoImageDepth)
164
    {
165 2
        $this->setArgument(sprintf('-dMonoImageDepth=%s', $monoImageDepth));
166
167 2
        return $this;
168
    }
169
170
    /**
171
     * Get monochrome image downsample threshold
172
     *
173
     * @return float
174
     */
175 2
    public function getMonoImageDownsampleThreshold()
176
    {
177 2
        $value = $this->getArgumentValue('-dMonoImageDownsampleThreshold');
178 2
        if (null === $value) {
179 2
            return 1.5;
180
        }
181
182 2
        return floatval($value);
183
    }
184
185
    /**
186
     * Set monochrome image downsample threshold
187
     *
188
     * @param float $monoImageDownsampleThreshold
189
     *
190
     * @return $this
191
     */
192 2
    public function setMonoImageDownsampleThreshold($monoImageDownsampleThreshold)
193
    {
194 2
        $this->setArgument(sprintf('-dMonoImageDownsampleThreshold=%s', $monoImageDownsampleThreshold));
195
196 2
        return $this;
197
    }
198
199
    /**
200
     * Get monochrome image downsample type
201
     *
202
     * @return string
203
     */
204 10 View Code Duplication
    public function getMonoImageDownsampleType()
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...
205
    {
206 10
        $value = $this->getArgumentValue('-dMonoImageDownsampleType');
207 10
        if (null === $value) {
208 10
            switch ($this->getPdfSettings()) {
209 10
                case PdfSettings::PREPRESS:
210 2
                    return ImageDownsampleType::BICUBIC;
211 4
                default:
212 8
                    return ImageDownsampleType::SUBSAMPLE;
213 4
            }
214
        }
215
216 10
        return ltrim($value, '/');
217
    }
218
219
    /**
220
     * Set monochrome image downsample type
221
     *
222
     * @param string $monoImageDownsampleType
223
     *
224
     * @throws \InvalidArgumentException
225
     *
226
     * @return $this
227
     */
228 12
    public function setMonoImageDownsampleType($monoImageDownsampleType)
229
    {
230 12
        $monoImageDownsampleType = ltrim($monoImageDownsampleType, '/');
231 12
        if (!in_array($monoImageDownsampleType, ImageDownsampleType::values())) {
232 2
            throw new \InvalidArgumentException('Invalid monochrome image downsample type argument');
233
        }
234
235 10
        $this->setArgument(sprintf('-dMonoImageDownsampleType=/%s', $monoImageDownsampleType));
236
237 10
        return $this;
238
    }
239
240
    /**
241
     * Get monochrome image filter
242
     *
243
     * @return string
244
     */
245 8
    public function getMonoImageFilter()
246
    {
247 8
        $value = $this->getArgumentValue('-dMonoImageFilter');
248 8
        if (null === $value) {
249 2
            return MonoImageFilter::CCITT_FAX_ENCODE;
250
        }
251
252 6
        return ltrim($value, '/');
253
    }
254
255
    /**
256
     * Set monochrome image filter
257
     *
258
     * @param string $monoImageFilter
259
     *
260
     * @throws \InvalidArgumentException
261
     *
262
     * @return $this
263
     */
264 8
    public function setMonoImageFilter($monoImageFilter)
265
    {
266 8
        $monoImageFilter = ltrim($monoImageFilter, '/');
267 8
        if (!in_array($monoImageFilter, MonoImageFilter::values())) {
268 2
            throw new \InvalidArgumentException('Invalid monochrome image filter argument');
269
        }
270
271 6
        $this->setArgument(sprintf('-dMonoImageFilter=/%s', $monoImageFilter));
272
273 6
        return $this;
274
    }
275
276
    /**
277
     * Get monochrome image resolution
278
     *
279
     * @return int
280
     */
281 10 View Code Duplication
    public function getMonoImageResolution()
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...
282
    {
283 10
        $value = $this->getArgumentValue('-dMonoImageResolution');
284 10
        if (null === $value) {
285 10
            switch ($this->getPdfSettings()) {
286 10
                case PdfSettings::PRINTER:
287 9
                case PdfSettings::PREPRESS:
288 4
                    return 1200;
289 3
                default:
290 6
                    return 300;
291 3
            }
292
        }
293
294 10
        return intval($value);
295
    }
296
297
    /**
298
     * Set monochrome image resolution
299
     *
300
     * @param int $monoImageResolution
301
     *
302
     * @return $this
303
     */
304 10
    public function setMonoImageResolution($monoImageResolution)
305
    {
306 10
        $this->setArgument(sprintf('-dMonoImageResolution=%s', $monoImageResolution));
307
308 10
        return $this;
309
    }
310
}
311