Skip to content

Commit ee4af2a

Browse files
committed
finally and refactor
1 parent 8edb7cf commit ee4af2a

File tree

6 files changed

+112
-112
lines changed

6 files changed

+112
-112
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ $rotation = new Rotation([
5050
'truncate' => false,
5151
'then' => function ($filename) {},
5252
'catch' => function (RotationFailed $exception) {},
53+
'finally' => function ($message, $filename) {},
5354
]);
5455

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

src/ErrorHandler.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,26 @@
77

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

12-
protected $finallyCallback = null;
14+
private $finallyCallback = null;
1315

1416
private ?string $_filename = null;
1517

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+
1630
/**
1731
* Call function if roteted catch any Exception.
1832
*/
@@ -23,11 +37,32 @@ public function catch(callable $callable): self
2337
return $this;
2438
}
2539

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+
2650
protected function setFilename(string $filename): void
2751
{
2852
$this->_filename = $filename;
2953
}
3054

55+
private function sucessfull(string $filenameSource, ?string $filenameRotated): void
56+
{
57+
$this->finished($filenameRotated, 'sucessfull');
58+
59+
if (is_null($this->thenCallback) || is_null($filenameRotated)) {
60+
return;
61+
}
62+
63+
call_user_func($this->thenCallback, $filenameRotated, $filenameSource);
64+
}
65+
3166
protected function exception(Throwable $exception): self
3267
{
3368
$this->finished($this->_filename, $exception->getMessage());
@@ -41,6 +76,7 @@ protected function exception(Throwable $exception): self
4176
return $this;
4277
}
4378

79+
4480
protected function finished(string $filenameSource, string $message): void
4581
{
4682
if (is_null($this->finallyCallback)) {

src/Rotation.php

Lines changed: 0 additions & 35 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();
@@ -88,28 +86,6 @@ public function minSize(int $bytes): self
8886
return $this;
8987
}
9088

91-
/**
92-
* Function that will be executed when the rotation is successful.
93-
* The first argument will be the name of the destination file and
94-
* the second the name of the rotated file.
95-
*/
96-
public function then(callable $callable): self
97-
{
98-
$this->thenCallback = $callable;
99-
100-
return $this;
101-
}
102-
103-
/**
104-
* Function that will be executed when the process was finished.
105-
*/
106-
public function finally(callable $callable): self
107-
{
108-
$this->finallyCallback = $callable;
109-
110-
return $this;
111-
}
112-
11389
/**
11490
* Rotate file.
11591
*
@@ -178,17 +154,6 @@ private function runCompress(string $filename): ?string
178154
}
179155
}
180156

181-
private function sucessfull(string $filenameSource, ?string $filenameRotated): void
182-
{
183-
if (is_null($this->thenCallback) || is_null($filenameRotated)) {
184-
return;
185-
}
186-
187-
call_user_func($this->thenCallback, $filenameRotated, $filenameSource);
188-
189-
$this->finished($filenameRotated, 'sucessfull');
190-
}
191-
192157
/**
193158
* check if file need rotate.
194159
*/

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/test_load/run.php

Lines changed: 0 additions & 63 deletions
This file was deleted.

tests/test_load/writelog.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)