FullPath::getDirname()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Koriym\AppStateDiagram;
6
7
use Koriym\AppStateDiagram\Exception\AlpsFileNotReadableException;
8
9
use function dirname;
10
use function explode;
11
use function file_exists;
12
use function sprintf;
13
use function strrpos;
14
use function substr;
15
16
final class FullPath
17
{
18
    public function __invoke(string $alpsFile, string $href): string
19
    {
20
        if (file_exists($href)) {
21
            return $href;
22
        }
23
24
        if (substr($href, 0, 4) === 'http') {
25
            return $href;
26
        }
27
28
        if (substr($href, 0, 1) === '#') {
29
            return sprintf('%s%s', $alpsFile, $href);
30
        }
31
32
        [$file] = explode('#', $href);
33
        if (file_exists($file)) {
34
            return $href;
35
        }
36
37
        $dirName = $this->getDirname($alpsFile);
38
39
        return sprintf('%s/%s', $dirName, $href);
40
    }
41
42
    private function getDirname(string $alpsFile): string
43
    {
44
        if (file_exists($alpsFile)) {
45
            return dirname($alpsFile);
46
        }
47
48
        $pos = strrpos($alpsFile, '/');
49
        if ($pos === false) {
50
            throw new AlpsFileNotReadableException($alpsFile);
51
        }
52
53
        return substr($alpsFile, 0, (int) strrpos($alpsFile, '/'));
54
    }
55
}
56