Passed
Push — main ( bb891e...7e6e75 )
by Siad
09:02
created

PDOSQLExecFormatterElement   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 334
Duplicated Lines 0 %

Test Coverage

Coverage 69.51%

Importance

Changes 0
Metric Value
wmc 28
eloc 68
c 0
b 0
f 0
dl 0
loc 334
rs 10
ccs 57
cts 82
cp 0.6951

19 Methods

Rating   Name   Duplication   Size   Complexity  
A getOutfile() 0 3 1
A setAppend() 0 3 1
A getDefaultOutput() 0 3 1
A setColdelim() 0 3 1
A createParam() 0 5 1
B prepare() 0 34 6
A setOutfile() 0 3 1
A __construct() 0 3 1
A setEncoding() 0 3 1
A getAppend() 0 3 1
A setType() 0 9 3
A setUseFile() 0 3 1
A getUseFile() 0 3 1
A setShowheaders() 0 3 1
A setClassName() 0 4 1
A setRowdelim() 0 3 1
A getFormatter() 0 3 1
A setFormatOutput() 0 3 1
A getOutputWriter() 0 12 3
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
     * Whether to format XML output.
80
     *
81
     * @var bool
82
     */
83
    private $formatoutput = true;
84
85
    /**
86
     * Encoding for XML output.
87
     *
88
     * @var string
89
     */
90
    private $encoding;
91
92
    /**
93
     * Column delimiter.
94
     * Defaults to ','.
95
     *
96
     * @var string
97
     */
98
    private $coldelimiter = ',';
99
100
    /**
101
     * Row delimiter.
102
     * Defaults to PHP_EOL.
103
     *
104
     * @var string
105
     */
106
    private $rowdelimiter = PHP_EOL;
107
108
    /**
109
     * Append to an existing file or overwrite it?
110
     *
111
     * @var bool
112
     */
113
    private $append = false;
114
115
    /**
116
     * Parameters for a custom formatter.
117
     *
118
     * @var array Parameter[]
119
     */
120
    private $formatterParams = [];
121
122
    /**
123
     * @var PDOSQLExecTask
124
     */
125
    private $parentTask;
126
127
    /**
128
     * @var Parameter[]
129
     */
130
    private $parameters = [];
131
132
    /**
133
     * @var bool
134
     */
135
    private $formatOutput;
136
137
    /**
138
     * Construct a new PDOSQLExecFormatterElement with parent task.
139
     */
140 2
    public function __construct(PDOSQLExecTask $parentTask)
141
    {
142 2
        $this->parentTask = $parentTask;
143 2
    }
144
145
    /**
146
     * Supports nested <param> element (for custom formatter classes).
147
     *
148
     * @return Parameter
149
     */
150 1
    public function createParam(): Parameter
151
    {
152 1
        $num = array_push($this->parameters, new Parameter());
153
154 1
        return $this->parameters[$num - 1];
155
    }
156
157
    /**
158
     * Configures wrapped formatter class with any attributes on this element.
159
     *
160
     * @throws BuildException
161
     */
162 2
    public function prepare(Location $location): void
163
    {
164 2
        if (!$this->formatter) {
165
            throw new BuildException('No formatter specified (use type or classname attribute)', $location);
166
        }
167
168 2
        $out = $this->getOutputWriter();
169
170 2
        $this->parentTask->log('Setting output writer to: ' . get_class($out), Project::MSG_VERBOSE);
171 2
        $this->formatter->setOutput($out);
172
173 2
        if ($this->formatter instanceof PlainPDOResultFormatter) {
174
            // set any options that apply to the plain formatter
175 1
            $this->formatter->setShowheaders($this->showheaders);
176 1
            $this->formatter->setRowdelim($this->rowdelimiter);
177 1
            $this->formatter->setColdelim($this->coldelimiter);
178 1
        } elseif ($this->formatter instanceof XMLPDOResultFormatter) {
179
            // set any options that apply to the xml formatter
180 1
            $this->formatter->setEncoding($this->encoding);
181 1
            $this->formatter->setFormatOutput($this->formatoutput);
182
        }
183
184 2
        foreach ($this->formatterParams as $param) {
185
            $param = new Parameter();
186
            $method = 'set' . $param->getName();
187
            if (!method_exists($this->formatter, $param->getName())) {
188
                throw new BuildException(
189
                    'Formatter ' . get_class(
190
                        $this->formatter
191
                    ) . " does not have a {$method} method.",
192
                    $location
193
                );
194
            }
195
            call_user_func([$this->formatter, $method], $param->getValue());
196
        }
197 2
    }
198
199
    /**
200
     * Sets the formatter type.
201
     *
202
     * @param string $type
203
     *
204
     * @throws BuildException
205
     */
206 2
    public function setType(string $type): void
207
    {
208 2
        $this->type = $type;
209 2
        if ('xml' === $this->type) {
210 1
            $this->formatter = new XMLPDOResultFormatter();
211 1
        } elseif ('plain' === $this->type) {
212 1
            $this->formatter = new PlainPDOResultFormatter();
213
        } else {
214
            throw new BuildException("Formatter '" . $this->type . "' not implemented");
215
        }
216 2
    }
217
218
    /**
219
     * Set classname for a custom formatter (must extend PDOResultFormatter).
220
     *
221
     * @param string $className
222
     */
223
    public function setClassName(string $className): void
224
    {
225
        $classNameNoDot = Phing::import($className);
226
        $this->formatter = new $classNameNoDot();
227
    }
228
229
    /**
230
     * Set whether to write formatter results to file.
231
     *
232
     * @param bool $useFile
233
     */
234
    public function setUseFile(bool $useFile): void
235
    {
236
        $this->useFile = $useFile;
237
    }
238
239
    /**
240
     * Return whether to write formatter results to file.
241
     *
242
     * @return bool
243
     */
244
    public function getUseFile(): bool
245
    {
246
        return $this->useFile;
247
    }
248
249
    /**
250
     * Sets the output file for the formatter results.
251
     *
252
     * @param File $outfile
253
     */
254 2
    public function setOutfile(File $outfile): void
255
    {
256 2
        $this->outfile = $outfile;
257 2
    }
258
259
    /**
260
     * Get the output file.
261
     *
262
     * @return File|null
263
     */
264 2
    public function getOutfile(): ?File
265
    {
266 2
        return $this->outfile;
267
    }
268
269
    /**
270
     * whether output should be appended to or overwrite
271
     * an existing file.  Defaults to false.
272
     *
273
     * @param bool $append
274
     */
275 1
    public function setAppend(bool $append): void
276
    {
277 1
        $this->append = (bool) $append;
278 1
    }
279
280
    /**
281
     * Whether output should be appended to file.
282
     *
283
     * @return bool
284
     */
285
    public function getAppend(): bool
286
    {
287
        return $this->append;
288
    }
289
290
    /**
291
     * Print headers for result sets from the
292
     * statements; optional, default true.
293
     *
294
     * @param bool $showheaders
295
     */
296 2
    public function setShowheaders($showheaders): void
297
    {
298 2
        $this->showheaders = (bool) $showheaders;
299 2
    }
300
301
    /**
302
     * Sets the column delimiter.
303
     *
304
     * @param string $v
305
     */
306 1
    public function setColdelim($v): void
307
    {
308 1
        $this->coldelimiter = $v;
309 1
    }
310
311
    /**
312
     * Sets the row delimiter.
313
     *
314
     * @param string $v
315
     */
316 1
    public function setRowdelim($v): void
317
    {
318 1
        $this->rowdelimiter = $v;
319 1
    }
320
321
    /**
322
     * Set the DOM document encoding.
323
     *
324
     * @param string $v
325
     */
326 2
    public function setEncoding($v): void
327
    {
328 2
        $this->encoding = $v;
329 2
    }
330
331
    /**
332
     * @param bool $v
333
     */
334 2
    public function setFormatOutput($v): void
335
    {
336 2
        $this->formatOutput = (bool) $v;
337 2
    }
338
339
    /**
340
     * Gets the formatter that has been configured based on this element.
341
     *
342
     * @return PDOResultFormatter
343
     */
344 2
    public function getFormatter(): ?PDOResultFormatter
345
    {
346 2
        return $this->formatter;
347
    }
348
349
    /**
350
     * Gets a configured output writer.
351
     *
352
     * @return Writer
353
     * @throws IOException
354
     */
355 2
    private function getOutputWriter()
356
    {
357 2
        if ($this->useFile) {
358 2
            $of = $this->getOutfile();
359 2
            if (!$of) {
0 ignored issues
show
introduced by
$of is of type Phing\Io\File, thus it always evaluated to true.
Loading history...
360
                $of = new File($this->formatter->getPreferredOutfile());
361
            }
362
363 2
            return new FileWriter($of, $this->append);
364
        }
365
366
        return $this->getDefaultOutput();
367
    }
368
369
    /**
370
     * Gets a default output writer for this task.
371
     *
372
     * @return Writer
373
     */
374
    private function getDefaultOutput()
375
    {
376
        return new LogWriter($this->parentTask);
377
    }
378
}
379