Passed
Pull Request — master (#182)
by Arman
03:14
created

LoggerManager   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 39
rs 10
c 1
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\Exceptions\ConfigException;
17
use Quantum\Exceptions\LangException;
18
use Quantum\Exceptions\DiException;
19
use Quantum\Loader\Setup;
20
use ReflectionException;
21
22
/**
23
 * Class LoggerManager
24
 * @package Quantum\Logger
25
 */
26
class LoggerManager
27
{
28
    const ADAPTERS = [
29
        'single',
30
        'daily',
31
    ];
32
33
    /**
34
     * @return Logger
35
     * @throws ConfigException
36
     * @throws DiException
37
     * @throws LoggerException
38
     * @throws ReflectionException
39
     * @throws LangException
40
     */
41
    public static function getHandler(): Logger
42
    {
43
        if(is_debug_mode()) {
44
            return LoggerFactory::createLogger();
45
        }
46
47
        if (!config()->has('logging')) {
48
            config()->import(new Setup('config', 'logging'));
49
        }
50
51
        $logAdapter = config()->get('logging.current');
52
53
        if (!in_array($logAdapter, self::ADAPTERS)) {
54
            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

54
            throw LoggerException::unsupportedAdapter(/** @scrutinizer ignore-type */ $logAdapter);
Loading history...
55
        }
56
57
        $logLevel = config()->get('logging.level', 'error');
58
        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

58
        LoggerConfig::setAppLogLevel(/** @scrutinizer ignore-type */ $logLevel);
Loading history...
59
60
        $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

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