Completed
Push — master ( 22d0de...5ae499 )
by smiley
01:45
created

Logfile::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * Class Logfile
4
 *
5
 * @filesource   Logfile.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\ContainerInterface;
17
18
/**
19
 */
20
class Logfile extends LogOutputAbstract{
21
22
	/**
23
	 * @var bool|resource
24
	 */
25
	protected $fh;
26
27
	/**
28
	 * Logfile constructor.
29
	 *
30
	 * @param \chillerlan\Traits\ContainerInterface|null $options
31
	 *
32
	 * @throws \chillerlan\Logger\LogException
33
	 */
34
	public function __construct(ContainerInterface $options = null){
35
		parent::__construct($options);
36
37
		if(!is_writable($this->options->logfileDir)){
38
			throw new LogException('log file directory inaccessible');
39
		}
40
41
		$logfile = $this->options->logfileDir.'/'.$this->options->logfileName.'-'.date('Y-m-d').'.'.$this->options->logfileExt;
42
43
		$this->fh = fopen($logfile, 'a+');
44
	}
45
46
	/**
47
	 * @inheritdoc
48
	 */
49
	public function __destruct(){
50
		$this->close();
51
52
		if(gettype($this->fh) === 'resource'){
53
			fclose($this->fh);
54
		}
55
	}
56
57
	/**
58
	 * @inheritdoc
59
	 */
60
	protected function __log(string $level, string $message, array $context = null){
61
		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

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