Passed
Push — master ( c3431c...d1be19 )
by Gabriel
03:48
created

MonologWrappers::prepareHandler()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 9.2978

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 10
c 1
b 0
f 1
nc 9
nop 2
dl 0
loc 17
rs 8.4444
ccs 8
cts 11
cp 0.7272
crap 9.2978
1
<?php
2
3
namespace Nip\Logger\Manager;
4
5
use Monolog\Formatter\LineFormatter;
6
use Monolog\Handler\FormattableHandlerInterface;
7
use Monolog\Handler\HandlerInterface;
8
use Monolog\Logger as Monolog;
9
use Nip\Config\Config;
10
use Psr\Log\InvalidArgumentException;
11
12
/**
13
 * Class MonologWrappers
14
 * @package Nip\Logger\Manager
15
 */
16
trait MonologWrappers
17
{
18
    /**
19
     * Create an instance of any handler available in Monolog.
20
     *
21
     * @param array $config
22
     * @return \Psr\Log\LoggerInterface
23
     *
24
     * @throws \InvalidArgumentException
25
     */
26 2
    protected function createMonologDriver($config)
27
    {
28 2
        $config = $config instanceof Config ? $config->toArray() : $config;
0 ignored issues
show
introduced by
$config is never a sub-type of Nip\Config\Config.
Loading history...
29
30 2
        if (!is_a($config['handler'], HandlerInterface::class, true)) {
31
            throw new InvalidArgumentException(
32
                $config['handler'].' must be an instance of '.HandlerInterface::class
33
            );
34
        }
35
36 2
        $with = array_merge(
37 2
            ['level' => $this->level($config)],
0 ignored issues
show
Bug introduced by
It seems like level() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
            ['level' => $this->/** @scrutinizer ignore-call */ level($config)],
Loading history...
38 2
            $config['with'] ?? [],
39 2
            $config['handler_with'] ?? []
40
        );
41
42 2
        return new Monolog($this->parseChannel($config), [
0 ignored issues
show
Bug introduced by
It seems like parseChannel() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        return new Monolog($this->/** @scrutinizer ignore-call */ parseChannel($config), [
Loading history...
43 2
            $this->prepareHandler(
44 2
                $this->getContainer()->get($config['handler'], $with),
0 ignored issues
show
Bug introduced by
It seems like getContainer() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
                $this->/** @scrutinizer ignore-call */ 
45
                       getContainer()->get($config['handler'], $with),
Loading history...
45 2
                $config
46
            ),
47
        ]);
48
    }
49
50
    /**
51
     * Prepare the handlers for usage by Monolog.
52
     *
53
     * @param array $handlers
54
     * @return array
55
     */
56
    protected function prepareHandlers(array $handlers)
57
    {
58
        foreach ($handlers as $key => $handler) {
59
            $handlers[$key] = $this->prepareHandler($handler);
60
        }
61
62
        return $handlers;
63
    }
64
65
    /**
66
     * Prepare the handler for usage by Monolog.
67
     *
68
     * @param \Monolog\Handler\HandlerInterface $handler
69
     * @param array $config
70
     * @return \Monolog\Handler\HandlerInterface
71
     */
72 4
    protected function prepareHandler(HandlerInterface $handler, array $config = [])
73
    {
74 4
        $isHandlerFormattable = false;
75
76 4
        if (Monolog::API === 1) {
0 ignored issues
show
introduced by
The condition Monolog\Logger::API === 1 is always false.
Loading history...
77
            $isHandlerFormattable = true;
78 4
        } elseif (Monolog::API === 2 && $handler instanceof FormattableHandlerInterface) {
79 4
            $isHandlerFormattable = true;
80
        }
81
82 4
        if ($isHandlerFormattable && !isset($config['formatter'])) {
83 4
            $handler->setFormatter($this->formatter());
0 ignored issues
show
Bug introduced by
The method setFormatter() does not exist on Monolog\Handler\HandlerInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Monolog\Handler\Handler or Monolog\Handler\AbstractHandler or Monolog\Handler\NullHandler or Monolog\Handler\NoopHandler. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
            $handler->/** @scrutinizer ignore-call */ 
84
                      setFormatter($this->formatter());
Loading history...
84
        } elseif ($isHandlerFormattable && $config['formatter'] !== 'default') {
85
            $handler->setFormatter($this->app->make($config['formatter'], $config['formatter_with'] ?? []));
86
        }
87
88 4
        return $handler;
89
    }
90
91
    /**
92
     * Get a Monolog formatter instance.
93
     *
94
     * @return \Monolog\Formatter\FormatterInterface
95
     */
96 4
    protected function formatter()
97
    {
98 4
        $formatter = new LineFormatter(null, $this->dateFormat, true, true);
99 4
        $formatter->includeStacktraces();
100
101 4
        return $formatter;
102
    }
103
}
104