Completed
Push — master ( 5a8dc8...611434 )
by Daniel
12:32
created

Ghostscript::createNullDevice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 2
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;
9
10
use GravityMedia\Ghostscript\Device\BoundingBoxInfo;
11
use GravityMedia\Ghostscript\Device\NoDisplay;
12
use GravityMedia\Ghostscript\Device\PdfInfo;
13
use GravityMedia\Ghostscript\Device\PdfWrite;
14
use GravityMedia\Ghostscript\Process\Arguments;
15
use Symfony\Component\Process\Process;
16
17
/**
18
 * The Ghostscript class.
19
 *
20
 * @package GravityMedia\Ghostscript
21
 */
22
class Ghostscript
23
{
24
    /**
25
     * The default binary
26
     */
27
    const DEFAULT_BINARY = 'gs';
28
29
    /**
30
     * The versions.
31
     *
32
     * @var string[]
33
     */
34
    protected static $versions = [];
35
36
    /**
37
     * The options.
38
     *
39
     * @var array
40
     */
41
    protected $options;
42
43
    /**
44
     * Create Ghostscript object.
45
     *
46
     * @param array $options
47 27
     *
48
     * @throws \RuntimeException
49 27
     */
50
    public function __construct(array $options = [])
51 27
    {
52 3
        $this->options = $options;
53
54 21
        if (version_compare('9.00', $this->getVersion()) > 0) {
55
            throw new \RuntimeException('Ghostscript version 9.00 or higher is required');
56
        }
57
    }
58
59
    /**
60
     * Get option.
61
     *
62
     * @param string $name
63
     * @param mixed  $default
64 24
     *
65
     * @return mixed
66 24
     */
67 6
    public function getOption($name, $default = null)
68
    {
69
        if (array_key_exists($name, $this->options)) {
70 24
            return $this->options[$name];
71
        }
72
73
        return $default;
74
    }
75
76
    /**
77
     * Get version.
78
     *
79
     * @throws \RuntimeException
80 24
     *
81
     * @return string
82 24
     */
83 24
    public function getVersion()
84 24
    {
85
        $binary = $this->getOption('bin', static::DEFAULT_BINARY);
86 24
87
        if (!isset(static::$versions[$binary])) {
88
            $process = new Process($binary . ' --version');
89
            $process->run();
90
91
            if (!$process->isSuccessful()) {
92
                throw new \RuntimeException($process->getErrorOutput());
93
            }
94
95
            static::$versions[$binary] = $process->getOutput();
96 24
        }
97
98 24
        return static::$versions[$binary];
99 24
    }
100 24
101
    /**
102 24
     * Create arguments object.
103 3
     *
104
     * @return Arguments
105
     */
106 21
    protected function createArguments()
107 21
    {
108
        $processArguments = new Arguments();
109 21
110
        if ($this->getOption('quiet', true)) {
111
            $processArguments->addArgument('-q');
112
        }
113
114
        return $processArguments;
115
    }
116
117
    /**
118
     * Create PDF device object.
119 6
     *
120
     * @param null|string $outputFile
121 6
     *
122 6
     * @return PdfWrite
123
     */
124 6
    public function createPdfDevice($outputFile = null)
125 6
    {
126 6
        $device = new PdfWrite($this, $this->createArguments());
127
        $device
128 6
            ->setSafer()
129
            ->setBatch()
130
            ->setNoPause();
131
132
        if (null !== $outputFile) {
133
            $device->setOutputFile($outputFile);
134
        }
135
136
        return $device;
137
    }
138 3
139
    /**
140 3
     * Create no display device object.
141 3
     *
142 3
     * @return NoDisplay
143 3
     */
144
    public function createNoDisplayDevice()
145 3
    {
146
        return new NoDisplay($this, $this->createArguments());
147 3
    }
148
149 3
    /**
150 3
     * Create PDF info device object.
151 3
     *
152
     * @param string $pdfInfoPath Path to toolbin/pdf_info.ps
153 3
     *
154
     * @return PdfInfo
155
     */
156
    public function createPdfInfoDevice($pdfInfoPath)
157
    {
158
        return new PdfInfo($this, $this->createArguments(), $pdfInfoPath);
159
    }
160
161
    /**
162
     * Create bounding box info device object.
163
     *
164
     * @return BoundingBoxInfo
165
     */
166
    public function createBoundingBoxInfoDevice()
167
    {
168
        $device = new BoundingBoxInfo($this, $this->createArguments());
169
        $device
170
            ->setSafer()
171
            ->setBatch()
172
            ->setNoPause();
173
174
        return $device;
175
    }
176
}
177