LogHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * This file is part of the miBadger package.
5
 *
6
 * @author Michael Webbers <[email protected]>
7
 * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License
8
 */
9
10
namespace miBadger\Log;
11
12
use miBadger\Observer\SubjectInterface;
13
use miBadger\File\File;
14
15
/**
16
 * The log handler class.
17
 *
18
 * @see http://tools.ietf.org/html/rfc5424
19
 * @since 1.0.0
20
 */
21
class LogHandler implements LogHandlerInterface
22
{
23
	const DEFAULT_PATH = '.logger';
24
	const DEFAULT_EXTENSION = '.log';
25
26
	/** @var string The path. */
27
	private $path;
28
29
	/**
30
	 * Construct a log handler object with the given path.
31
	 *
32
	 * @param string $path = self::DEFAULT_PATH
33
	 */
34 2
	public function __construct($path = self::DEFAULT_PATH)
35
	{
36 2
		$this->path = $path;
37 2
	}
38
39
	/**
40
	 * {@inheritdoc}
41
	 */
42 2
	public function update(SubjectInterface $subject, $arguments = null)
43
	{
44
		// Check instance
45 2
		if (!$arguments instanceof LogRecord) {
46 1
			return;
47
		}
48
49
		// Create file
50 1
		$file = new File($this->path . DIRECTORY_SEPARATOR . strtolower($arguments->getLevel()) . static::DEFAULT_EXTENSION);
51
52
		// Append message
53 1
		$file->append((new \DateTime())->format('Y-m-d H:i:s') . ' - ' . $arguments->getMessage() . PHP_EOL);
54 1
	}
55
}
56