Lines
20.00%
2 / 10
Methods
66.66%
2 / 3
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| getDirectory | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| getName | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| exists | n/a | 0 / 0 | n/a | 0 / 0 | 0 | ||
| delete | n/a | 0 / 0 | n/a | 0 / 0 | 0 | ||
| create | n/a | 0 / 0 | n/a | 0 / 0 | 0 | ||
| rename | 0.00% | 0 / 8 | 0.00% | 0 / 1 | 12 | ||
| 12 | abstract class FileSystemObject | |
| 13 | { | |
| 14 | /** | |
| 15 | * The absolute path to the filesystem object | |
| 16 | * | |
| 17 | * @var string | |
| 18 | */ | |
| 19 | public protected(set) string $path; | |
| 20 | ||
| 21 | /** | |
| 22 | * Gets the parent directory of this filesystem object | |
| 23 | * | |
| 24 | * @return Folder The parent directory as a Folder object | |
| 25 | */ | |
| 26 | public function getDirectory(): Folder | |
| 27 | { | |
| 28 | return new Folder(dirname($this->path), true); | |
| 29 | } | |
| 30 | ||
| 31 | /** | |
| 32 | * Gets the name of the filesystem object | |
| 33 | * | |
| 34 | * @return string The basename of the path | |
| 35 | */ | |
| 36 | public function getName(): string | |
| 37 | { | |
| 38 | return basename($this->path); | |
| 39 | } | |
| 40 | ||
| 41 | /** | |
| 42 | * Checks if the filesystem object exists | |
| 43 | * | |
| 44 | * @return bool True if exists, false otherwise | |
| 45 | */ | |
| 46 | abstract public function exists(): bool; | |
| 47 | ||
| 48 | /** | |
| 49 | * Deletes the filesystem object | |
| 50 | * | |
| 51 | * @return bool True if successfully deleted, false otherwise | |
| 52 | */ | |
| 53 | abstract public function delete(); | |
| 54 | ||
| 55 | /** | |
| 56 | * Creates the filesystem object | |
| 57 | * | |
| 58 | * @param mixed $params Creation parameters, varies by implementation | |
| 59 | * @return bool True if successfully created, false otherwise | |
| 60 | */ | |
| 61 | abstract public function create(mixed $params) : bool; | |
| 62 | ||
| 63 | /** | |
| 64 | * Renames the filesystem object | |
| 65 | * | |
| 66 | * @param string $newName The new name for the filesystem object | |
| 67 | * @return bool True if successfully renamed, false otherwise | |
| 68 | * @throws \RuntimeException If the target already exists or the rename operation fails | |
| 69 | */ | |
| 70 | public function rename(string $newName): bool | |
| 71 | { | |
| 72 | // Get the directory path of the current object | |
| 73 | $directory = $this->getDirectory()->path; | |
| 74 | ||
| 75 | // Construct the new full path | |
| 76 | $newPath = $directory . DIRECTORY_SEPARATOR . $newName; | |
| 77 | ||
| 78 | // Check if the target already exists | |
| 79 | if (file_exists($newPath)) { | |
| 80 | throw new \RuntimeException("Cannot rename: target '{$newPath}' already exists"); | |
| 81 | } | |
| 82 | ||
| 83 | // Perform the rename operation | |
| 84 | if (rename($this->path, $newPath)) { | |
| 85 | // Update the path property to reflect the new path | |
| 86 | $this->path = $newPath; | |
| 87 | return true; | |
| 88 | } | |
| 89 | ||
| 90 | return false; | |
| 91 | } | |
| 92 | } |