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

AbstractDevice::setTokenParameter()   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 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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\Device\CommandLineParameters\EpsTrait;
11
use GravityMedia\Ghostscript\Device\CommandLineParameters\FontTrait;
12
use GravityMedia\Ghostscript\Device\CommandLineParameters\IccColorTrait;
13
use GravityMedia\Ghostscript\Device\CommandLineParameters\InteractionTrait;
14
use GravityMedia\Ghostscript\Device\CommandLineParameters\OtherTrait;
15
use GravityMedia\Ghostscript\Device\CommandLineParameters\OutputSelectionTrait;
16
use GravityMedia\Ghostscript\Device\CommandLineParameters\PageTrait;
17
use GravityMedia\Ghostscript\Device\CommandLineParameters\RenderingTrait;
18
use GravityMedia\Ghostscript\Device\CommandLineParameters\ResourceTrait;
19
use GravityMedia\Ghostscript\Ghostscript;
20
use GravityMedia\Ghostscript\Process\Argument;
21
use GravityMedia\Ghostscript\Process\Arguments;
22
use Symfony\Component\Process\Process;
23
use Symfony\Component\Process\ProcessBuilder;
24
25
/**
26
 * The abstract device class
27
 *
28
 * @package GravityMedia\Ghostscript\Devices
29
 */
30
abstract class AbstractDevice
31
{
32
    /**
33
     * Use command line options
34
     */
35
    use CommandLineParametersTrait;
36
37
    /**
38
     * Use rendering parameters
39
     */
40
    use RenderingTrait;
41
42
    /**
43
     * Use page parameters
44
     */
45
    use PageTrait;
46
47
    /**
48
     * Use font-related parameters
49
     */
50
    use FontTrait;
51
52
    /**
53
     * Use resource-related parameters
54
     */
55
    use ResourceTrait;
56
57
    /**
58
     * Use interaction parameters
59
     */
60
    use InteractionTrait;
61
62
    /**
63
     * Use device and output selection parameters
64
     */
65
    use OutputSelectionTrait;
66
67
    /**
68
     * Use EPS parameters
69
     */
70
    use EpsTrait;
71
72
    /**
73
     * Use ICC color parameters
74
     */
75
    use IccColorTrait;
76
77
    /**
78
     * Use other parameters
79
     */
80
    use OtherTrait;
81
82
    /**
83
     * PostScript commands to be executed via command line when using this device.
84
     */
85
    const POSTSCRIPT_COMMANDS = '';
86
87
    /**
88
     * The Ghostscript object
89
     *
90
     * @var Ghostscript
91
     */
92
    protected $ghostscript;
93
94
    /**
95
     * The arguments object
96
     *
97
     * @var Arguments
98
     */
99
    private $arguments;
100
101
    /**
102
     * List of input files
103
     *
104
     * @var array
105
     */
106
    private $inputFiles = [];
107
108
    /**
109
     * Whether to read input from stdin
110
     *
111
     * @var bool
112
     */
113
    private $inputStdin = false;
114
115
    /**
116
     * Create abstract device object
117
     *
118
     * @param Ghostscript      $ghostscript
119
     * @param Arguments $arguments
120
     */
121 30
    public function __construct(Ghostscript $ghostscript, Arguments $arguments)
122
    {
123 30
        $this->ghostscript = $ghostscript;
124 30
        $this->arguments = $arguments;
125 30
    }
126
127
    /**
128
     * Get Argument
129
     *
130
     * @param string $name
131
     *
132
     * @return null|Argument
133
     */
134 6
    protected function getArgument($name)
135
    {
136 6
        return $this->arguments->getArgument($name);
137
    }
138
139
    /**
140
     * Whether argument is set
141
     *
142
     * @param string $name
143
     *
144
     * @return bool
145
     */
146 2
    protected function hasArgument($name)
147
    {
148 2
        return $this->getArgument($name) !== null;
149
    }
150
151
    /**
152
     * Get argument value
153
     *
154
     * @param string $name
155
     *
156
     * @return null|string
157
     */
158 2
    protected function getArgumentValue($name)
159
    {
160 2
        $argument = $this->getArgument($name);
161 2
        if (null === $argument) {
162 2
            return null;
163
        }
164
165 2
        return $argument->getValue();
166
    }
167
168
    /**
169
     * Set argument
170
     *
171
     * @param string $argument
172
     *
173
     * @return $this
174
     */
175 6
    protected function setArgument($argument)
176
    {
177 6
        $this->arguments->setArgument($argument);
178
179 6
        return $this;
180
    }
181
182
    /**
183
     * Set a generic command line parameter with a string value
184
     *
185
     * @param string $param the parameter name
186
     * @param string $value the parameter value
187
     *
188
     * @return $this
189
     */
190 2
    public function setStringParameter($param, $value)
191
    {
192 2
        $this->setArgument(sprintf('-s%s=%s', $param, $value));
193
194 2
        return $this;
195
    }
196
197
    /**
198
     * Set a generic command line parameter with a token value
199
     *
200
     * @param string $param the parameter name
201
     * @param mixed  $value the parameter value
202
     *
203
     * @return $this
204
     */
205 2
    public function setTokenParameter($param, $value)
206
    {
207 2
        $this->setArgument(sprintf('-d%s=%s', $param, $value));
208
209 2
        return $this;
210
    }
211
212
    /**
213
     * Add an input file
214
     *
215
     * @param string $inputFile a path to an existing file
216
     *
217
     * @throws \RuntimeException if $inputFile does not exist
218
     *
219
     * @return $this
220
     */
221 14
    public function addInputFile($inputFile)
222
    {
223 14
        if (!is_file($inputFile)) {
224 4
            throw new \RuntimeException('Input file does not exist');
225
        }
226 10
        $this->inputFiles[] = $inputFile;
227
228 10
        return $this;
229
    }
230
231
    /**
232
     * Add an stdin as input file
233
     *
234
     * @return $this
235
     */
236 8
    public function addInputStdin()
237
    {
238 8
        $this->inputStdin = true;
239
240 8
        return $this;
241
    }
242
243
    /**
244
     * Create process object
245
     *
246
     * @param string $inputFile either a path to an existing file or a dash (-) to read input from stdin
247
     *
248
     * @throws \RuntimeException if $inputFile does not exist
249
     *
250
     * @return Process
251
     */
252 16
    public function createProcess($inputFile = null)
253
    {
254 16
        if ('-' == $inputFile) {
255 2
            $this->addInputStdin();
256 15
        } elseif (null !== $inputFile) {
257 6
            $this->addInputFile($inputFile);
258 2
        }
259
260 14
        $arguments = array_values($this->arguments->toArray());
261 14
        array_push($arguments, '-c', static::POSTSCRIPT_COMMANDS, '-f');
262 14
        if (count($this->inputFiles)) {
263 10
            $arguments = array_merge($arguments, $this->inputFiles);
264 5
        }
265 14
        if ($this->inputStdin) {
266 8
            array_push($arguments, '-');
267 4
        }
268 14
        $this->resetInput();
269
270 14
        $processBuilder = new ProcessBuilder($arguments);
271 14
        $processBuilder->setPrefix($this->ghostscript->getOption('bin', Ghostscript::DEFAULT_BINARY));
272 14
        $processBuilder->addEnvironmentVariables($this->ghostscript->getOption('env', []));
273 14
        $processBuilder->setTimeout($this->ghostscript->getOption('timeout', 60));
274
275 14
        return $processBuilder->getProcess();
276
    }
277
278
    /**
279
     * Reset the input-related fields of this device.
280
     * Future processes created from this device will have their own input parameters.
281
     */
282 14
    private function resetInput()
283
    {
284 14
        $this->inputFiles = [];
285 14
        $this->inputStdin = false;
286 14
    }
287
}
288