Passed
Pull Request — master (#13)
by Cesar
01:42
created

Rotation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 15
rs 9.9332
c 0
b 0
f 0
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($filename, sprintf('the file %s not exists.', $filename));
164
            return false;
165
        }
166
167
        if (!$this->fileIsValid($filename)) {
168
            $this->exception(
169
                new Exception(sprintf('the file %s not is valid.', $filename), 10)
170
            );
171
172
            return false;
173
        }
174
175
        return filesize($filename) > ($this->_minSize > 0 ? $this->_minSize : 0);
176
    }
177
178
    /**
179
     * Set original File to processor.
180
     */
181
    private function initProcessorFile(string $filename): void
182
    {
183
        $this->processor->setFilenameSource($filename);
184
    }
185
186
    /**
187
     * check if file is valid to rotate.
188
     */
189
    private function fileIsValid(string $filename): bool
190
    {
191
        return is_file($filename) && is_writable($filename);
192
    }
193
194
    /**
195
     * copy data to temp file and truncate.
196
     */
197
    private function copyAndTruncate(string $filename): ?string
198
    {
199
        clearstatcache();
200
201
        $filenameTarget = tempnam(dirname($filename), 'LOG');
202
203
        $fd = fopen($filename, 'r+');
204
205
        if ($fd === false) {
206
            $this->exception(
207
                new Exception(sprintf('the file %s not can open.', $filename), 20)
208
            );
209
210
            return null;
211
        }
212
213
        if (!flock($fd, LOCK_EX)) {
214
            fclose($fd);
215
216
            $this->exception(
217
                new Exception(sprintf('the file %s not can lock.', $filename), 21)
218
            );
219
220
            return null;
221
        }
222
223
        if (!copy($filename, $filenameTarget)) {
224
            fclose($fd);
225
226
            $this->exception(
227
                new Exception(
228
                    sprintf('the file %s not can copy to temp file %s.', $filename, $filenameTarget),
229
                    22
230
                )
231
            );
232
233
            return null;
234
        }
235
236
        if (!ftruncate($fd, 0)) {
237
            fclose($fd);
238
239
            unlink($filenameTarget);
240
241
            $this->exception(
242
                new Exception(sprintf('the file %s not can truncate.', $filename), 23)
243
            );
244
245
            return null;
246
        }
247
248
        flock($fd, LOCK_UN);
249
250
        fflush($fd);
251
252
        fclose($fd);
253
254
        return $filenameTarget;
255
    }
256
257
    private function move(string $filename): ?string
258
    {
259
        clearstatcache();
260
261
        $filenameTarget = tempnam(dirname($filename), 'LOG');
262
263
        if (!rename($filename, $filenameTarget)) {
264
            $this->exception(
265
                new Exception(
266
                    sprintf('the file %s not can move to temp file %s.', $filename, $filenameTarget),
267
                    22
268
                )
269
            );
270
271
            return null;
272
        }
273
274
        return $filenameTarget;
275
    }
276
}
277