Completed
Branch develop (619864)
by Michael
05:01
created

LogHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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