Passed
Pull Request — master (#190)
by Arman
02:55
created

LoggerFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 71
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAdapterClass() 0 7 2
A createInstance() 0 18 3
A get() 0 7 2
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.5
13
 */
14
15
namespace Quantum\Libraries\Logger\Factories;
16
17
use Quantum\Libraries\Config\Exceptions\ConfigException;
18
use Quantum\Libraries\Logger\Exceptions\LoggerException;
19
use Quantum\Libraries\Logger\Adapters\MessageAdapter;
20
use Quantum\Libraries\Logger\Adapters\SingleAdapter;
21
use Quantum\Libraries\Logger\Adapters\DailyAdapter;
22
use Quantum\Libraries\Logger\LoggerConfig;
23
use Quantum\Di\Exceptions\DiException;
24
use Quantum\Exceptions\BaseException;
25
use Quantum\Libraries\Logger\Logger;
26
use Quantum\Loader\Setup;
27
use ReflectionException;
28
29
/**
30
 * Class LoggerFactory
31
 * @package Quantum\Logger
32
 */
33
class LoggerFactory
34
{
35
36
    /**
37
     * Supported adapters
38
     */
39
    const ADAPTERS = [
40
        Logger::SINGLE => SingleAdapter::class,
41
        Logger::DAILY => DailyAdapter::class,
42
    ];
43
44
    /**
45
     * @var Logger|null
46
     */
47
    private static $instance = null;
48
49
    /**
50
     * @return Logger
51
     * @throws BaseException
52
     * @throws ConfigException
53
     * @throws DiException
54
     * @throws ReflectionException
55
     */
56
    public static function get(): Logger
57
    {
58
        if (self::$instance === null) {
59
            return self::$instance = self::createInstance();
60
        }
61
62
        return self::$instance;
63
    }
64
65
    /**
66
     * @return Logger
67
     * @throws BaseException
68
     * @throws ConfigException
69
     * @throws DiException
70
     * @throws ReflectionException
71
     */
72
    private static function createInstance(): Logger
73
    {
74
        if (is_debug_mode()) {
75
            return new Logger(new MessageAdapter());
76
        }
77
78
        if (!config()->has('logging')) {
79
            config()->import(new Setup('config', 'logging'));
80
        }
81
82
        $adapter = config()->get('logging.current');
83
84
        $adapterClass = self::getAdapterClass($adapter);
0 ignored issues
show
Bug introduced by
It seems like $adapter can also be of type null; however, parameter $adapter of Quantum\Libraries\Logger...tory::getAdapterClass() 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

84
        $adapterClass = self::getAdapterClass(/** @scrutinizer ignore-type */ $adapter);
Loading history...
85
86
        $logLevel = config()->get('logging.level', 'error');
87
        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\Libraries\Logger...onfig::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

87
        LoggerConfig::setAppLogLevel(/** @scrutinizer ignore-type */ $logLevel);
Loading history...
88
89
        return new Logger(new $adapterClass(config()->get('logging.' . $adapter)));
90
    }
91
92
    /**
93
     * @param string $adapter
94
     * @return string
95
     * @throws BaseException
96
     */
97
    private static function getAdapterClass(string $adapter): string
98
    {
99
        if (!array_key_exists($adapter, self::ADAPTERS)) {
100
            throw LoggerException::adapterNotSupported($adapter);
101
        }
102
103
        return self::ADAPTERS[$adapter];
104
    }
105
}