Logger   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 0
loc 135
c 0
b 0
f 0
ccs 49
cts 49
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 3
A process() 0 16 4
A getIndexFirstBucket() 0 10 3
A getPsrLevelName() 0 11 2
A add() 0 6 1
A sortBuckets() 0 8 1
A getBuckets() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of the Apix Project.
5
 *
6
 * (c) Franck Cassedanne <franck at ouarz.net>
7
 *
8
 * @license http://opensource.org/licenses/BSD-3-Clause  New BSD License
9
 */
10
11
namespace Apix\Log;
12
13
use Apix\Log\Logger\AbstractLogger;
14
use Psr\Log\InvalidArgumentException;
15
16
/**
17
 * Minimalist logger implementing PSR-3 relying on PHP's error_log().
18
 *
19
 * @author Franck Cassedanne <franck at ouarz.net>
20
 */
21
class Logger extends AbstractLogger
22
{
23
    /**
24
     * Holds all the registered loggers as buckets.
25
     *
26
     * @var Logger\LoggerInterface[].
27
     */
28
    protected $buckets = array();
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param Logger\LoggerInterface[] $loggers
34
     */
35 80
    public function __construct(array $loggers = array())
36
    {
37 80
        foreach ($loggers as $key => $logger) {
38 12
            if ($logger instanceof Logger\LoggerInterface) {
39 8
                $this->buckets[] = $logger;
40 2
            } else {
41 4
                throw new InvalidArgumentException(
42 4
                    sprintf(
43 4
                        '"%s" must interface "%s".',
44 4
                        get_class($logger),
45 7
                        __NAMESPACE__.'\Logger\LoggerInterface'
46 1
                    )
47 1
                );
48
            }
49 20
        }
50 80
        $this->sortBuckets();
51 20
    }
52
53
    /**
54
     * Processes the given log.
55
     * (overwrite abstract).
56
     *
57
     * @param LogEntry $log The log record to handle.
58
     *
59
     * @return bool False when not processed.
60
     */
61 36
    public function process(LogEntry $log)
62
    {
63 36
        $i = $this->getIndexFirstBucket($log->level_code);
64 36
        if (false !== $i) {
65
            while (
66 32
                isset($this->buckets[$i])
67 32
                && $this->buckets[$i]->process($log)
68 8
            ) {
69 20
                ++$i;
70 5
            }
71
72 32
            return true;
73
        }
74
75 4
        return false;
76
    }
77
78
    /**
79
     * Checks if any log bucket can hanle the given code.
80
     *
81
     * @param int $level_code
82
     *
83
     * @return int|false
84
     */
85 36
    protected function getIndexFirstBucket($level_code)
86
    {
87 36
        foreach ($this->buckets as $key => $logger) {
88 36
            if ($logger->isHandling($level_code)) {
89 34
                return $key;
90
            }
91 6
        }
92
93 4
        return false;
94
    }
95
96
    /**
97
     * Gets the name of the PSR-3 logging level.
98
     *
99
     * @param string $level_name
100
     *
101
     * @return string
102
     *
103
     * @throws InvalidArgumentException
104
     */
105 8
    public static function getPsrLevelName($level_name)
106
    {
107 8
        $logLevel = '\Psr\Log\LogLevel::'.strtoupper($level_name);
108 8
        if (!defined($logLevel)) {
109 4
            throw new InvalidArgumentException(
110 4
                sprintf('Invalid PSR-3 log level "%s"', $level_name)
111 1
            );
112
        }
113
114 4
        return $level_name;
115
    }
116
117
    /**
118
     * Adds a logger.
119
     *
120
     * @param Logger\LoggerInterface $logger
121
     *
122
     * @return bool Returns TRUE on success or FALSE on failure.
123
     */
124 32
    public function add(Logger\LoggerInterface $logger)
125
    {
126 32
        $this->buckets[] = $logger;
127
128 32
        return $this->sortBuckets();
129
    }
130
131
    /**
132
     * Sorts the log buckets, prioritizes top-down by minimal level.
133
     * Beware: Exisiting level will be in FIFO order.
134
     * 
135
     * @return bool Returns TRUE on success or FALSE on failure.
136
     */
137 80
    protected function sortBuckets()
138
    {
139 80
        return usort(
140 66
            $this->buckets, function ($a, $b) {
141 24
                return $a->getMinLevel() > $b->getMinLevel();
142 60
            }
143 20
        );
144
    }
145
146
    /**
147
     * Returns all the registered log buckets.
148
     *
149
     * @return array
150
     */
151 16
    public function getBuckets()
152
    {
153 16
        return $this->buckets;
154
    }
155
}
156