FileLog   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 13
dl 0
loc 50
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __destruct() 0 5 2
A __construct() 0 10 2
A close() 0 3 1
A __log() 0 2 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\Settings\SettingsContainerInterface;
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\Settings\SettingsContainerInterface|null $options
29
	 *
30
	 * @throws \chillerlan\Logger\LogException
31
	 */
32
	public function __construct(SettingsContainerInterface $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