Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
26.67% |
4 / 15 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ActualFileWriter | |
26.67% |
4 / 15 |
|
25.00% |
1 / 4 |
49.44 | |
0.00% |
0 / 1 |
| writeFile | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
3.58 | |||
| clearPath | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| fileExists | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| readContents | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Other; |
| 3 | |
| 4 | use RuntimeException; |
| 5 | |
| 6 | final class ActualFileWriter implements FileWriterInterface, FileReaderInterface |
| 7 | { |
| 8 | public function writeFile(string $filename, string $fileContents): void |
| 9 | { |
| 10 | @mkdir(dirname($filename), recursive: true); |
| 11 | if ($fileContents === @file_get_contents($filename)) { |
| 12 | return; |
| 13 | } |
| 14 | if (false === @file_put_contents($filename, $fileContents)) { |
| 15 | throw new RuntimeException('I can not write to file: ' . $filename); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | public function clearPath(string $path): void |
| 20 | { |
| 21 | if ($path === '/') { |
| 22 | throw new \LogicException('I will not remove everything'); |
| 23 | } |
| 24 | if (is_dir($path)) { |
| 25 | system('rm -rf ' . escapeshellarg($path)); |
| 26 | } |
| 27 | @mkdir($path, recursive: true); |
| 28 | } |
| 29 | |
| 30 | public function fileExists(string $filePath): bool |
| 31 | { |
| 32 | return file_exists($filePath) && !is_dir($filePath); |
| 33 | } |
| 34 | |
| 35 | public function readContents(string $filePath): string |
| 36 | { |
| 37 | $contents = @file_get_contents($filePath); |
| 38 | if ($contents === false) { |
| 39 | throw new \RuntimeException('Could not read ' . $filePath); |
| 40 | } |
| 41 | return $contents; |
| 42 | } |
| 43 | } |