|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Cesargb\Log\Handlers; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Cesargb\Log\Exceptions\RotateFailed; |
| 7 | +use Cesargb\Log\Exceptions\FileIsNotValid; |
| 8 | +use Cesargb\Log\Exceptions\DirectoryIsNotValid; |
| 9 | + |
| 10 | +abstract class AbstractHandler implements HandlerInterface |
| 11 | +{ |
| 12 | + |
| 13 | + protected $file; |
| 14 | + |
| 15 | + protected $compress; |
| 16 | + |
| 17 | + public function __construct($file, bool $compress = true) |
| 18 | + { |
| 19 | + $this->file = $file; |
| 20 | + |
| 21 | + $this->compress = $compress; |
| 22 | + |
| 23 | + clearstatcache(); |
| 24 | + } |
| 25 | + |
| 26 | + protected function validateSource() |
| 27 | + { |
| 28 | + if (! is_writable($this->file)) { |
| 29 | + return new FileIsNotValid($this->file, 'is not writable'); |
| 30 | + } |
| 31 | + |
| 32 | + return true; |
| 33 | + } |
| 34 | + |
| 35 | + protected function validateDestination($fileDestination) |
| 36 | + { |
| 37 | + $dir_destination = dirname($fileDestination); |
| 38 | + |
| 39 | + if (! is_dir($dir_destination)) { |
| 40 | + if (! file_exists($dir_destination)) { |
| 41 | + if (! mkdir($dir_destination, 0777, true)) { |
| 42 | + return new DirectoryIsNotValid($this->file, 'Not is writable'); |
| 43 | + } |
| 44 | + } else { |
| 45 | + return new DirectoryIsNotValid($this->file, 'Exists and is not a directory'); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if (! is_writable($dir_destination)) { |
| 50 | + return new DirectoryIsNotValid($this->file, 'Not is writable'); |
| 51 | + } |
| 52 | + |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + protected function moveTo($fileDestination) |
| 57 | + { |
| 58 | + if ($e = $this->validateSource($this->file) !== true) { |
| 59 | + return $e; |
| 60 | + } |
| 61 | + |
| 62 | + if ($e = $this->validateDestination($fileDestination) !== true) { |
| 63 | + return $e; |
| 64 | + } |
| 65 | + |
| 66 | + if (filesize($this->file) == 0) { |
| 67 | + return true; |
| 68 | + } |
| 69 | + |
| 70 | + $fdSource = fopen($this->file, 'r+'); |
| 71 | + |
| 72 | + if (! $fdSource) { |
| 73 | + return new FileIsNotValid($this->file, 'is not writable'); |
| 74 | + } |
| 75 | + |
| 76 | + if (! flock($fdSource, LOCK_EX)) { |
| 77 | + fclose($fdSource); |
| 78 | + |
| 79 | + return new FileIsNotValid($this->file, 'is not lockeable'); |
| 80 | + } |
| 81 | + |
| 82 | + if (! copy($this->file, $fileDestination)) { |
| 83 | + fclose($fdSource); |
| 84 | + |
| 85 | + return new RotateFailed($this->file, $fileDestination, 'fail to copy data'); |
| 86 | + } |
| 87 | + |
| 88 | + if (! ftruncate($fdSource, 0)) { |
| 89 | + fclose($fdSource); |
| 90 | + |
| 91 | + unlink($fileDestination); |
| 92 | + |
| 93 | + return new FileIsNotValid($this->file, 'fail to truncate'); |
| 94 | + } |
| 95 | + |
| 96 | + flock($fdSource, LOCK_UN); |
| 97 | + |
| 98 | + fflush($fdSource); |
| 99 | + |
| 100 | + fclose($fdSource); |
| 101 | + |
| 102 | + clearstatcache(); |
| 103 | + |
| 104 | + return true; |
| 105 | + } |
| 106 | +} |
0 commit comments