Passed
Push — main ( c100f9...14c5bb )
by Siad
07:13
created

PDOSQLExecFormatterElement::setShowtrailers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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