Completed
Push — master ( 375de1...c13e1e )
by Andrii
13:21
created

Log   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 47
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setLevel() 0 12 3
A getLevel() 0 4 1
A getLevelName() 0 7 3
A getLevelNames() 0 8 2
1
<?php
2
/**
3
 * Automation tool mixed with code generator for easier continuous development
4
 *
5
 * @link      https://github.com/hiqdev/hidev
6
 * @package   hidev
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\components;
12
13
use yii\log\Logger;
14
15
/**
16
 * Log dispatcher component.
17
 */
18
class Log extends \yii\log\Dispatcher
19
{
20
    protected $_level = Logger::LEVEL_WARNING;
21
22
    protected $levels = [
23
        'error'     => Logger::LEVEL_ERROR,
24
        'warning'   => Logger::LEVEL_WARNING,
25
        'info'      => Logger::LEVEL_INFO,
26
    ];
27
28
    protected $names;
29
30
    public function setLevel($value)
31
    {
32
        if (isset($this->levels[$value])) {
33
            $this->_level = $this->levels[$value];
34
            return;
35
        }
36
        if (in_array($value, $this->levels, false)) {
37
            $this->_level = $value;
38
        }
39
40
        Yii::warning("wrong log level: $value");
41
    }
42
43
    public function getLevel()
44
    {
45
        return $this->_level;
46
    }
47
48
    public function getLevelName($level = null)
49
    {
50
        if ($level === null) {
51
            $level = $this->_level;
52
        }
53
        return isset($this->levelNames[$level]) ? $this->levelNames[$level] : null;
0 ignored issues
show
Documentation introduced by
The property levelNames does not exist on object<hidev\components\Log>. 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...
54
    }
55
56
    public function getLevelNames()
57
    {
58
        if ($this->names === null) {
59
            $this->names = array_flip($this->levels);
60
        }
61
62
        return $this->names;
63
    }
64
}
65