Completed
Push — master ( bfb20e...180eae )
by ARCANEDEV
13s queued 11s
created

LogChecker::isInvalidLogPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php namespace Arcanedev\LogViewer\Utilities;
2
3
use Arcanedev\LogViewer\Contracts\Utilities\Filesystem as FilesystemContract;
4
use Arcanedev\LogViewer\Contracts\Utilities\LogChecker as LogCheckerContract;
5
use Illuminate\Contracts\Config\Repository as ConfigContract;
6
7
/**
8
 * Class     LogChecker
9
 *
10
 * @package  Arcanedev\LogViewer\Utilities
11
 * @author   ARCANEDEV <[email protected]>
12
 *
13
 * @todo     Adding the translation or not ??
14
 */
15
class LogChecker implements LogCheckerContract
16
{
17
    /* -----------------------------------------------------------------
18
     |  Properties
19
     | -----------------------------------------------------------------
20
     */
21
22
    /**
23
     * The config repository instance.
24
     *
25
     * @var \Illuminate\Contracts\Config\Repository
26
     */
27
    private $config;
28
29
    /**
30
     * The filesystem instance.
31
     *
32
     * @var \Arcanedev\LogViewer\Contracts\Utilities\Filesystem
33
     */
34
    private $filesystem;
35
36
    /**
37
     * Log handler mode.
38
     *
39
     * @var string
40
     */
41
    protected $handler = '';
42
43
    /**
44
     * The check status.
45
     *
46
     * @var bool
47
     */
48
    private $status = true;
49
50
    /**
51
     * The check messages.
52
     *
53
     * @var array
54
     */
55
    private $messages;
56
57
    /**
58
     * Log files statuses.
59
     *
60
     * @var array
61
     */
62
    private $files = [];
63
64
    /* -----------------------------------------------------------------
65
     |  Constructor
66
     | -----------------------------------------------------------------
67
     */
68
69
    /**
70
     * LogChecker constructor.
71
     *
72
     * @param  \Illuminate\Contracts\Config\Repository              $config
73
     * @param  \Arcanedev\LogViewer\Contracts\Utilities\Filesystem  $filesystem
74
     */
75 12
    public function __construct(ConfigContract $config, FilesystemContract $filesystem)
76
    {
77 12
        $this->setConfig($config);
78 12
        $this->setFilesystem($filesystem);
79 12
        $this->refresh();
80 12
    }
81
82
    /* -----------------------------------------------------------------
83
     |  Getters & Setters
84
     | -----------------------------------------------------------------
85
     */
86
87
    /**
88
     * Set the config instance.
89
     *
90
     * @param  \Illuminate\Contracts\Config\Repository  $config
91
     *
92
     * @return self
93
     */
94 12
    public function setConfig(ConfigContract $config)
95
    {
96 12
        $this->config = $config;
97
98 12
        return $this;
99
    }
100
101
    /**
102
     * Set the Filesystem instance.
103
     *
104
     * @param  \Arcanedev\LogViewer\Contracts\Utilities\Filesystem  $filesystem
105
     *
106
     * @return self
107
     */
108 12
    public function setFilesystem(FilesystemContract $filesystem)
109
    {
110 12
        $this->filesystem = $filesystem;
111
112 12
        return $this;
113
    }
114
115
    /**
116
     * Set the log handler mode.
117
     *
118
     * @param  string  $handler
119
     *
120
     * @return self
121
     */
122 12
    protected function setHandler($handler)
123
    {
124 12
        $this->handler = strtolower($handler);
125
126 12
        return $this;
127
    }
128
129
    /* -----------------------------------------------------------------
130
     |  Main Methods
131
     | -----------------------------------------------------------------
132
     */
133
134
    /**
135
     * Get messages.
136
     *
137
     * @return array
138
     */
139 4
    public function messages()
140
    {
141 4
        $this->refresh();
142
143 4
        return $this->messages;
144
    }
145
146
    /**
147
     * Check if the checker passes.
148
     *
149
     * @return bool
150
     */
151 2
    public function passes()
152
    {
153 2
        $this->refresh();
154
155 2
        return $this->status;
156
    }
157
158
    /**
159
     * Check if the checker fails.
160
     *
161
     * @return bool
162
     */
163 2
    public function fails()
164
    {
165 2
        return ! $this->passes();
166
    }
167
168
    /**
169
     * Get the requirements.
170
     *
171
     * @return array
172
     */
173 6
    public function requirements()
174
    {
175 6
        $this->refresh();
176
177 6
        return $this->isDaily() ? [
178 4
            'status'  => 'success',
179
            'header'  => 'Application requirements fulfilled.',
180
            'message' => 'Are you ready to rock ?',
181
        ] : [
182 2
            'status'  => 'failed',
183 2
            'header'  => 'Application requirements failed.',
184 6
            'message' => $this->messages['handler']
185
        ];
186
    }
187
188
    /* -----------------------------------------------------------------
189
     |  Check Methods
190
     | -----------------------------------------------------------------
191
     */
192
193
    /**
194
     * Is a daily handler mode ?
195
     *
196
     * @return bool
197
     */
198 12
    protected function isDaily()
199
    {
200 12
        return $this->isSameHandler(self::HANDLER_DAILY);
201
    }
202
203
    /**
204
     * Is the handler is the same as the application log handler.
205
     *
206
     * @param  string  $handler
207
     *
208
     * @return bool
209
     */
210 12
    private function isSameHandler($handler)
211
    {
212 12
        return $this->handler === $handler;
213
    }
214
215
    /* -----------------------------------------------------------------
216
     |  Other Methods
217
     | -----------------------------------------------------------------
218
     */
219
220
    /**
221
     * Refresh the checks.
222
     *
223
     * @return \Arcanedev\LogViewer\Utilities\LogChecker
224
     */
225 12
    private function refresh()
226
    {
227 12
        $this->setHandler($this->config->get('logging.default', 'stack'));
228
229 12
        $this->messages = [
230
            'handler' => '',
231
            'files'   => [],
232
        ];
233 12
        $this->files    = [];
234
235 12
        $this->checkHandler();
236 12
        $this->checkLogFiles();
237
238 12
        return $this;
239
    }
240
241
    /**
242
     * Check the handler mode.
243
     */
244 12
    private function checkHandler()
245
    {
246 12
        if ($this->isDaily()) return;
247
248 2
        $this->messages['handler'] = 'You should set the log handler to `daily` mode. Please check the LogViewer wiki page (Requirements) for more details.';
249 2
    }
250
251
    /**
252
     * Check all log files.
253
     */
254 12
    private function checkLogFiles()
255
    {
256 12
        foreach ($this->filesystem->all() as $path) {
257 12
            $this->checkLogFile($path);
258
        }
259 12
    }
260
261
    /**
262
     * Check a log file.
263
     *
264
     * @param  string  $path
265
     */
266 12
    private function checkLogFile($path)
267
    {
268 12
        $status   = true;
269 12
        $filename = basename($path);
270 12
        $message  = "The log file [$filename] is valid.";
271
        $pattern  = $this->filesystem->getPattern();
272 12
273 12
        if ($this->isSingleLogFile($filename)) {
274 12
            $this->status = $status = false;
275 12
            $this->messages['files'][$filename] = $message =
276
                "You have a single log file in your application, you should split the [$filename] into separate log files.";
277 12
        }
278 12
        elseif ($this->isInvalidLogPattern($filename, $pattern)) {
279 12
            $this->status = $status = false;
280 12
            $this->messages['files'][$filename] = $message =
281
                "The log file [$filename] has an invalid date, the format must be like {$pattern}.";
282
        }
283 12
284 12
        $this->files[$filename] = compact('filename', 'status', 'message', 'path');
285
    }
286
287
    /**
288
     * Check if it's not a single log file.
289
     *
290
     * @param  string  $file
291
     *
292
     * @return bool
293 12
     */
294
    private function isSingleLogFile($file)
295 12
    {
296
        return $file === 'laravel.log';
297
    }
298
299
    /**
300
     * Check the date of the log file.
301
     *
302
     * @param  string  $file
303
     * @param  string  $pattern
304
     *
305 12
     * @return bool
306
     */
307 12
    private function isInvalidLogPattern($file, $pattern)
308
    {
309 12
        return ((bool) preg_match("/{$pattern}/", $file, $matches)) === false;
310
    }
311
}
312