Completed
Push — master ( 68f327...051b28 )
by Daniel
06:37
created

PdfWrite::createProcess()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 8
nc 4
nop 1
crap 3
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\Ghostscript;
13
use GravityMedia\Ghostscript\Process\Arguments;
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 options
24
     */
25
    use DistillerParametersTrait;
26
27
    /**
28
     * Use color image compression distiller options
29
     */
30
    use DistillerParameters\ColorImageCompressionTrait;
31
32
    /**
33
     * Use grayscale image compression distiller options
34
     */
35
    use DistillerParameters\GrayImageCompressionTrait;
36
37
    /**
38
     * Use monochrome image compression distiller options
39
     */
40
    use DistillerParameters\MonoImageCompressionTrait;
41
42
    /**
43
     * Use page compression distiller options
44
     */
45
    use DistillerParameters\PageCompressionTrait;
46
47
    /**
48
     * Use font distiller options
49
     */
50
    use DistillerParameters\FontTrait;
51
52
    /**
53
     * Use color conversion distiller options
54
     */
55
    use DistillerParameters\ColorConversionTrait;
56
57
    /**
58
     * Use advanced distiller options
59
     */
60
    use DistillerParameters\AdvancedTrait;
61
62
    /**
63
     * Create PDF write device object
64
     *
65
     * @param Ghostscript $ghostscript
66
     * @param Arguments   $arguments
67
     */
68 28
    public function __construct(Ghostscript $ghostscript, Arguments $arguments)
69
    {
70 28
        parent::__construct($ghostscript, $arguments->setArgument('-sDEVICE=pdfwrite'));
71
72 28
        $this->setPdfSettings(PdfSettings::__DEFAULT);
73 28
    }
74
75
    /**
76
     * Get output file
77
     *
78
     * @return null|string
79
     */
80 4
    public function getOutputFile()
81
    {
82 4
        return $this->getArgumentValue('-sOutputFile');
83
    }
84
85
    /**
86
     * Set output file
87
     *
88
     * @param string $outputFile
89
     *
90
     * @return $this
91
     */
92 4
    public function setOutputFile($outputFile)
93
    {
94 4
        $this->setArgument('-sOutputFile=' . $outputFile);
95
96 4
        return $this;
97
    }
98
99
    /**
100
     * Whether output file is stdout.
101
     *
102
     * @return bool
103
     */
104 2
    public function isOutputStdout()
105
    {
106 2
        return $this->getOutputFile() == '-';
107
    }
108
109
    /**
110
     * Set stdout as output.
111
     *
112
     * @return $this
113
     */
114 2
    public function setOutputStdout()
115
    {
116 2
        return $this->setOutputFile('-');
117
    }
118
119
    /**
120
     * Get PDF settings
121
     *
122
     * @return string
123
     */
124 10
    public function getPdfSettings()
125
    {
126 10
        return ltrim($this->getArgumentValue('-dPDFSETTINGS'), '/');
127
    }
128
129
    /**
130
     * Set PDF settings
131
     *
132
     * @param string $pdfSettings
133
     *
134
     * @throws \InvalidArgumentException
135
     *
136
     * @return $this
137
     */
138 28
    public function setPdfSettings($pdfSettings)
139
    {
140 28
        $pdfSettings = ltrim($pdfSettings, '/');
141 28
        if (!in_array($pdfSettings, PdfSettings::values())) {
142 2
            throw new \InvalidArgumentException('Invalid PDF settings argument');
143
        }
144
145 28
        $this->setArgument(sprintf('-dPDFSETTINGS=/%s', $pdfSettings));
146
147 28
        return $this;
148
    }
149
150
    /**
151
     * Get process color model
152
     *
153
     * @return string
154
     */
155 6
    public function getProcessColorModel()
156
    {
157 6
        return ltrim($this->getArgumentValue('-dProcessColorModel'), '/');
158
    }
159
160
    /**
161
     * Set process color model
162
     *
163
     * @param string $processColorModel
164
     *
165
     * @throws \InvalidArgumentException
166
     *
167
     * @return $this
168
     */
169 8
    public function setProcessColorModel($processColorModel)
170
    {
171 8
        $processColorModel = ltrim($processColorModel, '/');
172 8
        if (!in_array($processColorModel, ProcessColorModel::values())) {
173 2
            throw new \InvalidArgumentException('Invalid process color model argument');
174
        }
175
176 6
        $this->setArgument(sprintf('-dProcessColorModel=/%s', $processColorModel));
177
178 6
        return $this;
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184 2
    public function createProcess($input = null)
185
    {
186 2
        $input = $this->sanitizeInput($input);
187
188 2
        $code = $input->getPostScriptCode();
189 2
        if (null === $code) {
190 2
            $code = '';
191 1
        }
192
193 2
        if (false === strstr($code, '.setpdfwrite')) {
194 2
            $input->setPostScriptCode(ltrim($code . ' .setpdfwrite', ' '));
195 1
        }
196
197 2
        return parent::createProcess($input);
198
    }
199
}
200