EnvironmentProcessor::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 2
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 3
rs 10
c 0
b 0
f 0
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