8
8
9
9
class Rotation
10
10
{
11
+ use Optionable, ErrorHandler;
12
+
11
13
private RotativeProcessor $ processor ;
12
14
13
15
private bool $ _compress = false ;
@@ -16,13 +18,19 @@ class Rotation
16
18
17
19
private $ thenCallback = null ;
18
20
19
- private $ errorHandler = null ;
20
-
21
- public function __construct ()
21
+ public function __construct (array $ options = [])
22
22
{
23
23
$ this ->processor = new RotativeProcessor ();
24
24
25
- $ this ->errorHandler = new ErrorHandler ();
25
+ $ this ->methodsOptionables ([
26
+ 'compress ' ,
27
+ 'minSize ' ,
28
+ 'files ' ,
29
+ 'then ' ,
30
+ 'catch ' ,
31
+ ]);
32
+
33
+ $ this ->options ($ options );
26
34
}
27
35
28
36
/**
@@ -41,11 +49,12 @@ public function files(int $count): self
41
49
/**
42
50
* Old versions of log files are compressed
43
51
*
52
+ *@param bool $compress
44
53
* @return self
45
54
*/
46
- public function compress (): self
55
+ public function compress (bool $ compress = true ): self
47
56
{
48
- $ this ->_compress = true ;
57
+ $ this ->_compress = $ compress ;
49
58
50
59
$ this ->processor ->compress ();
51
60
@@ -79,19 +88,6 @@ public function then(callable $callable): self
79
88
return $ this ;
80
89
}
81
90
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
-
95
91
/**
96
92
* Rotate file
97
93
*
@@ -114,7 +110,7 @@ public function rotate(string $file): bool
114
110
try {
115
111
$ fileRotated = $ gz ->handler ($ fileRotated );
116
112
} catch (Exception $ error ) {
117
- $ this ->errorHandler -> exception ($ error );
113
+ $ this ->exception ($ error );
118
114
119
115
$ fileRotated = null ;
120
116
}
@@ -157,7 +153,7 @@ private function runProcessor(string $originalFile, ?string $fileRotated): ?stri
157
153
private function canRotate (string $ file ): bool
158
154
{
159
155
if (! $ this ->fileIsValid ($ file )) {
160
- $ this ->errorHandler -> exception (
156
+ $ this ->exception (
161
157
new Exception (sprintf ('the file %s not is valid. ' , $ file ), 10 )
162
158
);
163
159
@@ -204,7 +200,7 @@ private function moveContentToTempFile(string $file): ?string
204
200
$ fd = fopen ($ file , 'r+ ' );
205
201
206
202
if (! $ fd ) {
207
- $ this ->errorHandler -> exception (
203
+ $ this ->exception (
208
204
new Exception (sprintf ('the file %s not can open. ' , $ file ), 20 )
209
205
);
210
206
@@ -214,7 +210,7 @@ private function moveContentToTempFile(string $file): ?string
214
210
if (! flock ($ fd , LOCK_EX )) {
215
211
fclose ($ fd );
216
212
217
- $ this ->errorHandler -> exception (
213
+ $ this ->exception (
218
214
new Exception (sprintf ('the file %s not can lock. ' , $ file ), 21 )
219
215
);
220
216
@@ -224,7 +220,7 @@ private function moveContentToTempFile(string $file): ?string
224
220
if (! copy ($ file , $ fileDestination )) {
225
221
fclose ($ fd );
226
222
227
- $ this ->errorHandler -> exception (
223
+ $ this ->exception (
228
224
new Exception (
229
225
sprintf ('the file %s not can copy to temp file %s. ' , $ file , $ fileDestination ),
230
226
22
@@ -239,7 +235,7 @@ private function moveContentToTempFile(string $file): ?string
239
235
240
236
unlink ($ fileDestination );
241
237
242
- $ this ->errorHandler -> exception (
238
+ $ this ->exception (
243
239
new Exception (sprintf ('the file %s not can truncate. ' , $ file ), 23 )
244
240
);
245
241
0 commit comments