Passed
Push — master ( abfb73...066355 )
by Cesar
38s queued 12s
created

Rotation::sucessfull()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Cesargb\Log;
4
5
use Cesargb\Log\Compress\Gz;
6
use Cesargb\Log\Processors\RotativeProcessor;
7
use Exception;
8
9
class Rotation
10
{
11
    use Optionable;
12
    use ErrorHandler;
13
14
    private RotativeProcessor $processor;
15
16
    private bool $_compress = false;
17
18
    private int $_minSize = 0;
19
20
    private bool $_truncate = false;
21
22
    public function __construct(array $options = [])
23
    {
24
        $this->processor = new RotativeProcessor();
25
26
        $this->methodsOptionables([
27
            'compress',
28
            'truncate',
29
            'minSize',
30
            'files',
31
            'then',
32
            'catch',
33
            'finally',
34
        ]);
35
36
        $this->options($options);
37
    }
38
39
    /**
40
     * Log files are rotated count times before being removed.
41
     */
42
    public function files(int $count): self
43
    {
44
        $this->processor->files($count);
45
46
        return $this;
47
    }
48
49
    /**
50
     * Old versions of log files are compressed.
51
     */
52
    public function compress(bool $compress = true): self
53
    {
54
        $this->_compress = $compress;
55
56
        if ($compress) {
57
            $this->processor->addExtension('gz');
58
        } else {
59
            $this->processor->removeExtention('gz');
60
        }
61
62
        return $this;
63
    }
64
65
    /**
66
     * Truncate the original log file in place after creating a copy, instead of
67
     * moving the old log file.
68
     *
69
     * It can be used when some program cannot be told to close its logfile and
70
     * thus might continue writing (appending) to the previous log file forever.
71
     */
72
    public function truncate(bool $truncate = true): self
73
    {
74
        $this->_truncate = $truncate;
75
76
        return $this;
77
    }
78
79
    /**
80
     * Log files are rotated when they grow bigger than size bytes.
81
     */
82
    public function minSize(int $bytes): self
83
    {
84
        $this->_minSize = $bytes;
85
86
        return $this;
87
    }
88
89
    /**
90
     * Rotate file.
91
     *
92
     * @return bool true if rotated was successful
93
     */
94
    public function rotate(string $filename): bool
95
    {
96
        $this->setFilename($filename);
97
98
        if (!$this->canRotate($filename)) {
99
            return false;
100
        }
101
102
        $fileTemporary = $this->_truncate
103
            ? $this->copyAndTruncate($filename)
104
            : $this->move($filename);
105
106
        if (is_null($fileTemporary)) {
107
            return false;
108
        }
109
110
        $fileTarget = $this->runProcessor(
111
            $filename,
112
            $fileTemporary
113
        );
114
115
        if (is_null($fileTarget)) {
116
            return false;
117
        }
118
119
        $fileTarget = $this->runCompress($fileTarget);
120
121
        $this->sucessfull($filename, $fileTarget);
122
123
        return true;
124
    }
125
126
    /**
127
     * Run processor.
128
     */
129
    private function runProcessor(string $filenameSource, ?string $filenameTarget): ?string
130
    {
131
        $this->initProcessorFile($filenameSource);
132
133
        if (!$filenameTarget) {
134
            return null;
135
        }
136
137
        return $this->processor->handler($filenameTarget);
138
    }
139
140
    private function runCompress(string $filename): ?string
141
    {
142
        if (!$this->_compress) {
143
            return $filename;
144
        }
145
146
        $gz = new Gz();
147
148
        try {
149
            return $gz->handler($filename);
150
        } catch (Exception $error) {
151
            $this->exception($error);
152
153
            return null;
154
        }
155
    }
156
157
    /**
158
     * check if file need rotate.
159
     */
160
    private function canRotate(string $filename): bool
161
    {
162
        if (!file_exists($filename)) {
163
            $this->finished(sprintf('the file %s not exists.', $filename), $filename);
164
165
            return false;
166
        }
167
168
        if (!$this->fileIsValid($filename)) {
169
            $this->exception(
170
                new Exception(sprintf('the file %s not is valid.', $filename), 10)
171
            );
172
173
            return false;
174
        }
175
176
        return filesize($filename) > ($this->_minSize > 0 ? $this->_minSize : 0);
177
    }
178
179
    /**
180
     * Set original File to processor.
181
     */
182
    private function initProcessorFile(string $filename): void
183
    {
184
        $this->processor->setFilenameSource($filename);
185
    }
186
187
    /**
188
     * check if file is valid to rotate.
189
     */
190
    private function fileIsValid(string $filename): bool
191
    {
192
        return is_file($filename) && is_writable($filename);
193
    }
194
195
    /**
196
     * copy data to temp file and truncate.
197
     */
198
    private function copyAndTruncate(string $filename): ?string
199
    {
200
        clearstatcache();
201
202
        $filenameTarget = tempnam(dirname($filename), 'LOG');
203
204
        $fd = fopen($filename, 'r+');
205
206
        if ($fd === false) {
207
            $this->exception(
208
                new Exception(sprintf('the file %s not can open.', $filename), 20)
209
            );
210
211
            return null;
212
        }
213
214
        if (!flock($fd, LOCK_EX)) {
215
            fclose($fd);
216
217
            $this->exception(
218
                new Exception(sprintf('the file %s not can lock.', $filename), 21)
219
            );
220
221
            return null;
222
        }
223
224
        if (!copy($filename, $filenameTarget)) {
225
            fclose($fd);
226
227
            $this->exception(
228
                new Exception(
229
                    sprintf('the file %s not can copy to temp file %s.', $filename, $filenameTarget),
230
                    22
231
                )
232
            );
233
234
            return null;
235
        }
236
237
        if (!ftruncate($fd, 0)) {
238
            fclose($fd);
239
240
            unlink($filenameTarget);
241
242
            $this->exception(
243
                new Exception(sprintf('the file %s not can truncate.', $filename), 23)
244
            );
245
246
            return null;
247
        }
248
249
        flock($fd, LOCK_UN);
250
251
        fflush($fd);
252
253
        fclose($fd);
254
255
        return $filenameTarget;
256
    }
257
258
    private function move(string $filename): ?string
259
    {
260
        clearstatcache();
261
262
        $filenameTarget = tempnam(dirname($filename), 'LOG');
263
264
        if (!rename($filename, $filenameTarget)) {
265
            $this->exception(
266
                new Exception(
267
                    sprintf('the file %s not can move to temp file %s.', $filename, $filenameTarget),
268
                    22
269
                )
270
            );
271
272
            return null;
273
        }
274
275
        return $filenameTarget;
276
    }
277
}
278