Fop   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 256
Duplicated Lines 14.06 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 31
lcom 1
cbo 4
dl 36
loc 256
rs 9.8
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A convertToPdf() 0 4 1
A convertToRtf() 0 4 1
A convert() 0 18 4
A get() 0 14 3
A out() 18 18 4
A callback() 18 18 4
B runProcess() 0 44 5
A getFopExecutable() 0 4 1
A getConfigurationFile() 0 4 1
A setFopExecutable() 0 9 2
A setConfigurationFile() 0 9 2
A getJavaExecutable() 0 4 1
A setJavaExecutable() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Goetas\ApacheFopBundle\Processor;
4
5
use Symfony\Component\Process\Process;
6
7
use Goetas\ApacheFopBundle\Input\FileInput;
8
use Goetas\ApacheFopBundle\Input\InputInterface;
9
10
/**
11
 *
12
 * @author goetas <http://www.goetas.com/>
13
 */
14
use Symfony\Component\Process\ProcessBuilder;
15
16
class Fop
17
{
18
    const OTUPUT_PDF = 'application/pdf';
19
    const OTUPUT_RTF = 'text/rtf';
20
21
    protected $fopExecutable;
22
    protected $javaExecutable;
23
    protected $configurationFile;
24
    public function __construct($fopExecutable)
25
    {
26
        $this->setFopExecutable ( $fopExecutable );
27
    }
28
    /**
29
     * Convert to PDF reading a FOP input and save the result into file
30
     * @param  InputInterface|string $source
31
     * @param  string                $xsl
32
     * @throws \RuntimeException
33
     * @deprecated
34
     */
35
    public function convertToPdf($source, $destination, $xsl = null)
36
    {
37
        return $this->convert ( $source, $destination, self::OTUPUT_PDF, $xsl );
38
    }
39
    /**
40
     * Convert to RTF reading a FOP input and save the result into file
41
     * @param  InputInterface|string $source
42
     * @param  string                $xsl
43
     * @throws \RuntimeException
44
     * @deprecated
45
     */
46
    public function convertToRtf($source, $destination, $xsl = null)
47
    {
48
        return $this->convert ( $source, $destination, self::OTUPUT_RTF, $xsl );
49
    }
50
    /**
51
     * Convert reading a FOP input, saving the result into file
52
     * @param  InputInterface|string $source
53
     * @param  string                $destination
54
     * @param  string|const          $outputFormat self::OTUPUT_PDF or self::OTUPUT_RTF or other supported mimes by Apache FOP
55
     * @param  InputInterface|string $xsl
56
     * @param  array                 $params
57
     * @throws \RuntimeException
58
     */
59
    public function convert($source, $destination, $outputFormat, $xsl = null, array $params = array())
60
    {
61
        if (is_string($source)) {
62
            $source = new FileInput($source);
63
        }
64
        if (is_string($xsl)) {
65
            $xsl = new FileInput($xsl);
66
        }
67
68
        $process = $this->runProcess($source, $destination, $outputFormat, $xsl, $params);
69
        $process->run();
70
        if (!$process->isSuccessful()) {
71
            $e = new \Exception ( "Apache FOP exception.\n" . $process->getErrorOutput() );
72
            throw new \RuntimeException ( "Can't generate the document", null, $e );
73
        }
74
75
        return true;
76
    }
77
78
    /**
79
     * Convert reading a FOP input, and get the result
80
     * @param  InputInterface        $source
81
     * @param  string                $outputFormat self::OTUPUT_PDF or self::OTUPUT_RTF or other supported mimes by Apache FOP
82
     * @param  InputInterface|string $xsl
83
     * @param  array                 $params
84
     * @return string
85
     * @throws \RuntimeException
86
     */
87
    public function get(InputInterface $source, $outputFormat, $xsl = null, array $params = array())
88
    {
89
        if (is_string($xsl)) {
90
            $xsl = new FileInput($xsl);
91
        }
92
        $process = $this->runProcess($source, "-", $outputFormat, $xsl, $params);
93
        $process->run();
94
        if (!$process->isSuccessful()) {
95
            $e = new \Exception ( "Apache FOP exception.\n" . $process->getErrorOutput() );
96
            throw new \RuntimeException ( "Can't generate the document", null, $e );
97
        }
98
99
        return $process->getOutput();
100
    }
101
    /**
102
     * Convert reading a FOP input, and flush to output the result
103
     * @param  InputInterface        $source
104
     * @param  string                $outputFormat self::OTUPUT_PDF or self::OTUPUT_RTF or other supported mimes by Apache FOP
105
     * @param  InputInterface|string $xsl
106
     * @param  array                 $params
107
     * @throws \RuntimeException
108
     */
109 View Code Duplication
    public function out(InputInterface $source, $outputFormat, $xsl = null, array $params = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        if (is_string($xsl)) {
112
            $xsl = new FileInput($xsl);
113
        }
114
        $process = $this->runProcess($source, "-", $outputFormat, $xsl, $params);
115
        $process->run(function ($type, $buffer) {
116
            if (Process::OUT === $type) {
117
                echo $buffer;
118
            }
119
        });
120
        if (!$process->isSuccessful()) {
121
            $e = new \Exception ( "Apache FOP exception.\n" . $process->getErrorOutput() );
122
            throw new \RuntimeException ( "Can't generate the document", null, $e );
123
        }
124
125
        return true;
126
    }
127
    /**
128
     * Convert reading a FOP input, and flush to output the result
129
     * @param  InputInterface    $source
130
     * @param  callback          $callback     will recieve the output
131
     * @param  string            $outputFormat self::OTUPUT_PDF or self::OTUPUT_RTF or other supported mimes by Apache FOP
132
     * @param  string            $xsl
133
     * @param  array             $params
134
     * @throws \RuntimeException
135
     */
136 View Code Duplication
    public function callback(InputInterface $source, $callback, $outputFormat, $xsl = null, array $params = array())
0 ignored issues
show
Unused Code introduced by
The parameter $callback is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137
    {
138
        if (is_string($xsl)) {
139
            $xsl = new FileInput($xsl);
140
        }
141
        $process = $this->runProcess($source, "-", $outputFormat, $xsl, $params);
142
        $process->run(function ($type, $buffer) {
143
            if (Process::OUT === $type) {
144
                $callback($buffer);
0 ignored issues
show
Bug introduced by
The variable $callback does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
145
            }
146
        });
147
        if (!$process->isSuccessful()) {
148
            $e = new \Exception ( "Apache FOP exception.\n" . $process->getErrorOutput() );
149
            throw new \RuntimeException ( "Can't generate the document", null, $e );
150
        }
151
152
        return true;
153
    }
154
155
    /**
156
     *
157
     *
158
     * @param  InputInterface                            $input Ony $input can use StringInput
159
     * @param  string                                    $destination  The place where save the result
160
     * @param  string                                    $outputFormat
161
     * @param  InputInterface                            $xsl
162
     * @param  array                                     $params
163
     * @return \Symfony\Component\Process\Process
164
     */
165
    protected function runProcess(InputInterface $input, $destination,  $outputFormat, InputInterface $xsl = null, array $params = array())
166
    {
167
        $builder = new ProcessBuilder ();
168
        $builder->add ( $this->fopExecutable );
169
170
        $builder->add ( "-q" );
171
        $builder->add ( "-r" );
172
173
        if ($xsl instanceof InputInterface) {
174
            $builder->add ( "-xml" );
175
            $input->buildParams($builder);
176
177
            $builder->add ( "-xsl" );
178
            $xsl->buildParams($builder);
179
        } else {
180
            $builder->add ( "-fo" );
181
            $input->buildParams($builder);
182
        }
183
184
        foreach ($params as $key => $value) {
185
            $builder->add ( "-param" );
186
            $builder->add ( $key );
187
            $builder->add ( $value );
188
        }
189
190
        $builder->add ( "-out" );
191
        $builder->add ( $outputFormat );
192
        $builder->add ( $destination );
193
194
        if ($this->configurationFile !== null) {
195
            $builder->add ( "-c" );
196
            $builder->add ( $this->configurationFile );
197
        }
198
199
        $proc = $builder->getProcess();
200
201
        $input->setInput($proc);
202
203
        if ($xsl instanceof InputInterface) {
204
            $xsl->setInput($proc);
205
        }
206
207
        return $proc;
208
    }
209
    /**
210
     * Get the path for FOP executable
211
     * @return string
212
     */
213
    public function getFopExecutable()
214
    {
215
        return $this->fopExecutable;
216
    }
217
    /**
218
     * Get the path of FOP config file
219
     * @return string
220
     */
221
    public function getConfigurationFile()
222
    {
223
        return $this->configurationFile;
224
    }
225
    /**
226
     * SET the path for FOP executable
227
     * @return self
228
     */
229
    public function setFopExecutable($fopExecutable)
230
    {
231
        if (! is_executable ( $fopExecutable )) {
232
            throw new \RuntimeException ( sprintf ( "Can't find %s command", $fopExecutable ) );
233
        }
234
        $this->fopExecutable = $fopExecutable;
235
236
        return $this;
237
    }
238
    /**
239
     * Set the path of FOP config file
240
     * @param  string $configurationFile
241
     * @return self
242
     */
243
    public function setConfigurationFile($configurationFile)
244
    {
245
        if (! is_readable ( $configurationFile )) {
246
            throw new \RuntimeException ( sprintf ( "Can't find configuration file named '%s'", $configurationFile ) );
247
        }
248
        $this->configurationFile = $configurationFile;
249
250
        return $this;
251
    }
252
    /**
253
     * Get the path for Java executable
254
     * @return string
255
     */
256
    public function getJavaExecutable()
257
    {
258
        return $this->javaExecutable;
259
    }
260
    /**
261
     * SET the path for Java executable
262
     * @param  string $javaExecutable
263
     * @return self
264
     */
265
    public function setJavaExecutable($javaExecutable)
266
    {
267
        $this->javaExecutable = $javaExecutable;
268
269
        return $this;
270
    }
271
}
272