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

LogHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 35
ccs 9
cts 9
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A update() 0 13 2
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