Completed
Push — master ( 5ae499...6e618b )
by smiley
03:01
created

FileLog::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * Class FileLog
4
 *
5
 * @filesource   FileLog.php
6
 * @created      04.01.2018
7
 * @package      chillerlan\Logger\Output
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2018 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Logger\Output;
14
15
use chillerlan\Logger\LogException;
16
use chillerlan\Traits\ImmutableSettingsInterface;
17
18
class FileLog extends LogOutputAbstract{
19
20
	/**
21
	 * @var bool|resource
22
	 */
23
	protected $fh;
24
25
	/**
26
	 * FileLog constructor.
27
	 *
28
	 * @param \chillerlan\Traits\ImmutableSettingsInterface|null $options
29
	 *
30
	 * @throws \chillerlan\Logger\LogException
31
	 */
32
	public function __construct(ImmutableSettingsInterface $options = null){
33
		parent::__construct($options);
34
35
		if(!is_writable($this->options->logfileDir)){
36
			throw new LogException('log file directory inaccessible');
37
		}
38
39
		$logfile = $this->options->logfileDir.'/'.$this->options->logfileName.'-'.date('Y-m-d').'.'.$this->options->logfileExt;
40
41
		$this->fh = fopen($logfile, 'a+');
42
	}
43
44
	/**
45
	 * @inheritdoc
46
	 */
47
	public function __destruct(){
48
		$this->close();
49
50
		if(gettype($this->fh) === 'resource'){
51
			fclose($this->fh);
52
		}
53
	}
54
55
	/**
56
	 * @inheritdoc
57
	 */
58
	protected function __log(string $level, string $message, array $context = null){
59
		fwrite($this->fh, $this->message($level, $message, $context));
0 ignored issues
show
Bug introduced by
It seems like $this->fh can also be of type boolean; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
		fwrite(/** @scrutinizer ignore-type */ $this->fh, $this->message($level, $message, $context));
Loading history...
60
	}
61
62
	/**
63
	 * @inheritdoc
64
	 */
65
	public function close():LogOutputInterface{
66
		$this->__log('log closed', '~~~');
67
		return $this;
68
	}
69
70
}
71