Completed
Push — master ( 92d415...d91ee9 )
by Daniel
03:22
created

AbstractDevice::createProcessArguments()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
ccs 17
cts 17
cp 1
rs 8.439
cc 6
eloc 13
nc 10
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\Ghostscript;
11
use GravityMedia\Ghostscript\Input;
12
use GravityMedia\Ghostscript\Process\Argument;
13
use GravityMedia\Ghostscript\Process\Arguments;
14
use Symfony\Component\Process\Process;
15
use Symfony\Component\Process\ProcessBuilder;
16
17
/**
18
 * The abstract device class.
19
 *
20
 * @package GravityMedia\Ghostscript\Devices
21
 */
22
abstract class AbstractDevice
23
{
24
    /**
25
     * Use command line options.
26
     */
27
    use CommandLineParametersTrait;
28
29
    /**
30
     * Use rendering parameters.
31
     */
32
    use CommandLineParameters\RenderingTrait;
33
34
    /**
35
     * Use page parameters.
36
     */
37
    use CommandLineParameters\PageTrait;
38
39
    /**
40
     * Use font-related parameters.
41
     */
42
    use CommandLineParameters\FontTrait;
43
44
    /**
45
     * Use resource-related parameters.
46
     */
47
    use CommandLineParameters\ResourceTrait;
48
49
    /**
50
     * Use interaction parameters.
51
     */
52
    use CommandLineParameters\InteractionTrait;
53
54
    /**
55
     * Use device and output selection parameters.
56
     */
57
    use CommandLineParameters\OutputSelectionTrait;
58
59
    /**
60
     * Use EPS parameters.
61
     */
62
    use CommandLineParameters\EpsTrait;
63
64
    /**
65
     * Use ICC color parameters.
66
     */
67
    use CommandLineParameters\IccColorTrait;
68
69
    /**
70
     * Use other parameters.
71
     */
72
    use CommandLineParameters\OtherTrait;
73
74
    /**
75
     * The Ghostscript object.
76
     *
77
     * @var Ghostscript
78
     */
79
    private $ghostscript;
80
81
    /**
82
     * The arguments object.
83
     *
84
     * @var Arguments
85
     */
86
    private $arguments;
87
88
    /**
89
     * Create abstract device object.
90
     *
91
     * @param Ghostscript $ghostscript
92
     * @param Arguments   $arguments
93
     */
94 20
    public function __construct(Ghostscript $ghostscript, Arguments $arguments)
95
    {
96 20
        $this->ghostscript = $ghostscript;
97 20
        $this->arguments = $arguments;
98 20
    }
99
100
    /**
101
     * Get argument.
102
     *
103
     * @param string $name
104
     *
105
     * @return null|Argument
106
     */
107 6
    protected function getArgument($name)
108
    {
109 6
        return $this->arguments->getArgument($name);
110
    }
111
112
    /**
113
     * Whether argument is set.
114
     *
115
     * @param string $name
116
     *
117
     * @return bool
118
     */
119 2
    protected function hasArgument($name)
120
    {
121 2
        return null !== $this->getArgument($name);
122
    }
123
124
    /**
125
     * Get argument value.
126
     *
127
     * @param string $name
128
     *
129
     * @return null|string
130
     */
131 2
    protected function getArgumentValue($name)
132
    {
133 2
        $argument = $this->getArgument($name);
134 2
        if (null === $argument) {
135 2
            return null;
136
        }
137
138 2
        return $argument->getValue();
139
    }
140
141
    /**
142
     * Set argument.
143
     *
144
     * @param string $argument
145
     *
146
     * @return $this
147
     */
148 2
    protected function setArgument($argument)
149
    {
150 2
        $this->arguments->setArgument($argument);
151
152 2
        return $this;
153
    }
154
155
    /**
156
     * Sanitize input.
157
     *
158
     * @param null|string|resource|Input $input
159
     *
160
     * @return Input
161
     */
162 12
    protected function sanitizeInput($input)
163
    {
164 12
        if (null === $input) {
165 2
            $input = $this->ghostscript->getOption('input', new Input());
166 1
        }
167
168 12
        if ($input instanceof Input) {
169 6
            return $input;
170
        }
171
172 6
        $instance = new Input();
173
174 6
        if (is_resource($input)) {
175 2
            return $instance->setProcessInput($input);
176
        }
177
178 4
        if (file_exists($input)) {
179 2
            return $instance->addFile($input);
180
        }
181
182 2
        return $instance->setPostScriptCode((string)$input);
183
    }
184
185
    /**
186
     * Create process arguments.
187
     *
188
     * @param Input $input
189
     *
190
     * @throws \RuntimeException
191
     *
192
     * @return array
193
     */
194 12
    protected function createProcessArguments(Input $input)
195
    {
196 12
        $arguments = array_values($this->arguments->toArray());
197
198 12
        if (null !== $input->getPostScriptCode()) {
199 4
            array_push($arguments, '-c', $input->getPostScriptCode());
200 2
        }
201
202 12
        if (count($input->getFiles()) > 0) {
203 6
            array_push($arguments, '-f');
204 6
            foreach ($input->getFiles() as $file) {
205 6
                if (!is_file($file)) {
206 2
                    throw new \RuntimeException('Input file does not exist');
207
                }
208
209 4
                array_push($arguments, $file);
210 2
            }
211 2
        }
212
213 10
        if (null !== $input->getProcessInput()) {
214 4
            array_push($arguments, '-');
215 2
        }
216
217 10
        return $arguments;
218
    }
219
220
    /**
221
     * Create process builder.
222
     *
223
     * @param array $arguments
224
     * @param Input $input
225
     *
226
     * @return ProcessBuilder
227
     */
228 10
    protected function createProcessBuilder(array $arguments, Input $input)
229
    {
230 10
        $processBuilder = ProcessBuilder::create($arguments);
231 10
        $processBuilder->setPrefix($this->ghostscript->getOption('bin', Ghostscript::DEFAULT_BINARY));
232 10
        $processBuilder->setWorkingDirectory($this->ghostscript->getOption('cwd'));
233 10
        $processBuilder->addEnvironmentVariables($this->ghostscript->getOption('env', []));
234 10
        $processBuilder->setTimeout($this->ghostscript->getOption('timeout', 60));
235 10
        $processBuilder->setInput($input->getProcessInput());
236
237 10
        return $processBuilder;
238
    }
239
240
    /**
241
     * Create process object.
242
     *
243
     * @param null|string|resource|Input $input
244
     *
245
     * @throws \RuntimeException
246
     *
247
     * @return Process
248
     */
249 12
    public function createProcess($input = null)
250
    {
251 12
        $input = $this->sanitizeInput($input);
252
253 12
        $arguments = $this->createProcessArguments($input);
254
255 10
        return $this->createProcessBuilder($arguments, $input)->getProcess();
256
    }
257
}
258