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