RotativeProcessor   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 17
c 3
b 0
f 0
dl 0
loc 44
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handler() 0 9 1
A files() 0 5 1
A rotate() 0 19 4
1
<?php
2
3
namespace Cesargb\Log\Processors;
4
5
class RotativeProcessor extends AbstractProcessor
6
{
7
    private int $maxFiles = 366;
8
9
    /**
10
     * Log files are rotated count times before being removed.
11
     */
12
    public function files(int $count): self
13
    {
14
        $this->maxFiles = $count;
15
16
        return $this;
17
    }
18
19
    public function handler(string $filename): ?string
20
    {
21
        $filenameTarget = "{$this->filenameSource}.1";
22
23
        $this->rotate();
24
25
        rename($filename, $filenameTarget);
26
27
        return $this->processed($filenameTarget);
28
    }
29
30
    private function rotate(int $number = 1): string
31
    {
32
        $filenameTarget = "{$this->filenameSource}.{$number}{$this->extension}";
33
34
        if (!file_exists($filenameTarget)) {
35
            return $filenameTarget;
36
        }
37
38
        if ($this->maxFiles > 0 && $number >= $this->maxFiles) {
39
            unlink($filenameTarget);
40
41
            return $filenameTarget;
42
        }
43
44
        $nextFilename = $this->rotate($number + 1);
45
46
        rename($filenameTarget, $nextFilename);
47
48
        return $filenameTarget;
49
    }
50
}
51