Completed
Pull Request — master (#60)
by ARCANEDEV
12:03
created

LogLevels::getLocale()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php namespace Arcanedev\LogViewer\Utilities;
2
3
use Arcanedev\LogViewer\Contracts\LogLevelsInterface;
4
use Illuminate\Translation\Translator;
5
use Psr\Log\LogLevel;
6
use ReflectionClass;
7
8
/**
9
 * Class     LogLevels
10
 *
11
 * @package  Arcanedev\LogViewer\Utilities
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class LogLevels implements LogLevelsInterface
15
{
16
    /* ------------------------------------------------------------------------------------------------
17
     |  Properties
18
     | ------------------------------------------------------------------------------------------------
19
     */
20
    /**
21
     * The log levels.
22
     *
23
     * @var array
24
     */
25
    protected static $levels = [];
26
27
    /**
28
     * The Translator instance.
29
     *
30
     * @var \Illuminate\Translation\Translator
31
     */
32
    private $translator;
33
34
    /**
35
     * The selected locale.
36
     *
37
     * @var string
38
     */
39
    private $locale;
40
41
    /* ------------------------------------------------------------------------------------------------
42
     |  Constructor
43
     | ------------------------------------------------------------------------------------------------
44
     */
45
    /**
46
     * Create LogLevels instance.
47
     *
48
     * @param  \Illuminate\Translation\Translator  $translator
49
     * @param  string                              $locale
50
     */
51 936
    public function __construct(Translator $translator, $locale)
52
    {
53 936
        $this->setTranslator($translator);
54 936
        $this->setLocale($locale);
55 936
    }
56
57
    /* ------------------------------------------------------------------------------------------------
58
     |  Getters & Setters
59
     | ------------------------------------------------------------------------------------------------
60
     */
61
    /**
62
     * Set the Translator instance.
63
     *
64
     * @param  \Illuminate\Translation\Translator  $translator
65
     *
66
     * @return self
67
     */
68 936
    public function setTranslator(Translator $translator)
69
    {
70 936
        $this->translator = $translator;
71
72 936
        return $this;
73
    }
74
75
    /**
76
     * Get the selected locale.
77
     *
78
     * @return string
79
     */
80 120
    public function getLocale()
81
    {
82 120
        return $this->locale === 'auto'
83 120
            ? $this->translator->getLocale()
84 120
            : $this->locale;
85
    }
86
87
    /**
88
     * Set the selected locale.
89
     *
90
     * @param  string  $locale
91
     *
92
     * @return self
93
     */
94 936
    public function setLocale($locale)
95
    {
96 936
        $this->locale = is_null($locale) ? 'auto' : $locale;
97
98 936
        return $this;
99
    }
100
101
    /* ------------------------------------------------------------------------------------------------
102
     |  Main Functions
103
     | ------------------------------------------------------------------------------------------------
104
     */
105
    /**
106
     * Get the log levels.
107
     *
108
     * @param  bool|false  $flip
109
     *
110
     * @return array
111
     */
112 372
    public function lists($flip = false)
113
    {
114 372
        return self::all($flip);
115
    }
116
117
    /**
118
     * Get translated levels.
119
     *
120
     * @param  string|null  $locale
121
     *
122
     * @return array
123
     */
124 204
    public function names($locale = null)
125
    {
126 204
        $levels = self::all(true);
127
128 204
        array_walk($levels, function (&$name, $level) use ($locale) {
129 204
            $name = $this->get($level, $locale);
130 204
        });
131
132 204
        return $levels;
133
    }
134
135
    /**
136
     * Get PSR log levels.
137
     *
138
     * @param  bool|false  $flip
139
     *
140
     * @return array
141
     */
142 936
    public static function all($flip = false)
143
    {
144 936
        if (empty(self::$levels)) {
145 12
            $class        = new ReflectionClass(new LogLevel);
146 12
            self::$levels = $class->getConstants();
147 9
        }
148
149 936
        return $flip ? array_flip(self::$levels) : self::$levels;
150
    }
151
152
    /**
153
     * Get the translated level.
154
     *
155
     * @param  string       $key
156
     * @param  string|null  $locale
157
     *
158
     * @return string
159
     */
160 264
    public function get($key, $locale = null)
161
    {
162 264
        if (is_null($locale) || $locale === 'auto') {
163 108
            $locale = $this->getLocale();
164 81
        }
165
166 264
        return $this->translator->get("log-viewer::levels.$key", [], $locale);
167
    }
168
}
169