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

CreateDrivers::createDailyDriver()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 10
ccs 7
cts 7
cp 1
crap 2
1
<?php
2
3
namespace Nip\Logger\Manager;
4
5
use Monolog\Handler\HandlerInterface;
6
use Monolog\Handler\RotatingFileHandler;
7
use Monolog\Handler\StreamHandler;
8
use Monolog\Logger as Monolog;
9
use Nip\Config\Config;
10
use Nip\Logger\Logger;
11
use Psr\Log\InvalidArgumentException;
12
use Psr\Log\LoggerInterface;
13
14
/**
15
 * Trait CreateDrivers
16
 * @package Nip\Logger\Traits
17
 */
18
trait CreateDrivers
19
{
20
21
    /**
22
     * The registered custom driver creators.
23
     *
24
     * @var array
25
     */
26
    protected $customCreators = [];
27
28
    /**
29
     * @param $name
30
     * @return mixed
31
     */
32 4
    protected function initDriver($name)
33
    {
34 4
        $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...
35
36 4
        return $this->channels[$name];
37
    }
38
39
    /**
40
     * Resolve the given log instance by name.
41
     *
42
     * @param string $name
43
     * @return LoggerInterface
44
     *
45
     * @throws \InvalidArgumentException
46
     */
47 4
    protected function resolve($name)
48
    {
49 4
        $config = $this->configurationFor($name);
50
51 4
        if (is_null($config)) {
0 ignored issues
show
introduced by
The condition is_null($config) is always false.
Loading history...
52
            throw new InvalidArgumentException("Log [{$name}] is not defined.");
53
        }
54
55 4
        if (isset($this->customCreators[$config['driver']])) {
56
            return $this->callCustomCreator($config);
57
        }
58
59 4
        $driverMethod = 'create'.ucfirst($config['driver']).'Driver';
60
61 4
        if (method_exists($this, $driverMethod)) {
62 4
            return $this->{$driverMethod}($config);
63
        }
64
65
        throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported.");
66
    }
67
68
    /**
69
     * Call a custom driver creator.
70
     *
71
     * @param array $config
72
     * @return mixed
73
     */
74
    protected function callCustomCreator(array $config)
75
    {
76
        return $this->customCreators[$config['driver']](app(), $config);
77
    }
78
79
    /**
80
     * Create an aggregate log driver instance.
81
     *
82
     * @param array $config
83
     * @return LoggerInterface
84
     */
85 1
    protected function createStackDriver($config)
86
    {
87 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...
88
89 1
        $handlers = [];
90
        array_map(function ($channel) use (&$handlers) {
91 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

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

98
        return new Monolog($this->/** @scrutinizer ignore-call */ parseChannel($config), $handlers);
Loading history...
99
    }
100
101
    /**
102
     * Create an instance of the single file log driver.
103
     *
104
     * @param array $config
105
     * @return LoggerInterface
106
     */
107 1
    protected function createSingleDriver($config)
108
    {
109 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...
110
        $handlers = [
111 1
            $this->prepareHandler(
112 1
                new StreamHandler(
113 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

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

114
                    $this->/** @scrutinizer ignore-call */ 
115
                           level($config),
Loading history...
115 1
                    $config['bubble'] ?? true,
116 1
                    $config['permission'] ?? null,
117 1
                    $config['locking'] ?? false
118
                ),
119 1
                $config
120
            ),
121
        ];
122
123 1
        return new Monolog($this->parseChannel($config), $handlers);
124
    }
125
126
    /**
127
     * @param array $config
128
     * @return Monolog
129
     */
130 1
    protected function createDailyDriver($config)
131
    {
132 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...
133
134
        $handlers = [
135 1
            $this->prepareHandler(new RotatingFileHandler(
136 1
                $config['path'], $config['days'] ?? 7, $this->level($config),
137 1
                $config['bubble'] ?? true, $config['permission'] ?? null, $config['locking'] ?? false
138 1
            ), $config),
139
        ];
140
141 1
        return new Monolog($this->parseChannel($config), $handlers);
142
    }
143
144
    /**
145
     * Create an emergency log handler to avoid white screens of death.
146
     *
147
     * @return LoggerInterface
148
     */
149
    protected function createEmergencyLogger()
150
    {
151
        $config = $this->configurationFor('emergency');
152
153
        $handler = new StreamHandler(
154
            $config['path'] ?? $this->getLogsFolderPath().'/bytic.log',
155
            $this->level(['level' => 'debug'])
156
        );
157
158
        return $this->createLogger(
159
            new Monolog('bytic', [$this->prepareHandler($handler)])
160
        );
161
    }
162
163
    /**
164
     * @param $logger
165
     * @return Logger|LoggerInterface
166
     */
167
    abstract protected function createLogger($logger);
168
169
    /**
170
     * @param string $logger
171
     * @return array
172
     */
173
    abstract protected function configurationFor($logger);
174
175
    /**
176
     * @param HandlerInterface $handler
177
     * @param array $config
178
     * @return HandlerInterface
179
     */
180
    abstract protected function prepareHandler(HandlerInterface $handler, array $config = []);
181
}
182