44
55use Cesargb \Log \Compress \Gz ;
66use Cesargb \Log \Processors \RotativeProcessor ;
7- use LogicException ;
7+ use Exception ;
88
99class Rotation
1010{
@@ -16,9 +16,13 @@ class Rotation
1616
1717 private $ thenCallback = null ;
1818
19+ private $ errorHandler = null ;
20+
1921 public function __construct ()
2022 {
2123 $ this ->processor = new RotativeProcessor ();
24+
25+ $ this ->errorHandler = new ErrorHandler ();
2226 }
2327
2428 /**
@@ -75,6 +79,19 @@ public function then(callable $callable): self
7579 return $ this ;
7680 }
7781
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+
7895 /**
7996 * Rotate file
8097 *
@@ -94,7 +111,14 @@ public function rotate(string $file): bool
94111 if ($ fileRotated && $ this ->_compress ) {
95112 $ gz = new Gz ();
96113
97- $ fileRotated = $ gz ->handler ($ fileRotated );
114+ try {
115+ $ fileRotated = $ gz ->handler ($ fileRotated );
116+ } catch (Exception $ error ) {
117+ $ this ->errorHandler ->exception ($ error );
118+
119+ $ fileRotated = null ;
120+ }
121+
98122 }
99123
100124 if ($ fileRotated && $ this ->thenCallback ) {
@@ -129,12 +153,15 @@ private function runProcessor(string $originalFile, ?string $fileRotated): ?stri
129153 *
130154 * @param string $file
131155 * @return boolean
132- * @throws LogicException
133156 */
134157 private function canRotate (string $ file ): bool
135158 {
136159 if (! $ this ->fileIsValid ($ file )) {
137- throw new LogicException (sprintf ('the file %s not is valid. ' , $ file ), 2 );
160+ $ this ->errorHandler ->exception (
161+ new Exception (sprintf ('the file %s not is valid. ' , $ file ), 10 )
162+ );
163+
164+ return false ;
138165 }
139166
140167 return filesize ($ file ) > ($ this ->_minSize > 0 ? $ this ->_minSize : 0 );
@@ -177,18 +204,33 @@ private function moveContentToTempFile(string $file): ?string
177204 $ fd = fopen ($ file , 'r+ ' );
178205
179206 if (! $ fd ) {
207+ $ this ->errorHandler ->exception (
208+ new Exception (sprintf ('the file %s not can open. ' , $ file ), 20 )
209+ );
210+
180211 return null ;
181212 }
182213
183214 if (! flock ($ fd , LOCK_EX )) {
184215 fclose ($ fd );
185216
217+ $ this ->errorHandler ->exception (
218+ new Exception (sprintf ('the file %s not can lock. ' , $ file ), 21 )
219+ );
220+
186221 return null ;
187222 }
188223
189224 if (! copy ($ file , $ fileDestination )) {
190225 fclose ($ fd );
191226
227+ $ this ->errorHandler ->exception (
228+ new Exception (
229+ sprintf ('the file %s not can copy to temp file %s. ' , $ file , $ fileDestination ),
230+ 22
231+ )
232+ );
233+
192234 return null ;
193235 }
194236
@@ -197,6 +239,10 @@ private function moveContentToTempFile(string $file): ?string
197239
198240 unlink ($ fileDestination );
199241
242+ $ this ->errorHandler ->exception (
243+ new Exception (sprintf ('the file %s not can truncate. ' , $ file ), 23 )
244+ );
245+
200246 return null ;
201247 }
202248
0 commit comments