| Total Complexity | 10 |
| Total Lines | 45 |
| Duplicated Lines | 0 % |
| Coverage | 96.3% |
| Changes | 0 | ||
| 1 | <?php |
||
| 10 | class StreamLogWriter implements LogWriter { |
||
| 11 | |||
| 12 | private $url; |
||
| 13 | private $stream; |
||
| 14 | |||
| 15 | 36 | public function __construct( string $url ) { |
|
| 16 | 36 | $this->url = $url; |
|
| 17 | 36 | } |
|
| 18 | |||
| 19 | /** |
||
| 20 | * @param string $logEntry |
||
| 21 | * @throws LoggingError |
||
| 22 | */ |
||
| 23 | 36 | public function write( string $logEntry ) { |
|
| 24 | 36 | $this->openStreamIfNeeded(); |
|
| 25 | 35 | fwrite( |
|
| 26 | 35 | $this->stream, |
|
| 27 | 35 | $logEntry . "\n" |
|
| 28 | ); |
||
| 29 | 35 | } |
|
| 30 | |||
| 31 | 36 | private function openStreamIfNeeded() { |
|
| 32 | 36 | if ( $this->stream !== null ) { |
|
| 33 | 2 | return; |
|
| 34 | } |
||
| 35 | 36 | $this->createPathIfNeeded(); |
|
| 36 | 35 | $this->stream = @fopen( $this->url, 'a' ); |
|
| 37 | 35 | if ( $this->stream === false ) { |
|
| 38 | throw new LoggingError( 'Could not open ' . $this->url ); |
||
| 39 | } |
||
| 40 | 35 | } |
|
| 41 | |||
| 42 | 36 | private function createPathIfNeeded() { |
|
| 49 | } |
||
| 50 | 2 | } |
|
| 51 | |||
| 52 | 12 | public function __destruct() { |
|
| 55 | } |
||
| 56 | 12 | } |
|
| 57 | |||
| 58 | } |
If you suppress an error, we recommend checking for the error condition explicitly: