AbstractDevice   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 226
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 15
dl 0
loc 226
ccs 53
cts 53
cp 1
rs 10
c 0
b 0
f 0

8 Methods

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