Completed
Push — master ( 9cb65d...9ef077 )
by yuuki
32s
created

FluentHandler::processFormat()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 2
crap 4
1
<?php
2
3
/**
4
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10
 * THE SOFTWARE.
11
 *
12
 * This software consists of voluntary contributions made by many individuals
13
 * and is licensed under the MIT license.
14
 *
15
 * Copyright (c) 2015-2016 Yuuki Takezawa
16
 *
17
 */
18
namespace Ytake\LaravelFluent;
19
20
use Fluent\Logger\LoggerInterface;
21
use Monolog\Logger;
22
use Monolog\Handler\AbstractProcessingHandler;
23
24
/**
25
 * Class FluentHandler
26
 */
27
class FluentHandler extends AbstractProcessingHandler
28
{
29
    /** @var LoggerInterface */
30
    protected $logger;
31
32
    /** @var string */
33
    protected $tagFormat = '{{channel}}.{{level_name}}';
34
35
    /**
36
     * FluentHandler constructor.
37
     *
38
     * @param LoggerInterface $logger
39
     * @param null|string     $tagFormat
40
     * @param int             $level
41
     * @param bool            $bubble
42
     */
43 6
    public function __construct(LoggerInterface $logger, $tagFormat = null, $level = Logger::DEBUG, $bubble = true)
44
    {
45 6
        $this->logger = $logger;
46 6
        if ($tagFormat !== null) {
47 1
            $this->tagFormat = $tagFormat;
48 1
        }
49 6
        parent::__construct($level, $bubble);
50 6
    }
51
52
    /**
53
     * @param array $record
54
     */
55 2
    protected function write(array $record)
56
    {
57 2
        $tag = $this->populateTag($record);
58 1
        $this->logger->post(
59 1
            $tag,
60
            [
61 1
                'message' => $record['message'],
62 1
                'context' => $record['context'],
63 1
                'extra'   => $record['extra'],
64
            ]
65 1
        );
66 1
    }
67
68
    /**
69
     * @param array $record
70
     *
71
     * @return string
72
     */
73 2
    protected function populateTag(array $record)
74
    {
75 2
        return $this->processFormat($record, $this->tagFormat);
76
    }
77
78
    /**
79
     * @param array  $record
80
     * @param string $tag
81
     *
82
     * @return string
83
     * @throws \Exception
84
     */
85 2
    protected function processFormat(array $record, $tag)
86
    {
87 2
        if (preg_match_all('/\{\{(.*?)\}\}/', $tag, $matches)) {
88 2
            foreach ($matches[1] as $match) {
89 2
                if (!isset($record[$match])) {
90 1
                    throw new \LogicException('No such field in the record');
91
                }
92 2
                $tag = str_replace(sprintf('{{%s}}', $match), $record[$match], $tag);
93 2
            }
94 1
        }
95
96 1
        return $tag;
97
    }
98
99
    /**
100
     * @return LoggerInterface
101
     */
102 1
    public function getLogger()
103
    {
104 1
        return $this->logger;
105
    }
106
}
107