Completed
Push — master ( 1776d4...322507 )
by Neomerx
02:47
created

MonologFileSettings::get()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

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