| Total Complexity | 6 |
| Total Lines | 81 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
| 1 | <?php |
||
| 9 | class PathFixer |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Absolute or relative path |
||
| 13 | * |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | private string $path; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Amount of symbols which can be removed from provided path |
||
| 20 | * |
||
| 21 | * @var int |
||
| 22 | */ |
||
| 23 | private int $trimLength; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * PathFixer constructor. |
||
| 27 | * |
||
| 28 | * @param string $path |
||
| 29 | * |
||
| 30 | * @throws \Compolomus\FSHelper\Exceptions\PathNotFoundException |
||
| 31 | */ |
||
| 32 | public function __construct(string $path) |
||
| 33 | { |
||
| 34 | $this->path = $path; |
||
| 35 | $this->execute(); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Provided path processing |
||
| 40 | * |
||
| 41 | * @throws \Compolomus\FSHelper\Exceptions\PathNotFoundException |
||
| 42 | */ |
||
| 43 | private function execute(): void |
||
| 44 | { |
||
| 45 | // Get rale path from provided path (because it can be relative) |
||
| 46 | $real = realpath($this->path); |
||
| 47 | |||
| 48 | // Throw an exception if the path does not exist |
||
| 49 | if (!$real) { |
||
| 50 | throw new PathNotFoundException($this->path); |
||
| 51 | } |
||
| 52 | |||
| 53 | // Get directory name by provided real path |
||
| 54 | $base = basename($real); |
||
| 55 | |||
| 56 | // Calculate trimmed length between real path and directory only |
||
| 57 | $this->trimLength = mb_strlen($real) - mb_strlen($base); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Remove required amount of characters from start of provided path |
||
| 62 | * |
||
| 63 | * @param string $path |
||
| 64 | * |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | public function fix(string $path): string |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Return path added via constructor |
||
| 74 | * |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | public function getPath(): string |
||
| 78 | { |
||
| 79 | return $this->path; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Return amount of symbols which will be trimmed |
||
| 84 | * |
||
| 85 | * @return int |
||
| 86 | */ |
||
| 87 | public function getTrimLength(): int |
||
| 90 | } |
||
| 91 | } |
||
| 92 |