Completed
Push — master ( d90c8b...86899b )
by Siad
16:39
created

FileOutputStream::__construct()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.0073

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 9
nop 2
dl 0
loc 21
ccs 14
cts 15
cp 0.9333
crap 5.0073
rs 9.4222
c 0
b 0
f 0
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
/**
21
 * Output stream subclass for file streams.
22
 *
23
 * @package phing.system.io
24
 */
25
class FileOutputStream extends OutputStream
26
{
27
28
    /**
29
     * @var PhingFile The associated file.
30
     */
31
    protected $file;
32
33
    /**
34
     * Construct a new FileOutputStream.
35
     *
36
     * @param  mixed $file
37
     * @param  boolean $append Whether to append bytes to end of file rather than beginning.
38
     * @throws Exception   - if invalid argument specified.
39
     * @throws IOException - if unable to open file.
40
     */
41 44
    public function __construct($file, $append = false)
42
    {
43 44
        if ($file instanceof PhingFile) {
44 43
            $this->file = $file;
45 1
        } elseif (is_string($file)) {
46 1
            $this->file = new PhingFile($file);
47
        } else {
48
            throw new Exception("Invalid argument type for \$file.");
49
        }
50 44
        error_clear_last();
51 44
        if ($append) {
52 11
            $stream = @fopen($this->file->getAbsolutePath(), "ab");
53
        } else {
54 33
            $stream = @fopen($this->file->getAbsolutePath(), "wb");
55
        }
56 44
        if ($stream === false) {
57 1
            $lastError = error_get_last();
58 1
            $errormsg = $lastError['message'];
59 1
            throw new IOException("Unable to open " . $this->file->__toString() . " for writing: " . $errormsg);
60
        }
61 43
        parent::__construct($stream);
62 43
    }
63
64
    /**
65
     * Returns a string representation of the attached file.
66
     *
67
     * @return string
68
     */
69
    public function __toString()
70
    {
71
        return $this->file->getPath();
72
    }
73
}
74