EnvironmentProcessor   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 49
ccs 8
cts 16
cp 0.5
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A __invoke() 0 15 5
1
<?php
2
/*
3
 * This file is part of Monolog Extensions
4
 *
5
 * Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @see  http://github.com/graze/MonologExtensions/blob/master/LICENSE
11
 * @link http://github.com/graze/MonologExtensions
12
 */
13
14
namespace Graze\Monolog\Processor;
15
16
class EnvironmentProcessor
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $env;
22
23
    /**
24
     * @var string
25
     */
26
    protected $host;
27
28
    /**
29
     * @param array       $env
30
     * @param string|null $host
31
     */
32 3
    public function __construct(array $env = null, $host = null)
33
    {
34 3
        if (null === $env) {
35 3
            $env = array_change_key_case($_SERVER, CASE_LOWER);
36
        }
37 3
        if (null === $host) {
38 3
            $host = gethostname();
39
        }
40
41 3
        $this->env = $env;
42 3
        $this->host = $host;
43 3
    }
44
45
    /**
46
     * @param array $record
47
     *
48
     * @return array
49
     */
50
    public function __invoke(array $record)
51
    {
52
        if (null !== $this->host) {
53
            $record['host'] = $this->host;
54
        }
55
56
        if (!empty($this->env)) {
57
            $record['context']['env'] = $this->env;
58
59
            if (!isset($record['host']) && isset($this->env['server_name'])) {
60
                $record['host'] = $this->env['server_name'];
61
            }
62
        }
63
64
        return $record;
65
    }
66
}
67