Skip to content

Commit b1555c7

Browse files
committed
optionable
1 parent 64edf61 commit b1555c7

File tree

4 files changed

+117
-26
lines changed

4 files changed

+117
-26
lines changed

src/ErrorHandler.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@
44

55
use Throwable;
66

7-
class ErrorHandler
7+
trait ErrorHandler
88
{
99
private $catchCallable = null;
1010

11+
/**
12+
* Call function if roteted catch any Exception.
13+
*
14+
* @param callable $callable
15+
* @return self
16+
*/
1117
public function catch(callable $callable): self
1218
{
1319
$this->catchCallable = $callable;

src/Optionable.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Cesargb\Log;
4+
5+
use LogicException;
6+
7+
trait Optionable
8+
{
9+
private $validMethods = [];
10+
11+
/**
12+
* Set options
13+
*
14+
* @param array $options
15+
* @throws LogicException
16+
* @return self
17+
*/
18+
public function options(array $options): self
19+
{
20+
foreach ($options as $key => $value) {
21+
$this->setMethod($key, $value);
22+
}
23+
24+
return $this;
25+
}
26+
27+
protected function methodsOptionables(array $methods): self
28+
{
29+
$this->validMethods = $methods;
30+
31+
return $this;
32+
}
33+
34+
private function setMethod($key, $value)
35+
{
36+
$method = $this->convert($key);
37+
38+
if (in_array($method, $this->validMethods)) {
39+
$this->{$method}($value);
40+
} else {
41+
throw new LogicException("option {$key} is not valid.", 30);
42+
}
43+
}
44+
45+
private function convert(string $key): string
46+
{
47+
return lcfirst(
48+
str_replace(
49+
' ',
50+
'',
51+
ucwords(str_replace(['-', '_'], ' ', $key))
52+
)
53+
);
54+
}
55+
56+
57+
}

src/Rotation.php

+21-25
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
class Rotation
1010
{
11+
use Optionable, ErrorHandler;
12+
1113
private RotativeProcessor $processor;
1214

1315
private bool $_compress = false;
@@ -16,13 +18,19 @@ class Rotation
1618

1719
private $thenCallback = null;
1820

19-
private $errorHandler = null;
20-
21-
public function __construct()
21+
public function __construct(array $options = [])
2222
{
2323
$this->processor = new RotativeProcessor();
2424

25-
$this->errorHandler = new ErrorHandler();
25+
$this->methodsOptionables([
26+
'compress',
27+
'minSize',
28+
'files',
29+
'then',
30+
'catch',
31+
]);
32+
33+
$this->options($options);
2634
}
2735

2836
/**
@@ -41,11 +49,12 @@ public function files(int $count): self
4149
/**
4250
* Old versions of log files are compressed
4351
*
52+
*@param bool $compress
4453
* @return self
4554
*/
46-
public function compress(): self
55+
public function compress(bool $compress = true): self
4756
{
48-
$this->_compress = true;
57+
$this->_compress = $compress;
4958

5059
$this->processor->compress();
5160

@@ -79,19 +88,6 @@ public function then(callable $callable): self
7988
return $this;
8089
}
8190

82-
/**
83-
* Call function if roteted catch any Exception.
84-
*
85-
* @param callable $callable
86-
* @return self
87-
*/
88-
public function catch(callable $callable): self
89-
{
90-
$this->errorHandler->catch($callable);
91-
92-
return $this;
93-
}
94-
9591
/**
9692
* Rotate file
9793
*
@@ -114,7 +110,7 @@ public function rotate(string $file): bool
114110
try {
115111
$fileRotated = $gz->handler($fileRotated);
116112
} catch (Exception $error) {
117-
$this->errorHandler->exception($error);
113+
$this->exception($error);
118114

119115
$fileRotated = null;
120116
}
@@ -157,7 +153,7 @@ private function runProcessor(string $originalFile, ?string $fileRotated): ?stri
157153
private function canRotate(string $file): bool
158154
{
159155
if (! $this->fileIsValid($file)) {
160-
$this->errorHandler->exception(
156+
$this->exception(
161157
new Exception(sprintf('the file %s not is valid.', $file), 10)
162158
);
163159

@@ -204,7 +200,7 @@ private function moveContentToTempFile(string $file): ?string
204200
$fd = fopen($file, 'r+');
205201

206202
if (! $fd) {
207-
$this->errorHandler->exception(
203+
$this->exception(
208204
new Exception(sprintf('the file %s not can open.', $file), 20)
209205
);
210206

@@ -214,7 +210,7 @@ private function moveContentToTempFile(string $file): ?string
214210
if (! flock($fd, LOCK_EX)) {
215211
fclose($fd);
216212

217-
$this->errorHandler->exception(
213+
$this->exception(
218214
new Exception(sprintf('the file %s not can lock.', $file), 21)
219215
);
220216

@@ -224,7 +220,7 @@ private function moveContentToTempFile(string $file): ?string
224220
if (! copy($file, $fileDestination)) {
225221
fclose($fd);
226222

227-
$this->errorHandler->exception(
223+
$this->exception(
228224
new Exception(
229225
sprintf('the file %s not can copy to temp file %s.', $file, $fileDestination),
230226
22
@@ -239,7 +235,7 @@ private function moveContentToTempFile(string $file): ?string
239235

240236
unlink($fileDestination);
241237

242-
$this->errorHandler->exception(
238+
$this->exception(
243239
new Exception(sprintf('the file %s not can truncate.', $file), 23)
244240
);
245241

tests/OptionTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Cesargb\Log\Test;
4+
5+
use Exception;
6+
use Cesargb\Log\Rotation;
7+
use Cesargb\Log\Test\TestCase;
8+
9+
class OptionTest extends TestCase
10+
{
11+
public function test_pass_options()
12+
{
13+
$rotation = new Rotation([
14+
'files' => 1,
15+
'compress' => true,
16+
'min-size' => 10,
17+
'then' => function ($filename) {},
18+
'catch' => function ($error) {},
19+
]);
20+
21+
$this->assertNotNull($rotation);
22+
}
23+
24+
public function test_catch_exceptio_if_method_is_not_permited()
25+
{
26+
$this->expectException(\LogicException::class);
27+
28+
$r = new Rotation([
29+
'bad-method' => null,
30+
]);
31+
}
32+
}

0 commit comments

Comments
 (0)