1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file is part of the Lazzard/ftp-bridge package. |
||
5 | * |
||
6 | * (c) El Amrani Chakir <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | * |
||
11 | */ |
||
12 | |||
13 | namespace Lazzard\FtpBridge\Logger; |
||
14 | |||
15 | use Lazzard\FtpBridge\Exception\FileLoggerException; |
||
16 | |||
17 | /** |
||
18 | * FileLogger |
||
19 | * @since 1.0 |
||
20 | * @author El Amrani Chakir <[email protected]> |
||
21 | */ |
||
22 | class FileLogger extends Logger |
||
23 | { |
||
24 | /** @var resource */ |
||
25 | protected $stream; |
||
26 | |||
27 | /** @var string */ |
||
28 | protected $filePath; |
||
29 | |||
30 | /** @var bool */ |
||
31 | protected $append; |
||
32 | |||
33 | /** |
||
34 | * @param string $filePath |
||
35 | * @param int $mode |
||
36 | * @param bool $append |
||
37 | */ |
||
38 | public function __construct($filePath, $mode = LoggerInterface::PLAIN_MODE, $append = false) |
||
39 | { |
||
40 | parent::__construct($mode); |
||
41 | |||
42 | $this->filePath = $filePath; |
||
43 | $this->append = $append; |
||
44 | |||
45 | $this->open(); |
||
46 | } |
||
47 | |||
48 | public function getStream() |
||
49 | { |
||
50 | return $this->stream; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @return string |
||
55 | * |
||
56 | * @throws FileLoggerException |
||
57 | */ |
||
58 | public function getLogs() |
||
59 | { |
||
60 | if (!file_exists($this->filePath) || !is_readable($this->filePath)) { |
||
61 | throw new FileLoggerException($this->filePath . " file is not found or isn't readable."); |
||
62 | } |
||
63 | |||
64 | if (($content = file_get_contents($this->filePath)) === false) { |
||
65 | throw new FileLoggerException("Failed to get the " . $this->filePath . " content."); |
||
66 | } |
||
67 | |||
68 | return $content; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * {@inheritDoc} |
||
73 | * |
||
74 | * @throws FileLoggerException |
||
75 | */ |
||
76 | public function log($level, $message) |
||
77 | { |
||
78 | if ($this->mode === self::PLAIN_MODE) { |
||
79 | |||
80 | $this->write(sprintf("%s %s", $level, $message)); |
||
81 | |||
82 | } elseif ($this->mode === self::ARRAY_MODE) { |
||
83 | // remove the '\r\n' from the end of the message |
||
84 | $message = preg_replace("/[\r\n]$/", '', $message); |
||
85 | $lines = explode(self::CRLF, $message); |
||
86 | $indent = str_repeat(' ', 4); |
||
87 | |||
88 | $output = sprintf( |
||
89 | "%s[%s] array() %s [%s", |
||
90 | ftell($this->stream) ? self::CRLF : '', |
||
91 | count($lines), |
||
92 | $level, |
||
93 | self::CRLF |
||
94 | ); |
||
95 | |||
96 | foreach ($lines as $line) { |
||
97 | $output .= sprintf("%s%s%s", $indent, $line, self::CRLF); |
||
98 | } |
||
99 | |||
100 | $output .= ']'; |
||
101 | |||
102 | if ($this->write($output) === false) { |
||
103 | throw new FileLoggerException("Cannot write to file " . $this->filePath."."); |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * {@inheritDoc} |
||
110 | * |
||
111 | * @throws FileLoggerException |
||
112 | */ |
||
113 | public function clear() |
||
114 | { |
||
115 | if ($this->write('') === false) { |
||
116 | throw new FileLoggerException("Unable to clear the file " . $this->filePath . " content."); |
||
117 | } |
||
118 | |||
119 | return true; |
||
0 ignored issues
–
show
|
|||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @inheritDoc |
||
124 | */ |
||
125 | public function count() |
||
126 | { |
||
127 | if ($this->mode === self::PLAIN_MODE) { |
||
128 | return count(explode(self::CRLF, $this->getLogs())) - 1; |
||
129 | } |
||
130 | |||
131 | return substr_count($this->getLogs(), 'array'); |
||
132 | } |
||
133 | |||
134 | public function __destruct() |
||
135 | { |
||
136 | $this->close(); |
||
137 | } |
||
138 | |||
139 | protected function open() |
||
140 | { |
||
141 | return $this->stream = fopen($this->filePath, $this->append ? 'a' : 'w'); |
||
142 | } |
||
143 | |||
144 | protected function write($content) |
||
145 | { |
||
146 | return fwrite($this->stream, $content); |
||
147 | } |
||
148 | |||
149 | protected function close() |
||
150 | { |
||
151 | return fclose($this->stream); |
||
152 | } |
||
153 | } |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: