88
99class Rotation
1010{
11- use Optionable, ErrorHandler;
11+ use Optionable;
12+ use ErrorHandler;
1213
1314 private RotativeProcessor $ processor ;
1415
@@ -34,10 +35,7 @@ public function __construct(array $options = [])
3435 }
3536
3637 /**
37- * Log files are rotated count times before being removed
38- *
39- * @param int $count
40- * @return self
38+ * Log files are rotated count times before being removed.
4139 */
4240 public function files (int $ count ): self
4341 {
@@ -47,25 +45,23 @@ public function files(int $count): self
4745 }
4846
4947 /**
50- * Old versions of log files are compressed
51- *
52- *@param bool $compress
53- * @return self
48+ * Old versions of log files are compressed.
5449 */
5550 public function compress (bool $ compress = true ): self
5651 {
5752 $ this ->_compress = $ compress ;
5853
59- $ this ->processor ->compress ();
54+ if ($ compress ) {
55+ $ this ->processor ->addExtension ('gz ' );
56+ } else {
57+ $ this ->processor ->removeExtention ('gz ' );
58+ }
6059
6160 return $ this ;
6261 }
6362
6463 /**
65- * Log files are rotated when they grow bigger than size bytes
66- *
67- * @param integer $bytes
68- * @return self
64+ * Log files are rotated when they grow bigger than size bytes.
6965 */
7066 public function minSize (int $ bytes ): self
7167 {
@@ -75,11 +71,9 @@ public function minSize(int $bytes): self
7571 }
7672
7773 /**
78- * Call function if roteted was sucessfull and pass
79- * the file as argument.
80- *
81- * @param callable $callable
82- * @return self
74+ * Function that will be executed when the rotation is successful.
75+ * The first argument will be the name of the destination file and
76+ * the second the name of the rotated file.
8377 */
8478 public function then (callable $ callable ): self
8579 {
@@ -89,154 +83,134 @@ public function then(callable $callable): self
8983 }
9084
9185 /**
92- * Rotate file
86+ * Rotate file.
9387 *
94- * @param string $file
95- * @return boolean true if rotated was successful
88+ * @return bool true if rotated was successful
9689 */
97- public function rotate (string $ file ): bool
90+ public function rotate (string $ filename ): bool
9891 {
99- if (!$ this ->canRotate ($ file )) {
92+ if (!$ this ->canRotate ($ filename )) {
10093 return false ;
10194 }
10295
103- $ fileRotate = $ this ->moveContentToTempFile ($ file );
96+ $ fileNameTemp = $ this ->moveContentToTempFile ($ filename );
10497
105- $ fileRotated = $ this ->runProcessor ($ file , $ fileRotate );
98+ $ filenameRotated = $ this ->runProcessor ($ filename , $ fileNameTemp );
10699
107- if ($ fileRotated && $ this ->_compress ) {
100+ if ($ filenameRotated && $ this ->_compress ) {
108101 $ gz = new Gz ();
109102
110103 try {
111- $ fileRotated = $ gz ->handler ($ fileRotated );
104+ $ filenameRotated = $ gz ->handler ($ filenameRotated );
112105 } catch (Exception $ error ) {
113106 $ this ->exception ($ error );
114107
115- $ fileRotated = null ;
108+ $ filenameRotated = null ;
116109 }
117-
118110 }
119111
120- if ($ fileRotated && $ this ->thenCallback ) {
121- call_user_func ($ this ->thenCallback , $ fileRotated );
112+ if ($ filenameRotated && $ this ->thenCallback ) {
113+ call_user_func ($ this ->thenCallback , $ filenameRotated , $ filename );
122114 }
123115
124- return ! empty ($ fileRotated );
116+ return !empty ($ filenameRotated );
125117 }
126118
127119 /**
128- * Run processor
129- *
130- * @param string $originalFile
131- * @param string|null $fileRotated
132- * @return string|null
120+ * Run processor.
133121 */
134- private function runProcessor (string $ originalFile , ?string $ fileRotated ): ?string
122+ private function runProcessor (string $ filenameSource , ?string $ filenameTarget ): ?string
135123 {
136- $ this ->initProcessorFile ($ originalFile );
124+ $ this ->initProcessorFile ($ filenameSource );
137125
138- if (!$ fileRotated ) {
126+ if (!$ filenameTarget ) {
139127 return null ;
140128 }
141129
142- $ fileRotated = $ this ->processor ->handler ($ fileRotated );
143-
144- return $ fileRotated ;
130+ return $ this ->processor ->handler ($ filenameTarget );
145131 }
146132
147133 /**
148- * check if file need rotate
149- *
150- * @param string $file
151- * @return boolean
134+ * check if file need rotate.
152135 */
153- private function canRotate (string $ file ): bool
136+ private function canRotate (string $ filename ): bool
154137 {
155- if (! $ this ->fileIsValid ($ file )) {
138+ if (!$ this ->fileIsValid ($ filename )) {
156139 $ this ->exception (
157- new Exception (sprintf ('the file %s not is valid. ' , $ file ), 10 )
140+ new Exception (sprintf ('the file %s not is valid. ' , $ filename ), 10 )
158141 );
159142
160143 return false ;
161144 }
162145
163- return filesize ($ file ) > ($ this ->_minSize > 0 ? $ this ->_minSize : 0 );
146+ return filesize ($ filename ) > ($ this ->_minSize > 0 ? $ this ->_minSize : 0 );
164147 }
165148
166149 /**
167- * Set original File to processor
168- *
169- * @param string $file
170- * @return void
150+ * Set original File to processor.
171151 */
172- private function initProcessorFile (string $ file ): void
152+ private function initProcessorFile (string $ filename ): void
173153 {
174- $ this ->processor ->setFileOriginal ( $ file );
154+ $ this ->processor ->setFilenameSource ( $ filename );
175155 }
176156
177157 /**
178- * check if file is valid to rotate
179- *
180- * @param string|null $file
181- * @return boolean
158+ * check if file is valid to rotate.
182159 */
183- private function fileIsValid (?string $ file ): bool
160+ private function fileIsValid (?string $ filename ): bool
184161 {
185- return $ file && is_file ($ file ) && is_writable ($ file );
162+ return $ filename && is_file ($ filename ) && is_writable ($ filename );
186163 }
187164
188165 /**
189- * move data to temp file and truncate
190- *
191- * @param string $file
192- * @return string|null
166+ * move data to temp file and truncate.
193167 */
194- private function moveContentToTempFile (string $ file ): ?string
168+ private function moveContentToTempFile (string $ filename ): ?string
195169 {
196170 clearstatcache ();
197171
198- $ fileDestination = tempnam (dirname ($ file ), 'LOG ' );
172+ $ filenameTarget = tempnam (dirname ($ filename ), 'LOG ' );
199173
200- $ fd = fopen ($ file , 'r+ ' );
174+ $ fd = fopen ($ filename , 'r+ ' );
201175
202- if (! $ fd ) {
176+ if (!$ fd ) {
203177 $ this ->exception (
204- new Exception (sprintf ('the file %s not can open. ' , $ file ), 20 )
178+ new Exception (sprintf ('the file %s not can open. ' , $ filename ), 20 )
205179 );
206180
207181 return null ;
208182 }
209183
210- if (! flock ($ fd , LOCK_EX )) {
184+ if (!flock ($ fd , LOCK_EX )) {
211185 fclose ($ fd );
212186
213187 $ this ->exception (
214- new Exception (sprintf ('the file %s not can lock. ' , $ file ), 21 )
188+ new Exception (sprintf ('the file %s not can lock. ' , $ filename ), 21 )
215189 );
216190
217191 return null ;
218192 }
219193
220- if (! copy ($ file , $ fileDestination )) {
194+ if (!copy ($ filename , $ filenameTarget )) {
221195 fclose ($ fd );
222196
223197 $ this ->exception (
224198 new Exception (
225- sprintf ('the file %s not can copy to temp file %s. ' , $ file , $ fileDestination ),
199+ sprintf ('the file %s not can copy to temp file %s. ' , $ filename , $ filenameTarget ),
226200 22
227201 )
228202 );
229203
230204 return null ;
231205 }
232206
233- if (! ftruncate ($ fd , 0 )) {
207+ if (!ftruncate ($ fd , 0 )) {
234208 fclose ($ fd );
235209
236- unlink ($ fileDestination );
210+ unlink ($ filenameTarget );
237211
238212 $ this ->exception (
239- new Exception (sprintf ('the file %s not can truncate. ' , $ file ), 23 )
213+ new Exception (sprintf ('the file %s not can truncate. ' , $ filename ), 23 )
240214 );
241215
242216 return null ;
@@ -248,6 +222,6 @@ private function moveContentToTempFile(string $file): ?string
248222
249223 fclose ($ fd );
250224
251- return $ fileDestination ;
225+ return $ filenameTarget ;
252226 }
253227}
0 commit comments