Skip to content

Commit 066355e

Browse files
authored
Merge pull request #13 from cesargb/finally
Add option finally
2 parents abfb735 + 070e742 commit 066355e

File tree

5 files changed

+129
-23
lines changed

5 files changed

+129
-23
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ $rotation
3434
->truncate() // Optional, truncate the original log file in place after creating a copy, instead of moving the old log file.
3535
->then(function ($filenameTarget, $filenameRotated) {}) // Optional, to get filename target and original filename
3636
->catch(function (RotationFailed $exception) {}) // Optional, to catch a exception in rotating
37+
->finally(function ($message, $filenameTarget) {}) // Optional, this method will be called when the process has finished
3738
->rotate('file.log');
3839
```
3940

@@ -50,6 +51,7 @@ $rotation = new Rotation([
5051
'truncate' => false,
5152
'then' => function ($filename) {},
5253
'catch' => function (RotationFailed $exception) {},
54+
'finally' => function ($message, $filename) {},
5355
]);
5456

5557
$rotation->rotate('file.log');

src/ErrorHandler.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,26 @@
77

88
trait ErrorHandler
99
{
10+
private $thenCallback = null;
11+
1012
private $catchCallable = null;
1113

14+
private $finallyCallback = null;
15+
1216
private ?string $_filename = null;
1317

18+
/**
19+
* Function that will be executed when the rotation is successful.
20+
* The first argument will be the name of the destination file and
21+
* the second the name of the rotated file.
22+
*/
23+
public function then(callable $callable): self
24+
{
25+
$this->thenCallback = $callable;
26+
27+
return $this;
28+
}
29+
1430
/**
1531
* Call function if roteted catch any Exception.
1632
*/
@@ -21,13 +37,36 @@ public function catch(callable $callable): self
2137
return $this;
2238
}
2339

40+
/**
41+
* Function that will be executed when the process was finished.
42+
*/
43+
public function finally(callable $callable): self
44+
{
45+
$this->finallyCallback = $callable;
46+
47+
return $this;
48+
}
49+
2450
protected function setFilename(string $filename): void
2551
{
2652
$this->_filename = $filename;
2753
}
2854

55+
private function sucessfull(string $filenameSource, ?string $filenameRotated): void
56+
{
57+
$this->finished('sucessfull', $filenameSource);
58+
59+
if (is_null($this->thenCallback) || is_null($filenameRotated)) {
60+
return;
61+
}
62+
63+
call_user_func($this->thenCallback, $filenameRotated, $filenameSource);
64+
}
65+
2966
protected function exception(Throwable $exception): self
3067
{
68+
$this->finished($exception->getMessage(), $this->_filename);
69+
3170
if ($this->catchCallable) {
3271
call_user_func($this->catchCallable, $this->convertException($exception));
3372
} else {
@@ -37,6 +76,16 @@ protected function exception(Throwable $exception): self
3776
return $this;
3877
}
3978

79+
80+
protected function finished(string $message, ?string $filenameSource): void
81+
{
82+
if (is_null($this->finallyCallback)) {
83+
return;
84+
}
85+
86+
call_user_func($this->finallyCallback, $message, $filenameSource);
87+
}
88+
4089
private function convertException(Throwable $exception): RotationFailed
4190
{
4291
return new RotationFailed(

src/Rotation.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ class Rotation
1919

2020
private bool $_truncate = false;
2121

22-
private $thenCallback = null;
23-
2422
public function __construct(array $options = [])
2523
{
2624
$this->processor = new RotativeProcessor();
@@ -32,6 +30,7 @@ public function __construct(array $options = [])
3230
'files',
3331
'then',
3432
'catch',
33+
'finally',
3534
]);
3635

3736
$this->options($options);
@@ -87,18 +86,6 @@ public function minSize(int $bytes): self
8786
return $this;
8887
}
8988

90-
/**
91-
* Function that will be executed when the rotation is successful.
92-
* The first argument will be the name of the destination file and
93-
* the second the name of the rotated file.
94-
*/
95-
public function then(callable $callable): self
96-
{
97-
$this->thenCallback = $callable;
98-
99-
return $this;
100-
}
101-
10289
/**
10390
* Rotate file.
10491
*
@@ -167,21 +154,14 @@ private function runCompress(string $filename): ?string
167154
}
168155
}
169156

170-
private function sucessfull(string $filenameSource, ?string $filenameRotated): void
171-
{
172-
if (is_null($this->thenCallback) || is_null($filenameRotated)) {
173-
return;
174-
}
175-
176-
call_user_func($this->thenCallback, $filenameRotated, $filenameSource);
177-
}
178-
179157
/**
180158
* check if file need rotate.
181159
*/
182160
private function canRotate(string $filename): bool
183161
{
184162
if (!file_exists($filename)) {
163+
$this->finished(sprintf('the file %s not exists.', $filename), $filename);
164+
185165
return false;
186166
}
187167

tests/ErrorHandlerTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,34 @@
77

88
class ErrorHandlerTest extends TestCase
99
{
10+
public function testCallThenIfRotateWasSucessfull()
11+
{
12+
file_put_contents(self::DIR_WORK.'file.log', microtime(true));
13+
14+
$rotation = new Rotation();
15+
16+
$thenCalled = false;
17+
18+
$rotation->then(function () use (&$thenCalled) {
19+
$thenCalled = true;
20+
})->rotate(self::DIR_WORK.'file.log');
21+
22+
$this->assertTrue($thenCalled);
23+
}
24+
25+
public function testNotCallThenIfRotateNotWasSucessfull()
26+
{
27+
$rotation = new Rotation();
28+
29+
$thenCalled = false;
30+
31+
$rotation->then(function () use (&$thenCalled) {
32+
$thenCalled = true;
33+
})->rotate(self::DIR_WORK.'file.log');
34+
35+
$this->assertFalse($thenCalled);
36+
}
37+
1038
public function testThrowsException()
1139
{
1240
$this->expectException(RotationFailed::class);
@@ -39,4 +67,50 @@ public function testCatchException()
3967

4068
$this->assertFalse($result);
4169
}
70+
71+
public function testCallFinallyIfRotateWasSucessfull()
72+
{
73+
file_put_contents(self::DIR_WORK.'file.log', microtime(true));
74+
75+
$rotation = new Rotation();
76+
77+
$finallyCalled = false;
78+
79+
$rotation->finally(function () use (&$finallyCalled) {
80+
$finallyCalled = true;
81+
})->rotate(self::DIR_WORK.'file.log');
82+
83+
$this->assertTrue($finallyCalled);
84+
}
85+
86+
public function testCallFinallyIfFileDontExists()
87+
{
88+
$rotation = new Rotation();
89+
90+
$finallyCalled = false;
91+
92+
$rotation->finally(function () use (&$finallyCalled) {
93+
$finallyCalled = true;
94+
})->rotate(self::DIR_WORK.'file.log');
95+
96+
$this->assertTrue($finallyCalled);
97+
}
98+
99+
public function testCallFinallyIfThrowException()
100+
{
101+
$this->expectException(RotationFailed::class);
102+
103+
$rotation = new Rotation();
104+
105+
touch(self::DIR_WORK.'/file.log');
106+
chmod(self::DIR_WORK.'/file.log', 0444);
107+
108+
$finallyCalled = false;
109+
110+
$rotation->finally(function () use (&$finallyCalled) {
111+
$finallyCalled = true;
112+
})->rotate(self::DIR_WORK.'file.log');
113+
114+
$this->assertTrue($finallyCalled);
115+
}
42116
}

tests/OptionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function testPassOptions()
1515
'truncate' => false,
1616
'then' => function ($filename) {},
1717
'catch' => function ($error) {},
18+
'finally' => function ($message) {},
1819
]);
1920

2021
$this->assertNotNull($rotation);

0 commit comments

Comments
 (0)