Completed
Push — master ( 2b4b54...68f327 )
by Daniel
04:56
created

PdfWrite::setOutputFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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
     * 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
     *
67
     * @link http://ghostscript.com/doc/current/Language.htm#.setpdfwrite
68
     */
69
    const POSTSCRIPT_COMMANDS = '.setpdfwrite';
70
71
    /**
72
     * Create PDF write device object
73
     *
74
     * @param Ghostscript $ghostscript
75
     * @param Arguments   $arguments
76
     */
77 26
    public function __construct(Ghostscript $ghostscript, Arguments $arguments)
78
    {
79 26
        parent::__construct($ghostscript, $arguments->setArgument('-sDEVICE=pdfwrite'));
80
81 26
        $this->setPdfSettings(PdfSettings::__DEFAULT);
82 26
    }
83
84
    /**
85
     * Get output file
86
     *
87
     * @return null|string
88
     */
89 4
    public function getOutputFile()
90
    {
91 4
        return $this->getArgumentValue('-sOutputFile');
92
    }
93
94
    /**
95
     * Set output file
96
     *
97
     * @param string $outputFile
98
     *
99
     * @return $this
100
     */
101 4
    public function setOutputFile($outputFile)
102
    {
103 4
        $this->setArgument('-sOutputFile=' . $outputFile);
104
105 4
        return $this;
106
    }
107
108
    /**
109
     * Whether output file is stdout.
110
     *
111
     * @return bool
112
     */
113 2
    public function isOutputStdout()
114
    {
115 2
        return $this->getOutputFile() == '-';
116
    }
117
118
    /**
119
     * Set stdout as output.
120
     *
121
     * @return $this
122
     */
123 2
    public function setOutputStdout()
124
    {
125 2
        return $this->setOutputFile('-');
126
    }
127
128
    /**
129
     * Get PDF settings
130
     *
131
     * @return string
132
     */
133 10
    public function getPdfSettings()
134
    {
135 10
        return ltrim($this->getArgumentValue('-dPDFSETTINGS'), '/');
136
    }
137
138
    /**
139
     * Set PDF settings
140
     *
141
     * @param string $pdfSettings
142
     *
143
     * @throws \InvalidArgumentException
144
     *
145
     * @return $this
146
     */
147 26
    public function setPdfSettings($pdfSettings)
148
    {
149 26
        $pdfSettings = ltrim($pdfSettings, '/');
150 26
        if (!in_array($pdfSettings, PdfSettings::values())) {
151 2
            throw new \InvalidArgumentException('Invalid PDF settings argument');
152
        }
153
154 26
        $this->setArgument(sprintf('-dPDFSETTINGS=/%s', $pdfSettings));
155
156 26
        return $this;
157
    }
158
159
    /**
160
     * Get process color model
161
     *
162
     * @return string
163
     */
164 6
    public function getProcessColorModel()
165
    {
166 6
        return ltrim($this->getArgumentValue('-dProcessColorModel'), '/');
167
    }
168
169
    /**
170
     * Set process color model
171
     *
172
     * @param string $processColorModel
173
     *
174
     * @throws \InvalidArgumentException
175
     *
176
     * @return $this
177
     */
178 8
    public function setProcessColorModel($processColorModel)
179
    {
180 8
        $processColorModel = ltrim($processColorModel, '/');
181 8
        if (!in_array($processColorModel, ProcessColorModel::values())) {
182 2
            throw new \InvalidArgumentException('Invalid process color model argument');
183
        }
184
185 6
        $this->setArgument(sprintf('-dProcessColorModel=/%s', $processColorModel));
186
187 6
        return $this;
188
    }
189
}
190