Passed
Push — master ( b0a201...a76d1f )
by Cesar
03:05
created

AbstractHandler::validateDirectory()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 5
nop 0
dl 0
loc 25
rs 9.5555
c 0
b 0
f 0
1
<?php
2
3
namespace Cesargb\File\Rotate\Handlers;
4
5
use Cesargb\File\Rotate\Events\RotateHasFailed;
6
use Cesargb\File\Rotate\Events\RotateIsNotNecessary;
7
use Cesargb\File\Rotate\Helpers\Log as LogHelper;
8
use Exception;
9
10
abstract class AbstractHandler implements HandlerInterface
11
{
12
    protected $file;
13
14
    protected $compress;
15
16
    protected $dir_to_archive;
17
18
    protected $file_rotated;
19
20
    public function __construct($file, bool $compress = true, $dir_to_archive = null)
21
    {
22
        $this->file = $file;
23
        $this->compress = $compress;
24
25
        if (empty($dir_to_archive)) {
26
            $this->dir_to_archive = dirname($file);
27
        } else {
28
            if (substr($dir_to_archive, 0, 1) == '/') {
29
                $this->dir_to_archive = $dir_to_archive;
30
            } else {
31
                $this->dir_to_archive = dirname($file).'/'.$dir_to_archive;
32
            }
33
        }
34
    }
35
36
    protected function validate()
37
    {
38
        clearstatcache();
39
40
        return $this->validateFile() && $this->validateDirectory();
41
    }
42
43
    private function validateFile(): bool
44
    {
45
        if (! is_file($this->file)) {
46
            event(new RotateIsNotNecessary($this->file, 'File '.$this->file.' does not exists'));
47
48
            return false;
49
        }
50
51
        if (filesize($this->file) == 0) {
52
            event(new RotateIsNotNecessary($this->file, 'File '.$this->file.' is empty'));
53
54
            return false;
55
        }
56
57
        if (! is_writable($this->file)) {
58
            event(new RotateHasFailed($this->file, new Exception('File '.$this->file.' is not writable')));
59
60
            return false;
61
        }
62
63
        return true;
64
    }
65
66
    private function validateDirectory(): bool
67
    {
68
        if (is_dir($this->dir_to_archive)) {
69
            if (! is_writable($this->dir_to_archive)) {
70
                event(new RotateHasFailed($this->file, new Exception('Directory '.$this->dir_to_archive.' to archive logs is not writable')));
71
72
                return false;
73
            }
74
75
            return true;
76
        }
77
78
        if (file_exists($this->dir_to_archive)) {
79
            event(new RotateHasFailed($this->file, new Exception('Directory '.$this->dir_to_archive.' to archive exists and is not a directory')));
80
81
            return false;
82
        }
83
84
        if (! mkdir($this->dir_to_archive, 0777, true)) {
85
            event(new RotateHasFailed($this->file, new Exception('Directory '.$this->dir_to_archive.' to archive logs is not writable')));
86
87
            return false;
88
        }
89
90
        return true;
91
    }
92
93
    protected function rebaseArchiveDir($file)
94
    {
95
        return $this->dir_to_archive.'/'.basename($file);
96
    }
97
98
    protected function close()
99
    {
100
        LogHelper::closeHandlers();
101
    }
102
103
    protected function moveData($fileSource, $fileDestination)
104
    {
105
        $fdSource = fopen($fileSource, 'r+');
106
107
        if (! $fdSource) {
0 ignored issues
show
introduced by
$fdSource is of type false|resource, thus it always evaluated to false.
Loading history...
108
            return false;
109
        }
110
111
        if (! flock($fdSource, LOCK_EX)) {
112
            fclose($fdSource);
113
114
            return false;
115
        }
116
117
        if (! copy($fileSource, $fileDestination)) {
118
            fclose($fdSource);
119
120
            return false;
121
        }
122
123
        if (! ftruncate($fdSource, 0)) {
124
            fclose($fdSource);
125
126
            unlink($fileDestination);
127
128
            return false;
129
        }
130
131
        flock($fdSource, LOCK_UN);
132
133
        fflush($fdSource);
134
135
        fclose($fdSource);
136
137
        clearstatcache();
138
139
        return true;
140
    }
141
}
142