Passed
Push — main ( ddd1a4...b98cb8 )
by Michiel
08:47
created

PDOSQLExecFormatterElement::getAppend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
namespace Phing\Task\System\Pdo;
21
22
use Phing\Exception\BuildException;
23
use Phing\Io\FileWriter;
24
use Phing\Io\LogWriter;
25
use Phing\Io\File;
26
use Phing\Io\Writer;
27
use Phing\Parser\Location;
28
use Phing\Phing;
29
use Phing\Project;
30
use Phing\Type\Parameter;
31
32
/**
33
 * A class to represent the nested <formatter> element for PDO SQL results.
34
 *
35
 * This class is inspired by the similarly-named class in the PHPUnit tasks.
36
 *
37
 * @author  Hans Lellelid <[email protected]>
38
 * @package phing.tasks.ext.pdo
39
 * @since   2.3.0
40
 */
41
class PDOSQLExecFormatterElement
42
{
43
    /**
44
     * @var PDOResultFormatter
45
     */
46
    private $formatter;
47
48
    /**
49
     * The type of the formatter (used for built-in formatter classes).
50
     *
51
     * @var string
52
     */
53
    private $type = "";
54
55
    /**
56
     * Whether to use file (or write output to phing log).
57
     *
58
     * @var boolean
59
     */
60
    private $useFile = true;
61
62
    /**
63
     * Output file for formatter.
64
     *
65
     * @var File
66
     */
67
    private $outfile;
68
69
    /**
70
     * Print header columns.
71
     *
72
     * @var boolean
73
     */
74
    private $showheaders = true;
75
76
    /**
77
     * Whether to format XML output.
78
     *
79
     * @var boolean
80
     */
81
    private $formatoutput = true;
82
83
    /**
84
     * Encoding for XML output.
85
     *
86
     * @var string
87
     */
88
    private $encoding;
89
90
    /**
91
     * Column delimiter.
92
     * Defaults to ','
93
     *
94
     * @var string
95
     */
96
    private $coldelimiter = ",";
97
98
    /**
99
     * Row delimiter.
100
     * Defaults to PHP_EOL.
101
     *
102
     * @var string
103
     */
104
    private $rowdelimiter = PHP_EOL;
105
106
    /**
107
     * Append to an existing file or overwrite it?
108
     *
109
     * @var boolean
110
     */
111
    private $append = false;
112
113
    /**
114
     * Parameters for a custom formatter.
115
     *
116
     * @var array Parameter[]
117
     */
118
    private $formatterParams = [];
119
120
    /**
121
     * @var PDOSQLExecTask
122
     */
123
    private $parentTask;
124
125
    /**
126
     * @var Parameter[]
127
     */
128
    private $parameters;
129
130
    /**
131
     * @var bool
132
     */
133
    private $formatOutput;
134
135
    /**
136
     * Construct a new PDOSQLExecFormatterElement with parent task.
137
     *
138
     * @param PDOSQLExecTask $parentTask
139
     */
140
    public function __construct(PDOSQLExecTask $parentTask)
141
    {
142
        $this->parentTask = $parentTask;
143
    }
144
145
    /**
146
     * Supports nested <param> element (for custom formatter classes).
147
     *
148
     * @return Parameter
149
     */
150
    public function createParam()
151
    {
152
        $num = array_push($this->parameters, new Parameter());
153
154
        return $this->parameters[$num - 1];
155
    }
156
157
    /**
158
     * Gets a configured output writer.
159
     *
160
     * @return Writer
161
     */
162
    private function getOutputWriter()
163
    {
164
        if ($this->useFile) {
165
            $of = $this->getOutfile();
166
            if (!$of) {
0 ignored issues
show
introduced by
$of is of type Phing\Io\File, thus it always evaluated to true.
Loading history...
167
                $of = new File($this->formatter->getPreferredOutfile());
168
            }
169
170
            return new FileWriter($of, $this->append);
171
        }
172
173
        return $this->getDefaultOutput();
174
    }
175
176
    /**
177
     * Configures wrapped formatter class with any attributes on this element.
178
     *
179
     * @throws BuildException
180
     */
181
    public function prepare(Location $location)
182
    {
183
        if (!$this->formatter) {
184
            throw new BuildException("No formatter specified (use type or classname attribute)", $location);
185
        }
186
187
        $out = $this->getOutputWriter();
188
189
        $this->parentTask->log("Setting output writer to: " . get_class($out), Project::MSG_VERBOSE);
190
        $this->formatter->setOutput($out);
191
192
        if ($this->formatter instanceof PlainPDOResultFormatter) {
193
            // set any options that apply to the plain formatter
194
            $this->formatter->setShowheaders($this->showheaders);
195
            $this->formatter->setRowdelim($this->rowdelimiter);
196
            $this->formatter->setColdelim($this->coldelimiter);
197
        } elseif ($this->formatter instanceof XMLPDOResultFormatter) {
198
            // set any options that apply to the xml formatter
199
            $this->formatter->setEncoding($this->encoding);
200
            $this->formatter->setFormatOutput($this->formatoutput);
201
        }
202
203
        foreach ($this->formatterParams as $param) {
204
            $param = new Parameter();
205
            $method = 'set' . $param->getName();
206
            if (!method_exists($this->formatter, $param->getName())) {
207
                throw new BuildException(
208
                    "Formatter " . get_class(
209
                        $this->formatter
210
                    ) . " does not have a $method method.",
211
                    $location
212
                );
213
            }
214
            call_user_func([$this->formatter, $method], $param->getValue());
215
        }
216
    }
217
218
    /**
219
     * Sets the formatter type.
220
     *
221
     * @param  string $type
222
     * @throws BuildException
223
     */
224
    public function setType($type)
225
    {
226
        $this->type = $type;
227
        if ($this->type === "xml") {
228
            $this->formatter = new XMLPDOResultFormatter();
229
        } elseif ($this->type === "plain") {
230
            $this->formatter = new PlainPDOResultFormatter();
231
        } else {
232
            throw new BuildException("Formatter '" . $this->type . "' not implemented");
233
        }
234
    }
235
236
    /**
237
     * Set classname for a custom formatter (must extend PDOResultFormatter).
238
     *
239
     * @param string $className
240
     */
241
    public function setClassName($className)
242
    {
243
        $classNameNoDot = Phing::import($className);
244
        $this->formatter = new $classNameNoDot();
245
    }
246
247
    /**
248
     * Set whether to write formatter results to file.
249
     *
250
     * @param boolean $useFile
251
     */
252
    public function setUseFile($useFile)
253
    {
254
        $this->useFile = (bool) $useFile;
255
    }
256
257
    /**
258
     * Return whether to write formatter results to file.
259
     *
260
     * @return boolean
261
     */
262
    public function getUseFile()
263
    {
264
        return $this->useFile;
265
    }
266
267
    /**
268
     * Sets the output file for the formatter results.
269
     *
270
     * @param    File $outfile
271
     * @internal param PhingFile $outFile
272
     */
273
    public function setOutfile(File $outfile)
274
    {
275
        $this->outfile = $outfile;
276
    }
277
278
    /**
279
     * Get the output file.
280
     *
281
     * @return File
282
     */
283
    public function getOutfile()
284
    {
285
        return $this->outfile;
286
    }
287
288
    /**
289
     * whether output should be appended to or overwrite
290
     * an existing file.  Defaults to false.
291
     *
292
     * @param boolean $append
293
     */
294
    public function setAppend($append)
295
    {
296
        $this->append = (bool) $append;
297
    }
298
299
    /**
300
     * Whether output should be appended to file.
301
     *
302
     * @return boolean
303
     */
304
    public function getAppend()
305
    {
306
        return $this->append;
307
    }
308
309
    /**
310
     * Print headers for result sets from the
311
     * statements; optional, default true.
312
     *
313
     * @param boolean $showheaders
314
     */
315
    public function setShowheaders($showheaders)
316
    {
317
        $this->showheaders = (bool) $showheaders;
318
    }
319
320
    /**
321
     * Sets the column delimiter.
322
     *
323
     * @param string $v
324
     */
325
    public function setColdelim($v)
326
    {
327
        $this->coldelimiter = $v;
328
    }
329
330
    /**
331
     * Sets the row delimiter.
332
     *
333
     * @param string $v
334
     */
335
    public function setRowdelim($v)
336
    {
337
        $this->rowdelimiter = $v;
338
    }
339
340
    /**
341
     * Set the DOM document encoding.
342
     *
343
     * @param string $v
344
     */
345
    public function setEncoding($v)
346
    {
347
        $this->encoding = $v;
348
    }
349
350
    /**
351
     * @param boolean $v
352
     */
353
    public function setFormatOutput($v)
354
    {
355
        $this->formatOutput = (bool) $v;
356
    }
357
358
    /**
359
     * Gets a default output writer for this task.
360
     *
361
     * @return Writer
362
     */
363
    private function getDefaultOutput()
364
    {
365
        return new LogWriter($this->parentTask);
366
    }
367
368
    /**
369
     * Gets the formatter that has been configured based on this element.
370
     *
371
     * @return PDOResultFormatter
372
     */
373
    public function getFormatter()
374
    {
375
        return $this->formatter;
376
    }
377
}
378