Skip to content

Commit 4e1ca2e

Browse files
committed
refactor: name variable
1 parent ab714f9 commit 4e1ca2e

File tree

2 files changed

+29
-45
lines changed

2 files changed

+29
-45
lines changed

src/Processors/AbstractProcessor.php

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,36 @@
44

55
abstract class AbstractProcessor
66
{
7-
private string $fileOut;
7+
protected string $filenameSource;
88

9-
protected string $fileOriginal;
9+
protected string $extension = '';
1010

11-
protected string $suffix = '';
12-
13-
abstract public function handler($file): ?string;
11+
abstract public function handler(string $filename): ?string;
1412

1513
public function __construct()
1614
{
1715
clearstatcache();
1816
}
1917

20-
public function compress(): void
18+
public function addExtension(string $extension): void
2119
{
22-
$this->suffix = '.gz';
20+
$this->extension = '.'.$extension;
2321
}
2422

25-
public function setFileOriginal($fileOriginal): self
23+
public function removeExtention(string $extension): void
2624
{
27-
$this->fileOriginal = $fileOriginal;
28-
29-
return $this;
25+
$this->extension = str_replace('.'.$extension, '', $this->extension);
3026
}
3127

32-
protected function processed($file): ?string
28+
public function setFilenameSource($filenameSource): self
3329
{
34-
if (is_file($file)) {
35-
$this->fileOut = $file;
30+
$this->filenameSource = $filenameSource;
3631

37-
return $this->fileOut;
38-
}
32+
return $this;
33+
}
3934

40-
return null;
35+
protected function processed(string $filename): ?string
36+
{
37+
return is_file($filename) ? $filename : null;
4138
}
4239
}

src/Processors/RotativeProcessor.php

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ class RotativeProcessor extends AbstractProcessor
77
private $maxFiles = 366;
88

99
/**
10-
* Log files are rotated count times before being removed
11-
*
12-
* @param int $count
13-
* @return self
10+
* Log files are rotated count times before being removed.
1411
*/
1512
public function files(int $count): self
1613
{
@@ -19,47 +16,37 @@ public function files(int $count): self
1916
return $this;
2017
}
2118

22-
public function handler($file): ?string
19+
public function handler(string $filename): ?string
2320
{
24-
$nextFile = "{$this->fileOriginal}.1";
21+
$filenameTarget = "{$this->filenameSource}.1";
2522

2623
$this->rotate();
2724

28-
rename($file, $nextFile);
25+
rename($filename, $filenameTarget);
2926

30-
return $this->processed($nextFile);
27+
return $this->processed($filenameTarget);
3128
}
3229

3330
private function rotate(int $number = 1): string
3431
{
35-
$file = "{$this->fileOriginal}.{$number}{$this->suffix}";
32+
$filenameTarget = "{$this->filenameSource}.{$number}{$this->extension}";
3633

37-
if (!file_exists($file)) {
38-
return "{$this->fileOriginal}.{$number}{$this->suffix}";
34+
if (!file_exists($filenameTarget)) {
35+
return $filenameTarget;
3936
}
4037

41-
if ($this->maxFiles > 0 && $number >= $this->maxFiles ) {
42-
if (file_exists($file)) {
43-
unlink($file);
38+
if ($this->maxFiles > 0 && $number >= $this->maxFiles) {
39+
if (file_exists($filenameTarget)) {
40+
unlink($filenameTarget);
4441
}
4542

46-
return "{$this->fileOriginal}.{$number}{$this->suffix}";
43+
return $filenameTarget;
4744
}
4845

49-
$nextFile = $this->rotate($number + 1);
46+
$nextFilename = $this->rotate($number + 1);
5047

51-
rename($file, $nextFile);
48+
rename($filenameTarget, $nextFilename);
5249

53-
return "{$this->fileOriginal}.{$number}{$this->suffix}";
54-
}
55-
56-
private function getnumber(string $file): ?int
57-
{
58-
$fileName = basename($file);
59-
$fileOriginaleName = basename($this->fileOriginal);
60-
61-
preg_match("/{$fileOriginaleName}.([0-9]+){$this->suffix}/", $fileName, $output);
62-
63-
return $output[1] ?? null;
50+
return $filenameTarget;
6451
}
6552
}

0 commit comments

Comments
 (0)