Passed
Branch dev (152e5d)
by compolom
02:01
created

PathFixer::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 15
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Compolomus\FSHelper;
6
7
use Compolomus\FSHelper\Exceptions\PathNotFoundException;
8
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
68
    {
69
        return mb_substr($path, $this->trimLength);
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
88
    {
89
        return $this->trimLength;
90
    }
91
}
92