Completed
Push — feature/0.7.0 ( 79c386...7f3169 )
by Ryuichi
03:02
created

LoggerConfigurationManager::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
namespace WebStream\Log;
3
4
use WebStream\Module\Utility\FileUtils;
5
use WebStream\Module\Utility\LoggerUtils;
6
use WebStream\Module\Container;
7
use WebStream\Exception\Extend\LoggerException;
8
9
/**
10
 * LoggerConfigurationManager
11
 * @author Ryuichi Tanaka
12
 * @since 2016/01/29
13
 * @version 0.7
14
 */
15
class LoggerConfigurationManager
16
{
17
    use FileUtils, LoggerUtils;
18
19
    /**
20
     * @var Container ログ設定コンテナ
21
     */
22
    private $logContainer;
23
24
    /**
25
     * @var array<string> ログ設定情報
26
     */
27
    private $configMap;
28
29
    /**
30
     * Constructor
31
     */
32
    public function __construct()
33
    {
34
        $this->logContainer = new Container(false);
35
        $this->configMap = [];
36
    }
37
38
    /**
39
     * 設定を読み込む
40
     * @param string $configPath 設定ファイル相対パス
41
     * @throws LoggerException
42
     */
43
    public function load($configPath)
44
    {
45
        $this->loadConfigFile($configPath)
46
             ->loadLogLevel()
47
             ->loadLogFilePath()
48
             ->loadRotateCycle()
49
             ->loadRotateSize()
50
             ->loadApplicationName()
51
             ->loadFormat();
52
    }
53
54
    /**
55
     * ログ設定を返却する
56
     * @return Container ログ設定
57
     */
58
    public function getConfig()
59
    {
60
        return $this->logContainer;
61
    }
62
63
    /**
64
     * 設定ファイルを読み込む
65
     * @param string $configPath 設定アイル相対パス
66
     * @throws LoggerException
67
     */
68
    private function loadConfigFile($configPath)
69
    {
70
        $configMap = $this->parseConfig($configPath);
71
        if ($configMap === null) {
72
            throw new LoggerException("Log config file does not exist: " . $configPath);
73
        }
74
        $this->configMap = $configMap;
75
76
        return $this;
77
    }
78
79
    /**
80
     * ログレベルを読み込む
81
     * @throws LoggerException
82
     */
83 View Code Duplication
    private function loadLogLevel()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    {
85
        if (!array_key_exists("level", $this->configMap)) {
86
            throw new LoggerException("Log level must be defined.");
87
        }
88
89
        $logLevel = $this->toLogLevelValue($this->configMap["level"]);
90
        if ($logLevel === 0) {
91
            throw new LoggerException("Invalid log level: " . $this->configMap["level"]);
92
        }
93
        $this->logContainer->logLevel = $logLevel;
0 ignored issues
show
Documentation introduced by
The property logLevel does not exist on object<WebStream\Module\Container>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
94
95
        return $this;
96
    }
97
98
    /**
99
     * ログ保存先パスを読み込む
100
     * @throws LoggerException
101
     */
102
    private function loadLogFilePath()
103
    {
104
        if (!array_key_exists("path", $this->configMap)) {
105
            throw new LoggerException("Log path must be defined.");
106
        }
107
108
        $path = $this->getApplicationRoot() . "/" . $this->configMap["path"];
109
        if (!file_exists(dirname($path))) {
110
            throw new LoggerException("Log directory does not exist: " . dirname($path));
111
        }
112
        $this->logContainer->logPath = $path;
0 ignored issues
show
Documentation introduced by
The property logPath does not exist on object<WebStream\Module\Container>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
113
114
        $this->logContainer->statusPath = preg_replace_callback('/(.*)\..+/', function ($matches) {
0 ignored issues
show
Documentation introduced by
The property statusPath does not exist on object<WebStream\Module\Container>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
115
            return "$matches[1].status";
116
        }, $this->logContainer->logPath);
0 ignored issues
show
Documentation introduced by
The property logPath does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
117
118
        return $this;
119
    }
120
121
    /**
122
     * ログローテートサイクルを読み込む
123
     * @throws LoggerException
124
     */
125 View Code Duplication
    private function loadRotateCycle()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127
        if (array_key_exists("rotate_cycle", $this->configMap)) {
128
            $rotateCycle = $this->cycle2value($this->configMap["rotate_cycle"]);
129
            // 妥当なローテートサイクルか
130
            if ($rotateCycle === 0) {
131
                throw new LoggerException("Invalid log rotate cycle: " . $this->configMap["rotate_cycle"]);
132
            }
133
            $this->logContainer->rotateCycle = $rotateCycle;
0 ignored issues
show
Documentation introduced by
The property rotateCycle does not exist on object<WebStream\Module\Container>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
134
        }
135
136
        return $this;
137
    }
138
139
    /**
140
     * ログローテートサイズを読み込む
141
     * @throws LoggerException
142
     */
143 View Code Duplication
    private function loadRotateSize()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
    {
145
        if (array_key_exists("rotate_size", $this->configMap)) {
146
            $rotateSize = intval($this->configMap["rotate_size"]);
147
            // ローテートサイズが不正の場合(正の整数以外の値が設定された場合)
148
            if ($rotateSize <= 0) {
149
                throw new LoggerException("Invalid log rotate size: " . $this->configMap["rotate_size"]);
150
            }
151
            $this->logContainer->rotateSize = $rotateSize;
0 ignored issues
show
Documentation introduced by
The property rotateSize does not exist on object<WebStream\Module\Container>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
152
        }
153
154
        return $this;
155
    }
156
157
    /**
158
     * アプリケーション名を読み込む
159
     */
160
    private function loadApplicationName()
161
    {
162
        if (array_key_exists("application_name", $this->configMap)) {
163
            $this->logContainer->applicationName = $this->configMap["application_name"];
0 ignored issues
show
Documentation introduced by
The property applicationName does not exist on object<WebStream\Module\Container>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
164
        }
165
166
        return $this;
167
    }
168
169
    /**
170
     * ロガーフォーマットを読み込む
171
     */
172
    private function loadFormat()
173
    {
174
        if (array_key_exists("format", $this->configMap)) {
175
            $this->logContainer->format = $this->configMap["format"];
0 ignored issues
show
Documentation introduced by
The property format does not exist on object<WebStream\Module\Container>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
176
        } else {
177
            $this->logContainer->format = $this->defaultLoggerFormatter();
0 ignored issues
show
Documentation introduced by
The property format does not exist on object<WebStream\Module\Container>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
178
        }
179
180
        return $this;
181
    }
182
183
    /**
184
     * ログローテートサイクルを時間に変換
185
     * @param string ローテートサイクル
186
     * @return int ローテート時間
187
     */
188
    private function cycle2value($cycle)
189
    {
190
        $day_to_h = 24;
191
        $week_to_h = $day_to_h * 7;
192
        $month_to_h = $day_to_h * intval(date("t", time()));
193
        $year_to_h = $day_to_h * 365;
194
195
        $year = date("Y");
196
        if (($year % 4 === 0 && $year % 100 !== 0) || $year % 400 === 0) {
197
            $year_to_h = $day_to_h * 366;
198
        }
199
200
        switch (strtolower($cycle)) {
201
            case 'day':
202
                return $day_to_h;
203
            case 'week':
204
                return $week_to_h;
205
            case 'month':
206
                return $month_to_h;
207
            case 'year':
208
                return $year_to_h;
209
            default:
210
                return 0;
211
        }
212
    }
213
}
214