maestriam /
filesystem
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Maestriam\FileSystem\Foundation\File; |
||
| 4 | |||
| 5 | use Exception; |
||
| 6 | use Maestriam\FileSystem\Foundation\Drive\PathSanitizer; |
||
| 7 | |||
| 8 | class FileHandler |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Nome do arquivo |
||
| 12 | * |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | private string $name; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Nível de permissão |
||
| 19 | * |
||
| 20 | * @var int |
||
| 21 | */ |
||
| 22 | private int $permission = 0755; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Manipulação |
||
| 26 | * |
||
| 27 | * @param string $location |
||
| 28 | * @param string $name |
||
| 29 | */ |
||
| 30 | public function __construct(string $location, string $name) |
||
| 31 | { |
||
| 32 | $this->setLocation($location)->setName($name); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Define o local do arquivo |
||
| 37 | * |
||
| 38 | * @param string $location |
||
| 39 | * @return FileHandler |
||
| 40 | */ |
||
| 41 | private function setLocation(string $location) : FileHandler |
||
| 42 | { |
||
| 43 | if (! strlen($location)) { |
||
| 44 | throw new \Exception('Folder not defined.'); |
||
| 45 | } |
||
| 46 | |||
| 47 | $this->location = $location; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 48 | return $this; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Define o nome do |
||
| 53 | * |
||
| 54 | * @param string $name |
||
| 55 | * @return FileHandler |
||
| 56 | */ |
||
| 57 | private function setName(string $name) : FileHandler |
||
| 58 | { |
||
| 59 | if (! strlen($name)) { |
||
| 60 | throw new \Exception('File name not defined.'); |
||
| 61 | } |
||
| 62 | |||
| 63 | $this->name = $name; |
||
| 64 | return $this; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Executa a criação do arquivo |
||
| 69 | * |
||
| 70 | * @param string $content |
||
| 71 | * @return FileHandler |
||
| 72 | */ |
||
| 73 | private function makeFile(string $content) : FileHandler |
||
| 74 | { |
||
| 75 | try { |
||
| 76 | |||
| 77 | $location = $this->getLocation(); |
||
| 78 | $filename = sprintf("%s/%s", $location, $this->name); |
||
| 79 | |||
| 80 | $handle = fopen($filename, 'w'); |
||
| 81 | |||
| 82 | fwrite($handle, $content); |
||
| 83 | fclose($handle); |
||
| 84 | |||
| 85 | $this->content = $content; |
||
|
0 ignored issues
–
show
|
|||
| 86 | |||
| 87 | return $this; |
||
| 88 | |||
| 89 | } catch (\Exception $e) { |
||
| 90 | throw new Exception('Error to create file: '.$e->getMessage()); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Cria o diretório para inserção do arquivo |
||
| 96 | * |
||
| 97 | * @return FileHandler |
||
| 98 | */ |
||
| 99 | private function makeFolder() : FileHandler |
||
| 100 | { |
||
| 101 | try { |
||
| 102 | |||
| 103 | $location = $this->getLocation(); |
||
| 104 | |||
| 105 | if (! is_dir($location)) { |
||
| 106 | mkdir($location, $this->permission, true); |
||
| 107 | } |
||
| 108 | |||
| 109 | return $this; |
||
| 110 | |||
| 111 | } catch (\Exception $e) { |
||
| 112 | throw new Exception("Error to create folder: ". $e->getMessage()); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Undocumented function |
||
| 118 | * |
||
| 119 | * @return FileInfo |
||
| 120 | */ |
||
| 121 | private function toObject() : FileInfo |
||
| 122 | { |
||
| 123 | $obj = new FileInfo(); |
||
| 124 | |||
| 125 | $obj->created_at = now(); |
||
| 126 | $obj->updated_at = now(); |
||
| 127 | $obj->name = $this->name; |
||
| 128 | $obj->content = $this->content; |
||
| 129 | $obj->folder = $this->getLocation(); |
||
| 130 | $obj->absolute_path = $this->getLocation() . $this->name; |
||
| 131 | |||
| 132 | return $obj; |
||
| 133 | } |
||
| 134 | |||
| 135 | public function getLocation() |
||
| 136 | { |
||
| 137 | return PathSanitizer::sanitize($this->location); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Define o conteúdo que será inserido no arquivo |
||
| 142 | * |
||
| 143 | * @param string $content |
||
| 144 | * @return FileHandler |
||
| 145 | */ |
||
| 146 | private function setContent(string $content) : FileHandler |
||
| 147 | { |
||
| 148 | $this->content = $content; |
||
|
0 ignored issues
–
show
|
|||
| 149 | |||
| 150 | return $this; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Analisa o nome do arquivo se contém algum sub-diretório embutido |
||
| 155 | * Se houver, separa e |
||
| 156 | * |
||
| 157 | * @return FileHandler |
||
| 158 | */ |
||
| 159 | private function analizeFilename() : FileHandler |
||
| 160 | { |
||
| 161 | if (! str_contains($this->name, '/')) { |
||
| 162 | return $this; |
||
| 163 | } |
||
| 164 | |||
| 165 | $pieces = explode('/', $this->name); |
||
| 166 | $flname = array_pop($pieces); |
||
| 167 | $folder = implode('/', $pieces); |
||
| 168 | |||
| 169 | $this->name = $flname; |
||
| 170 | $this->location .= $folder . DS; |
||
| 171 | |||
| 172 | return $this; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Undocumented function |
||
| 177 | * |
||
| 178 | * @param string $content |
||
| 179 | * @return void |
||
| 180 | */ |
||
| 181 | public function create(string $content) : FileInfo |
||
| 182 | { |
||
| 183 | return $this->analizeFilename() |
||
|
0 ignored issues
–
show
|
|||
| 184 | ->makeFolder() |
||
| 185 | ->makeFile($content) |
||
| 186 | ->setContent($content) |
||
| 187 | ->toObject(); |
||
| 188 | } |
||
| 189 | } |