MonologFileSettings   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 53
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 18 2
A getSettings() 0 12 2
A getAppConfig() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Application\Packages\Monolog;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Contracts\Application\ApplicationConfigurationInterface as A;
22
use Limoncello\Contracts\Settings\Packages\MonologFileSettingsInterface;
23
use Monolog\Logger;
24
use function assert;
25
use function glob;
26
27
/**
28
 * @package Limoncello\Application
29
 */
30
class MonologFileSettings implements MonologFileSettingsInterface
31
{
32
    /**
33
     * @var array
34
     */
35
    private $appConfig;
36 1
37
    /**
38 1
     * @inheritdoc
39
     */
40 1
    final public function get(array $appConfig): array
41
    {
42 1
        $this->appConfig = $appConfig;
43 1
44 1
        $defaults = $this->getSettings();
45 1
46 1
        $logFolder = $defaults[static::KEY_LOG_FOLDER] ?? null;
47
        $logFile   = $defaults[static::KEY_LOG_FILE] ?? null;
48 1
        assert(
49
            $logFolder !== null && empty(glob($logFolder)) === false,
50 1
            "Invalid Logs folder `$logFolder`."
51
        );
52 1
        assert(empty($logFile) === false, "Invalid Logs file name `$logFile`.");
53
54
        $logPath = $logFolder . DIRECTORY_SEPARATOR . $logFile;
55
56
        return $defaults + [static::KEY_LOG_PATH => $logPath];
57
    }
58 1
59
    /**
60 1
     * @return array
61
     */
62 1
    protected function getSettings(): array
63
    {
64
        $appConfig = $this->getAppConfig();
65 1
66 1
        $isDebug = (bool)($appConfig[A::KEY_IS_DEBUG] ?? false);
67 1
68
        return [
69
            static::KEY_IS_ENABLED => (bool)($appConfig[A::KEY_IS_LOG_ENABLED] ?? false),
70
            static::KEY_LOG_LEVEL  => $isDebug === true ? Logger::DEBUG : Logger::INFO,
71
            static::KEY_LOG_FILE   => 'limoncello.log',
72
        ];
73
    }
74 1
75
    /**
76 1
     * @return mixed
77
     */
78
    protected function getAppConfig()
79
    {
80
        return $this->appConfig;
81
    }
82
}
83