Passed
Pull Request — master (#182)
by Arman
05:01 queued 02:11
created

LoggerManager   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 38
rs 10
c 2
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A getHandler() 0 24 4
1
<?php
2
/**
3
 * Quantum PHP Framework
4
 *
5
 * An open source software development framework for PHP
6
 *
7
 * @package Quantum
8
 * @author Arman Ag. <[email protected]>
9
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
10
 * @link http://quantum.softberg.org/
11
 * @since 2.9.5
12
 */
13
14
namespace Quantum\Logger;
15
16
use Quantum\Libraries\Config\ConfigException;
17
use Quantum\Exceptions\DiException;
18
use Quantum\Loader\Setup;
19
use ReflectionException;
20
21
/**
22
 * Class LoggerManager
23
 * @package Quantum\Logger
24
 */
25
class LoggerManager
26
{
27
    const ADAPTERS = [
28
        'single',
29
        'daily',
30
    ];
31
32
    /**
33
     * @return Logger
34
     * @throws ConfigException
35
     * @throws DiException
36
     * @throws LoggerException
37
     * @throws ReflectionException
38
     */
39
    public static function getHandler(): Logger
40
    {
41
        if(is_debug_mode()) {
42
            return LoggerFactory::createLogger();
43
        }
44
45
        if (!config()->has('logging')) {
46
            config()->import(new Setup('config', 'logging'));
47
        }
48
49
        $logAdapter = config()->get('logging.current');
50
51
        if (!in_array($logAdapter, self::ADAPTERS)) {
52
            throw LoggerException::unsupportedAdapter($logAdapter);
0 ignored issues
show
Bug introduced by
It seems like $logAdapter can also be of type null; however, parameter $name of Quantum\Logger\LoggerExc...n::unsupportedAdapter() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

52
            throw LoggerException::unsupportedAdapter(/** @scrutinizer ignore-type */ $logAdapter);
Loading history...
53
        }
54
55
        $logLevel = config()->get('logging.level', 'error');
56
        LoggerConfig::setAppLogLevel($logLevel);
0 ignored issues
show
Bug introduced by
It seems like $logLevel can also be of type null; however, parameter $level of Quantum\Logger\LoggerConfig::setAppLogLevel() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

56
        LoggerConfig::setAppLogLevel(/** @scrutinizer ignore-type */ $logLevel);
Loading history...
57
58
        $logAdapterClass = __NAMESPACE__ . '\\Adapters\\' . ucfirst($logAdapter) . 'Adapter';
0 ignored issues
show
Bug introduced by
It seems like $logAdapter can also be of type null; however, parameter $string of ucfirst() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

58
        $logAdapterClass = __NAMESPACE__ . '\\Adapters\\' . ucfirst(/** @scrutinizer ignore-type */ $logAdapter) . 'Adapter';
Loading history...
59
60
        $logAdapter = new $logAdapterClass(config()->get('logging.' . $logAdapter));
61
62
        return LoggerFactory::createLogger($logAdapter);
63
    }
64
65
}