Completed
Pull Request — 5.0 (#66)
by
unknown
14:59 queued 13:33
created

FileHandler   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 64.1%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 0
dl 0
loc 89
ccs 25
cts 39
cp 0.641
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 4
A getNextLine() 0 4 1
A ended() 0 4 1
A close() 0 4 1
C save() 0 32 7
1
<?php namespace Sepia\PoParser\Handler;
2
3
/**
4
 *    Copyright (c) 2012 Raúl Ferràs [email protected]
5
 *    All rights reserved.
6
 *
7
 *    Redistribution and use in source and binary forms, with or without
8
 *    modification, are permitted provided that the following conditions
9
 *    are met:
10
 *    1. Redistributions of source code must retain the above copyright
11
 *       notice, this list of conditions and the following disclaimer.
12
 *    2. Redistributions in binary form must reproduce the above copyright
13
 *       notice, this list of conditions and the following disclaimer in the
14
 *       documentation and/or other materials provided with the distribution.
15
 *    3. Neither the name of copyright holders nor the names of its
16
 *       contributors may be used to endorse or promote products derived
17
 *       from this software without specific prior written permission.
18
 *
19
 *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
 *    ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21
 *    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22
 *    PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
23
 *    BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
 *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
 *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
 *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
 *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
 *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
 *    POSSIBILITY OF SUCH DAMAGE.
30
 *
31
 * https://github.com/raulferras/PHP-po-parser
32
 */
33
class FileHandler implements HandlerInterface
34
{
35
    /**
36
     * @var resource
37
     */
38
    protected $fileHandle;
39
40
    /**
41
     * @param string $filepath
42
     *
43
     * @throws \Exception
44
     */
45 39
    public function __construct($filepath)
46
    {
47 39
        if (file_exists($filepath) === false) {
48
            throw new \Exception('PoParser: Input File does not exists: "' . htmlspecialchars($filepath) . '"');
49 39
        } elseif (is_readable($filepath) === false) {
50
            throw new \Exception('PoParser: File is not readable: "' . htmlspecialchars($filepath) . '"');
51
        }
52
53 39
        $this->fileHandle = @fopen($filepath, "r");
54 39
        if ($this->fileHandle===false) {
55
            throw new \Exception('PoParser: Could not open file: "' . htmlspecialchars($filepath) . '"');
56
        }
57 39
    }
58
59
    /**
60
     * @return string
61
     */
62 39
    public function getNextLine()
63
    {
64 39
        return fgets($this->fileHandle);
65
    }
66
67
    /**
68
     * @return bool
69
     */
70 39
    public function ended()
71
    {
72 39
        return feof($this->fileHandle);
73
    }
74
75
    /**
76
     * @return bool
77
     */
78 39
    public function close()
79
    {
80 39
        return @fclose($this->fileHandle);
81
    }
82
83
    /**
84
     * @inheritdoc
85
     *
86
     * @param string $output
87
     * @param array  $params
88
     */
89 15
    public function save($output, $params)
90
    {
91 15
        $outputFilePath = isset($params['filepath']) ? $params['filepath'] : null;
92
93 15
        if ($outputFilePath) {
94 15
            $fileHandle = @fopen($params['filepath'], 'w');
95 15
            if ($fileHandle === false) {
96
                throw new \RuntimeException(
97
                    sprintf(
98
                        'Could not open filename "%s" for writing.',
99 10
                        $params['filepath']
100
                    )
101
                );
102
            }
103 5
        } else {
104
            $fileHandle = $this->fileHandle;
105
            if (is_resource($fileHandle) === false) {
106
                throw new \RuntimeException(
107
                    'No source file opened nor `filepath` parameter specified in FileHandler::save method.'
108
                );
109
            }
110
        }
111
112 15
        $bytesWritten = @fwrite($fileHandle, $output);
113 15
        if ($bytesWritten === false) {
114
            throw new \RuntimeException('Could not write data into file.');
115
        }
116
117 15
        if (isset($params['filepath'])) {
118 15
            @fclose($fileHandle);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
119 5
        }
120
    }
121
}