Completed
Push — master ( 4aea71...c3431c )
by Gabriel
03:40
created

CreateDrivers::createStackDriver()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 14
rs 10
ccs 6
cts 6
cp 1
crap 2
1
<?php
2
3
namespace Nip\Logger\Manager;
4
5
use Monolog\Handler\HandlerInterface;
6
use Monolog\Handler\StreamHandler;
7
use Monolog\Logger as Monolog;
8
use Nip\Config\Config;
9
use Psr\Log\InvalidArgumentException;
10
11
/**
12
 * Trait CreateDrivers
13
 * @package Nip\Logger\Traits
14
 */
15
trait CreateDrivers
16
{
17
18
    /**
19
     * The registered custom driver creators.
20
     *
21
     * @var array
22
     */
23
    protected $customCreators = [];
24
25
    /**
26
     * @param $name
27
     * @return mixed
28
     */
29 3
    protected function initDriver($name)
30
    {
31 3
        $this->channels[$name] = $this->createLogger($this->resolve($name));
0 ignored issues
show
Bug Best Practice introduced by
The property channels does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
It seems like createLogger() 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

31
        /** @scrutinizer ignore-call */ 
32
        $this->channels[$name] = $this->createLogger($this->resolve($name));
Loading history...
32
33 3
        return $this->channels[$name];
34
    }
35
36
    /**
37
     * Resolve the given log instance by name.
38
     *
39
     * @param string $name
40
     * @return \Psr\Log\LoggerInterface
41
     *
42
     * @throws \InvalidArgumentException
43
     */
44 3
    protected function resolve($name)
45
    {
46 3
        $config = $this->configurationFor($name);
0 ignored issues
show
Bug introduced by
It seems like configurationFor() 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

46
        /** @scrutinizer ignore-call */ 
47
        $config = $this->configurationFor($name);
Loading history...
47
48 3
        if (is_null($config)) {
49
            throw new InvalidArgumentException("Log [{$name}] is not defined.");
50
        }
51
52 3
        if (isset($this->customCreators[$config['driver']])) {
53
            return $this->callCustomCreator($config);
54
        }
55
56 3
        $driverMethod = 'create'.ucfirst($config['driver']).'Driver';
57
58 3
        if (method_exists($this, $driverMethod)) {
59 3
            return $this->{$driverMethod}($config);
60
        }
61
62
        throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported.");
63
    }
64
65
    /**
66
     * Call a custom driver creator.
67
     *
68
     * @param array $config
69
     * @return mixed
70
     */
71
    protected function callCustomCreator(array $config)
72
    {
73
        return $this->customCreators[$config['driver']]($this->app, $config);
74
    }
75
76
    /**
77
     * Create an aggregate log driver instance.
78
     *
79
     * @param array $config
80
     * @return \Psr\Log\LoggerInterface
81
     */
82 1
    protected function createStackDriver($config)
83
    {
84 1
        $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...
85
86 1
        $handlers = [];
87
        array_map(function ($channel) use (&$handlers) {
88 1
            $handlers = array_merge($handlers, $this->channel($channel)->getHandlers());
0 ignored issues
show
Bug introduced by
It seems like channel() 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

88
            $handlers = array_merge($handlers, $this->/** @scrutinizer ignore-call */ channel($channel)->getHandlers());
Loading history...
89 1
        }, $config['channels']);
90
91
//        if ($config['ignore_exceptions'] ?? false) {
92
//            $handlers = [new WhatFailureGroupHandler($handlers)];
93
//        }
94
95 1
        return new Monolog($this->parseChannel($config), $handlers);
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

95
        return new Monolog($this->/** @scrutinizer ignore-call */ parseChannel($config), $handlers);
Loading history...
96
    }
97
98
    /**
99
     * Create an instance of the single file log driver.
100
     *
101
     * @param array $config
102
     * @return \Psr\Log\LoggerInterface
103
     */
104 1
    protected function createSingleDriver($config)
105
    {
106 1
        $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...
107
        $handlers = [
108 1
            $this->prepareHandler(
0 ignored issues
show
Bug introduced by
It seems like prepareHandler() 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

108
            $this->/** @scrutinizer ignore-call */ 
109
                   prepareHandler(
Loading history...
109 1
                new StreamHandler(
110 1
                    $config['path'] ?? $this->getLogsFolderPath().'/bytic.log',
0 ignored issues
show
Bug introduced by
It seems like getLogsFolderPath() 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

110
                    $config['path'] ?? $this->/** @scrutinizer ignore-call */ getLogsFolderPath().'/bytic.log',
Loading history...
111 1
                    $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

111
                    $this->/** @scrutinizer ignore-call */ 
112
                           level($config),
Loading history...
112 1
                    $config['bubble'] ?? true,
113 1
                    $config['permission'] ?? null,
114 1
                    $config['locking'] ?? false
115
                ),
116 1
                $config
117
            ),
118
        ];
119
120 1
        return new Monolog($this->parseChannel($config), $handlers);
121
    }
122
123
    /**
124
     * Create an emergency log handler to avoid white screens of death.
125
     *
126
     * @return \Psr\Log\LoggerInterface
127
     */
128
    protected function createEmergencyLogger()
129
    {
130
        $config = $this->configurationFor('emergency');
131
132
        $handler = new StreamHandler(
133
            $config['path'] ?? $this->getLogsFolderPath().'/bytic.log',
134
            $this->level(['level' => 'debug'])
135
        );
136
137
        return $this->createLogger(
138
            new Monolog('bytic', $this->prepareHandlers([$handler]))
0 ignored issues
show
Bug introduced by
It seems like prepareHandlers() 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

138
            new Monolog('bytic', $this->/** @scrutinizer ignore-call */ prepareHandlers([$handler]))
Loading history...
139
        );
140
    }
141
}
142