Completed
Pull Request — master (#1)
by
unknown
04:25
created

PdfWrite::setProcessColorModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
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\PdfSettings;
11
use GravityMedia\Ghostscript\Enum\ProcessColorModel;
12
use GravityMedia\Ghostscript\Process\Arguments as ProcessArguments;
13
use Symfony\Component\Process\ProcessBuilder;
14
15
/**
16
 * The PDF write device class
17
 *
18
 * @package GravityMedia\Ghostscript\Devices
19
 */
20
class PdfWrite extends AbstractDevice
21
{
22
    /**
23
     * Use distiller parameters
24
     */
25
    use DistillerParametersTrait;
26
27
    /**
28
     * Use color image compression distiller parameters
29
     */
30
    use DistillerParameters\ColorImageCompressionTrait;
31
32
    /**
33
     * Use grayscale image compression distiller parameters
34
     */
35
    use DistillerParameters\GrayImageCompressionTrait;
36
37
    /**
38
     * Use monochrome image compression distiller parameters
39
     */
40
    use DistillerParameters\MonoImageCompressionTrait;
41
42
    /**
43
     * Use page compression distiller parameters
44
     */
45
    use DistillerParameters\PageCompressionTrait;
46
47
    /**
48
     * Use font distiller parameters
49
     */
50
    use DistillerParameters\FontTrait;
51
52
    /**
53
     * Use color conversion distiller parameters
54
     */
55
    use DistillerParameters\ColorConversionTrait;
56
57
    /**
58
     * Use advanced distiller parameters
59
     */
60
    use DistillerParameters\AdvancedTrait;
61
62
    /**
63
     * This operator conditions the environment for the pdfwrite output device. It is a shorthand for setting parameters
64
     * that have been deemed benificial. While not strictly necessary, it is usually helpful to set call this when using
65
     * the pdfwrite device.
66
     * @link http://ghostscript.com/doc/current/Language.htm#.setpdfwrite
67 24
     */
68
    const POSTSCRIPT_COMMANDS = '.setpdfwrite';
69 24
70
    /**
71 24
     * Create PDF write device object
72 24
     *
73
     * @param ProcessBuilder   $builder
74
     * @param ProcessArguments $arguments
75
     */
76
    public function __construct(ProcessBuilder $builder, ProcessArguments $arguments)
77
    {
78
        parent::__construct($builder, $arguments->setArgument('-sDEVICE=pdfwrite'));
79 3
80
        $this->setPdfSettings(PdfSettings::__DEFAULT);
81 3
    }
82
83
    /**
84
     * Get output file
85
     *
86
     * @return null|string
87
     */
88
    public function getOutputFile()
89
    {
90
        return $this->getArgumentValue('-sOutputFile');
91 3
    }
92
93 3
    /**
94
     * Set output file
95 3
     *
96
     * @param string $outputFile
97
     *
98
     * @return $this
99
     */
100
    public function setOutputFile($outputFile)
101
    {
102
        $this->setArgument('-sOutputFile=' . $outputFile);
103 15
104
        return $this;
105 15
    }
106
107
    /**
108
     * Whether output file is stdout.
109
     *
110
     * @return bool
111
     */
112
    public function isOutputStdout()
113
    {
114
        return $this->getOutputFile() == '-';
115
    }
116
117 24
    /**
118
     * Set stdout as output.
119 24
     *
120 24
     * @return $this
121 3
     */
122
    public function setOutputStdout()
123
    {
124 24
        return $this->setOutputFile('-');
125
    }
126 24
127
    /**
128
     * Get PDF settings
129
     *
130
     * @return string
131
     */
132
    public function getPdfSettings()
133
    {
134
        return ltrim($this->getArgumentValue('-dPDFSETTINGS'), '/');
135
    }
136
137
    /**
138
     * Set PDF settings
139
     *
140
     * @param string $pdfSettings
141
     *
142
     * @throws \InvalidArgumentException
143
     *
144
     * @return $this
145
     */
146
    public function setPdfSettings($pdfSettings)
147
    {
148
        $pdfSettings = ltrim($pdfSettings, '/');
149
        if (!in_array($pdfSettings, PdfSettings::values())) {
150
            throw new \InvalidArgumentException('Invalid PDF settings argument');
151
        }
152
153
        $this->setArgument(sprintf('-dPDFSETTINGS=/%s', $pdfSettings));
154
155
        return $this;
156
    }
157
158
    /**
159
     * Get process color model
160
     *
161
     * @return string
162
     */
163
    public function getProcessColorModel()
164
    {
165
        return ltrim($this->getArgumentValue('-dProcessColorModel'), '/');
166
    }
167
168
    /**
169
     * Set process color model
170
     *
171
     * @param string $processColorModel
172
     *
173
     * @throws \InvalidArgumentException
174
     *
175
     * @return $this
176
     */
177
    public function setProcessColorModel($processColorModel)
178
    {
179
        $processColorModel = ltrim($processColorModel, '/');
180
        if (!in_array($processColorModel, ProcessColorModel::values())) {
181
            throw new \InvalidArgumentException('Invalid process color model argument');
182
        }
183
184
        $this->setArgument(sprintf('-dProcessColorModel=/%s', $processColorModel));
185
186
        return $this;
187
    }
188
}
189