Completed
Push — feature/0.7.0 ( 17fb54...5bdbd1 )
by Ryuichi
04:00
created

LoggerConfigurationManager::__construct()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
rs 8.6845
cc 4
eloc 15
nc 3
nop 1
1
<?php
2
namespace WebStream\Log;
3
4
use WebStream\DI\Injector;
5
use WebStream\IO\File;
6
use WebStream\Module\Utility\FileUtils;
7
use WebStream\Module\Utility\LoggerUtils;
8
use WebStream\Module\Container;
9
use WebStream\Exception\Extend\LoggerException;
10
11
/**
12
 * LoggerConfigurationManager
13
 * @author Ryuichi Tanaka
14
 * @since 2016/01/29
15
 * @version 0.7
16
 */
17
class LoggerConfigurationManager
18
{
19
    use Injector, FileUtils, LoggerUtils;
20
21
    /**
22
     * @var Container ログ設定コンテナ
23
     */
24
    private $logContainer;
25
26
    /**
27
     * @var Container IOコンテナ
28
     */
29
    private $ioContainer;
30
31
    /**
32
     * @var array<string> ログ設定情報
33
     */
34
    private $configMap;
35
36
    /**
37
     * Constructor
38
     */
39
    public function __construct($config)
40
    {
41
        if (is_array($config)) {
42
            $configMap = $config;
43
        } else {
44
            $configMap = $this->parseConfig($config);
45
            if ($configMap === null) {
46
                throw new LoggerException("Log config file does not exist: " . $config);
47
            }
48
        }
49
50
        $this->logContainer = new Container(false);
51
        $this->ioContainer = new Container();
52
53
        $rootDir = $this->getApplicationRoot();
54
        $this->ioContainer->file = function () use ($rootDir, $configMap) {
0 ignored issues
show
Documentation introduced by
The property file 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...
55
            if (!array_key_exists("path", $configMap)) {
56
                throw new LoggerException("Log path must be defined.");
57
            }
58
            return new File($rootDir . "/" . $configMap["path"]);
59
        };
60
61
        $this->configMap = $configMap;
62
    }
63
64
    /**
65
     * 設定を読み込む
66
     * @throws LoggerException
67
     */
68
    public function load()
69
    {
70
        $this->loadLogLevel()
71
             ->loadLogFilePath()
72
             ->loadRotateCycle()
73
             ->loadRotateSize()
74
             ->loadApplicationName()
75
             ->loadFormat();
76
    }
77
78
    /**
79
     * ログ設定を返却する
80
     * @return Container ログ設定
81
     */
82
    public function getConfig()
83
    {
84
        return $this->logContainer;
85
    }
86
87
    /**
88
     * ログレベルを読み込む
89
     * @throws LoggerException
90
     */
91 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...
92
    {
93
        if (!array_key_exists("level", $this->configMap)) {
94
            throw new LoggerException("Log level must be defined.");
95
        }
96
97
        $logLevel = $this->toLogLevelValue($this->configMap["level"]);
98
        if ($logLevel === 0) {
99
            throw new LoggerException("Invalid log level: " . $this->configMap["level"]);
100
        }
101
        $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...
102
103
        return $this;
104
    }
105
106
    /**
107
     * ログ保存先パスを読み込む
108
     * @throws LoggerException
109
     */
110
    private function loadLogFilePath()
111
    {
112
        $file = $this->ioContainer->file;
0 ignored issues
show
Documentation introduced by
The property file 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
        if (!($file->exists() && $file->isFile())) {
114
            throw new LoggerException("Log directory does not exist: " . $file->getFilePath());
115
        }
116
117
        $this->logContainer->logPath = $file->getFilePath();
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...
118
        $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...
119
            return "$matches[1].status";
120
        }, $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...
121
122
        return $this;
123
    }
124
125
    /**
126
     * ログローテートサイクルを読み込む
127
     * @throws LoggerException
128
     */
129 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...
130
    {
131
        if (array_key_exists("rotate_cycle", $this->configMap)) {
132
            $rotateCycle = $this->cycle2value($this->configMap["rotate_cycle"]);
133
            // 妥当なローテートサイクルか
134
            if ($rotateCycle === 0) {
135
                throw new LoggerException("Invalid log rotate cycle: " . $this->configMap["rotate_cycle"]);
136
            }
137
            $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...
138
        }
139
140
        return $this;
141
    }
142
143
    /**
144
     * ログローテートサイズを読み込む
145
     * @throws LoggerException
146
     */
147 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...
148
    {
149
        if (array_key_exists("rotate_size", $this->configMap)) {
150
            $rotateSize = intval($this->configMap["rotate_size"]);
151
            // ローテートサイズが不正の場合(正の整数以外の値が設定された場合)
152
            if ($rotateSize <= 0) {
153
                throw new LoggerException("Invalid log rotate size: " . $this->configMap["rotate_size"]);
154
            }
155
            $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...
156
        }
157
158
        return $this;
159
    }
160
161
    /**
162
     * アプリケーション名を読み込む
163
     */
164
    private function loadApplicationName()
165
    {
166
        if (array_key_exists("application_name", $this->configMap) && !empty($this->configMap["application_name"])) {
167
            $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...
168
        }
169
170
        return $this;
171
    }
172
173
    /**
174
     * ロガーフォーマットを読み込む
175
     */
176
    private function loadFormat()
177
    {
178
        if (array_key_exists("format", $this->configMap)) {
179
            $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...
180
        } else {
181
            $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...
182
        }
183
184
        return $this;
185
    }
186
187
    /**
188
     * ログローテートサイクルを時間に変換
189
     * @param string ローテートサイクル
190
     * @return int ローテート時間
191
     */
192
    private function cycle2value($cycle)
193
    {
194
        $day_to_h = 24;
195
        $week_to_h = $day_to_h * 7;
196
        $month_to_h = $day_to_h * intval(date("t", time()));
197
        $year_to_h = $day_to_h * 365;
198
199
        $year = date("Y");
200
        if (($year % 4 === 0 && $year % 100 !== 0) || $year % 400 === 0) {
201
            $year_to_h = $day_to_h * 366;
202
        }
203
204
        switch (strtolower($cycle)) {
205
            case 'day':
206
                return $day_to_h;
207
            case 'week':
208
                return $week_to_h;
209
            case 'month':
210
                return $month_to_h;
211
            case 'year':
212
                return $year_to_h;
213
            default:
214
                return 0;
215
        }
216
    }
217
}
218