Rotate   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 16
c 1
b 0
f 0
dl 0
loc 35
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A file() 0 9 2
A files() 0 4 1
A buildRotateDefault() 0 15 1
1
<?php
2
3
namespace Cesargb\LaravelLog;
4
5
use Cesargb\LaravelLog\Events\RotateHasFailed;
6
use Cesargb\LaravelLog\Events\RotateWasSuccessful;
7
use Cesargb\LaravelLog\Helpers\Log;
8
use Cesargb\Log\Rotation;
9
10
class Rotate
11
{
12
    public function files(array $filenames)
13
    {
14
        array_walk($filenames, function ($filename) {
15
            $this->file($filename);
16
        });
17
    }
18
19
    public function file(string $filename, array $options = []): bool
20
    {
21
        $result = $this->buildRotateDefault($options)->rotate($filename);
22
23
        if ($result) {
24
            Log::closeHandlers();
25
        }
26
27
        return $result;
28
    }
29
30
    private function buildRotateDefault(array $options = []): Rotation
31
    {
32
        return new Rotation(array_merge(
33
            [
34
                'files' => config('rotate.log_max_files', 366),
35
                'compress' => config('rotate.log_compress_files', true),
36
                'then' => function ($filenameRotated, $filename) {
37
                    event(new RotateWasSuccessful($filename, $filenameRotated));
38
                },
39
                'truncate' => config('rotate.truncate', true),
40
                'catch' => function ($error) {
41
                    event(new RotateHasFailed('', $error));
42
                },
43
            ],
44
            $options
45
        ));
46
    }
47
}
48