Passed
Push — master ( a76d1f...2241b9 )
by Cesar
02:25
created

RotativeHandler::getPatternGlob()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 15
rs 10
1
<?php
2
3
namespace Cesargb\File\Rotate\Handlers;
4
5
use Cesargb\File\Rotate\Events\RotateHasFailed;
6
use Cesargb\File\Rotate\Events\RotateWasSuccessful;
7
8
class RotativeHandler extends AbstractHandler
9
{
10
    const EXTENSION_COMPRESS = 'gz';
11
12
    protected $max_files;
13
14
    public function __construct($file, $max_files = 0, bool $compress = true, $dir_to_archive = null)
15
    {
16
        parent::__construct($file, $compress, $dir_to_archive);
17
18
        $this->max_files = $max_files;
19
    }
20
21
    public function run()
22
    {
23
        if (! $this->validate()) {
24
            return false;
25
        }
26
27
        $this->file_rotated = $this->rebaseArchiveDir($this->getRotatedFileName());
28
29
        if ($this->rotate()) {
30
            $this->close();
31
32
            event(new RotateWasSuccessful($this->file, $this->file_rotated));
33
34
            return true;
35
        } else {
36
            event(new RotateHasFailed($this->file, new \Exception('Failed to move file '.$this->file.' to '.$this->file_rotated)));
37
38
            return false;
39
        }
40
    }
41
42
    protected function rotate()
43
    {
44
        if ($this->compress) {
45
            return $this->moveData($this->file, $this->file_rotated);
46
        }
47
48
        $file_tmp_name = tempnam(dirname($this->file), 'laravel_log_rotate');
49
50
        if ($this->moveData($this->file, $file_tmp_name)) {
51
            $fd_tmp = fopen($file_tmp_name, 'r');
52
53
            if ($fd_tmp) {
0 ignored issues
show
introduced by
$fd_tmp is of type false|resource, thus it always evaluated to false.
Loading history...
54
                $fd_compress = gzopen($this->file_rotated, 'w');
55
56
                while (! feof($fd_tmp)) {
57
                    gzwrite($fd_compress, fread($fd_tmp, 1024 * 512));
58
                }
59
60
                gzclose($fd_compress);
61
                fclose($fd_tmp);
62
63
                unlink($file_tmp_name);
64
65
                return true;
66
            }
67
        }
68
69
        return false;
70
    }
71
72
    protected function getRotatedFileName()
73
    {
74
        $patternGlob = $this->getPatternGlob(pathinfo($this->file));
75
76
        $curFiles = glob($patternGlob);
77
78
        for ($n = count($curFiles); $n > 0; $n--) {
0 ignored issues
show
Bug introduced by
It seems like $curFiles can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
        for ($n = count(/** @scrutinizer ignore-type */ $curFiles); $n > 0; $n--) {
Loading history...
79
            $file_to_move = str_replace('*', $n, $patternGlob);
80
81
            if (file_exists($file_to_move)) {
82
                if ($this->max_files > 0 && $n >= $this->max_files) {
83
                    unlink($file_to_move);
84
                } else {
85
                    rename($file_to_move, str_replace('*', $n + 1, $patternGlob));
86
                }
87
            }
88
        }
89
90
        return str_replace('*', '1', $patternGlob);
91
    }
92
93
    private function getPatternGlob($fileInfo): string
94
    {
95
        $patternGlob = $fileInfo['dirname'].'/'.$fileInfo['filename'];
96
97
        if (! empty($fileInfo['extension'])) {
98
            $patternGlob .= '.'.$fileInfo['extension'];
99
        }
100
101
        $patternGlob .= '.*';
102
103
        if ($this->compress) {
104
            $patternGlob .= '.'.self::EXTENSION_COMPRESS;
105
        }
106
107
        return $patternGlob;
108
    }
109
}
110