Resque_Log::interpolate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 10
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
/**
4
 * Resque default logger PSR-3 compliant
5
 *
6
 * @package		Resque/Stat
7
 * @author		Chris Boulton <[email protected]>
8
 * @license		http://www.opensource.org/licenses/mit-license.php
9
 */
10
class Resque_Log extends Psr\Log\AbstractLogger
11
{
12
	public $verbose;
13 71
14 71
	public function __construct($verbose = false)
15 71
	{
16
		$this->verbose = $verbose;
17
	}
18
19
	/**
20
	 * Logs with an arbitrary level.
21
	 *
22
	 * @param mixed   $level    PSR-3 log level constant, or equivalent string
23
	 * @param string  $message  Message to log, may contain a { placeholder }
24
	 * @param array   $context  Variables to replace { placeholder }
25 26
	 * @return null
26
	 */
27 26
	public function log($level, $message, array $context = array())
28
	{
29
		if ($this->verbose) {
30
			fwrite(
31
				STDOUT,
32
				'[' . $level . '] [' . strftime('%T %Y-%m-%d') . '] ' . $this->interpolate($message, $context) . PHP_EOL
33
			);
34
			return;
35 26
		}
36 14
37 14
		if (!($level === Psr\Log\LogLevel::INFO || $level === Psr\Log\LogLevel::DEBUG)) {
38 14
			fwrite(
39
				STDOUT,
40
				'[' . $level . '] ' . $this->interpolate($message, $context) . PHP_EOL
41 26
			);
42
		}
43
	}
44
45
	/**
46
	 * Fill placeholders with the provided context
47
	 * @author Jordi Boggiano [email protected]
48
	 *
49
	 * @param  string  $message  Message to be logged
50
	 * @param  array   $context  Array of variables to use in message
51 16
	 * @return string
52
	 */
53
	public function interpolate($message, array $context = array())
54 16
	{
55 16
		// build a replacement array with braces around the context keys
56 15
		$replace = array();
57
		foreach ($context as $key => $val) {
58
			$replace['{' . $key . '}'] = $val;
59
		}
60 16
61
		// interpolate replacement values into the message and return
62
		return strtr($message, $replace);
63 1
	}
64
}
65