LoggerHelper   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 0
loc 114
ccs 0
cts 60
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fileTargetConfig() 0 17 1
A targetConfigs() 0 10 2
A targetConfig() 0 9 3
B bootstrapConfig() 0 30 10
A config() 0 6 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember/
7
 */
8
9
namespace flipbox\craft\ember\helpers;
10
11
use Craft;
12
use craft\log\FileTarget;
13
use yii\log\Logger;
14
use yii\web\Request;
15
use yii\web\User;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 2.0.0
20
 */
21
class LoggerHelper
22
{
23
    public static $requireSession = true;
24
25
    /**
26
     * Takes an array of log categories and creates log target configs
27
     *
28
     * @param array $categories
29
     * @param array $targetConfig
30
     * @return array
31
     */
32
    public static function targetConfigs(array $categories, array $targetConfig = []): array
33
    {
34
        $configs = [];
35
36
        foreach ($categories as $category) {
37
            $configs[$category] = static::targetConfig($category, $targetConfig);
38
        }
39
40
        return array_filter($configs);
41
    }
42
43
    /**
44
     * Takes a log category and creates a log target config
45
     *
46
     * @param string $category
47
     * @param array $targetConfig
48
     * @return array
49
     */
50
    public static function targetConfig(string $category, array $targetConfig = []): array
51
    {
52
        // When empty, assume file target
53
        if (empty($targetConfig) || !isset($targetConfig['class'])) {
54
            return static::fileTargetConfig($category, $targetConfig);
55
        }
56
57
        return static::bootstrapConfig($category, $targetConfig);
58
    }
59
60
    /**
61
     * Takes a log category and creates a log target config
62
     *
63
     * @param string $category
64
     * @param array $targetConfig
65
     * @return array
66
     * @since 2.3.3
67
     */
68
    public static function fileTargetConfig(string $category, array $targetConfig = []): array
69
    {
70
        $generalConfig = Craft::$app->getConfig()->getGeneral();
71
72
        return static::targetConfig(
73
            $category,
74
            array_merge(
75
                [
76
                    'class' => FileTarget::class,
77
                    'logFile' => '@storage/logs/' . $category . '.log',
78
                    'fileMode' => $generalConfig->defaultFileMode,
79
                    'dirMode' => $generalConfig->defaultDirMode
80
                ],
81
                $targetConfig
82
            )
83
        );
84
    }
85
86
    /**
87
     * @param string $category
88
     * @param array $config
89
     * @return array
90
     * @since 2.6.2
91
     */
92
    public static function bootstrapConfig(string $category, array $config = []): array
93
    {
94
        $request = Craft::$app->getRequest();
95
        $user = Craft::$app->getUser();
96
97
        // Only log console requests and web requests that aren't getAuthTimeout requests
98
        $isConsoleRequest = $request instanceof Request && $request->getIsConsoleRequest();
99
        if (!$isConsoleRequest &&
100
            (static::$requireSession && ($user instanceof User && !Craft::$app->getUser()->enableSession))
101
        ) {
102
            return [];
103
        }
104
105
        $target = [
106
            'logVars' => [],
107
            'categories' => [$category, $category . ':*']
108
        ];
109
110
        if (!$isConsoleRequest) {
111
            // Only log errors and warnings, unless Craft is running in Dev Mode or it's being installed/updated
112
            if (!YII_DEBUG
113
                && Craft::$app->getIsInstalled()
114
                && !Craft::$app->getUpdates()->getIsCraftDbMigrationNeeded()
115
            ) {
116
                $target['levels'] = Logger::LEVEL_ERROR | Logger::LEVEL_WARNING;
117
            }
118
        }
119
120
        return array_merge($target, $config);
121
    }
122
123
    /**
124
     * @return callable
125
     *
126
     * @deprecated
127
     */
128
    public static function config(): callable
129
    {
130
        return function (string $category, array $config = []) {
131
            return static::bootstrapConfig($category, $config);
132
        };
133
    }
134
}
135