88
99class 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
0 commit comments