Completed
Push — master ( c3decf...b8fb31 )
by yuuki
01:17
created

FluentHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 89.29%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 73
ccs 25
cts 28
cp 0.8929
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A write() 0 9 1
A populateTag() 0 4 1
A processFormat() 0 15 4
A getLogger() 0 4 1
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
 * Copyright (c) 2015 Yuuki Takezawa
15
 */
16
17
namespace Ytake\LaravelFluent;
18
19
use Fluent\Logger\LoggerInterface;
20
use Monolog\Logger;
21
use Monolog\Handler\AbstractProcessingHandler;
22
23
/**
24
 * Class FluentHandler
25
 *
26
 * @package Ytake\LaravelFluent
27
 */
28
class FluentHandler extends AbstractProcessingHandler
29
{
30
    /** @var LoggerInterface */
31
    protected $logger;
32
33
    /** @var string  */
34
    protected $tagFormat = '{{channel}}.{{level_name}}';
35
36
    /**
37
     * FluentHandler constructor.
38
     *
39
     * @param LoggerInterface $logger
40
     * @param bool|int        $level
41
     * @param bool|true       $bubble
42
     */
43 5
    public function __construct(LoggerInterface $logger, $tagFormat = null, $level = Logger::DEBUG, $bubble = true)
44
    {
45 5
        $this->logger = $logger;
46 5
        if ($tagFormat !== null) {
47
            $this->tagFormat = $tagFormat;
48
        }
49 5
        parent::__construct($level, $bubble);
0 ignored issues
show
Bug introduced by
It seems like $level defined by parameter $level on line 43 can also be of type boolean; however, Monolog\Handler\AbstractHandler::__construct() does only seem to accept integer, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
50 5
    }
51
52
    /**
53
     * @param array $record
54
     */
55 1
    protected function write(array $record)
56
    {
57
        //sprintf($this->tagFormat, $record['channel'], $record['level_name'])
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
58 1
        $tag = $this->populateTag($record);
59 1
        $this->logger->post(
60 1
            $tag,
0 ignored issues
show
Documentation introduced by
$tag is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
61 1
            [$record['message'] => $record['context']]
62 1
        );
63 1
    }
64
65
    /**
66
     * @param array $record
67
     */
68 1
    protected function populateTag($record)
69
    {
70 1
        return $this->processFormat($record, $this->tagFormat);
0 ignored issues
show
Documentation introduced by
$this->tagFormat is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
    }
72
73
    /**
74
     * @param array $record
75
     * @param array $format
76
     */
77 1
    protected function processFormat($record, $format)
78
    {
79 1
        $tag = $format;
80
81 1
        if (preg_match_all('/\{\{(.*?)\}\}/', $tag, $matches)) {
82 1
            foreach ($matches[1] as $match) {
83 1
                if (!isset($record[$match])) {
84
                    throw new Exception('No such field in the record');
85
                }
86 1
                $tag = str_replace(sprintf('{{%s}}', $match), $record[$match], $tag);
87 1
            }
88 1
        }
89
90 1
        return $tag;
91
    }
92
93
    /**
94
     * @return LoggerInterface
95
     */
96 1
    public function getLogger()
97
    {
98 1
        return $this->logger;
99
    }
100
}
101