Passed
Push — master ( 7fa044...a851a0 )
by compolom
03:30 queued 02:09
created

PathFixer::getTrimLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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
    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
57
    {
58 3
        return mb_substr($path, $this->trimLength);
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
73
    {
74 1
        return $this->trimLength;
75
    }
76
}
77