Completed
Push — develop ( afc25d...fe7998 )
by Baptiste
01:57
created

Relative::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 13
cts 13
cp 1
rs 9.2
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 2
crap 3
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Loader\PathResolver;
5
6
use Innmind\Compose\Loader\PathResolver;
7
use Innmind\Url\{
8
    PathInterface,
9
    Path
10
};
11
use Innmind\UrlResolver\{
12
    Path as ResolverPath,
13
    RelativePath
14
};
15
use Innmind\Immutable\Str;
16
17
final class Relative implements PathResolver
18
{
19 6
    public function __invoke(PathInterface $from, PathInterface $to): PathInterface
20
    {
21 6
        $target = Str::of((string) $to);
22
23 6
        if ((string) $target->substring(0, 1) === '/') {
24 1
            return $to;
25
        }
26
27 5
        $origin = Str::of((string) $from);
28 5
        $toRemove = 0;
29
30 5
        if ((string) $origin->substring(0, 1) !== '/') {
31 5
            $origin = $origin->prepend('/');
32 5
            ++$toRemove;
33
        }
34
35 5
        $target = (new ResolverPath((string) $origin))->pointingTo(
36 5
            new RelativePath((string) $to)
37
        );
38
39 5
        return new Path(
40 5
            (string) Str::of((string) $target)->substring($toRemove)
41
        );
42
    }
43
}
44