Completed
Push — develop ( 16e25e...7d2249 )
by Baptiste
01:52
created

Composer::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Loader\PathResolver;
5
6
use Innmind\Compose\{
7
    Loader\PathResolver,
8
    Exception\ValueNotSupported
9
};
10
use Innmind\Url\{
11
    PathInterface,
12
    Path
13
};
14
use Innmind\Immutable\{
15
    Str,
16
    Sequence
17
};
18
use Composer\Autoload\ClassLoader;
19
20
final class Composer implements PathResolver
21
{
22
    private $vendorPath;
23
24
    public function __construct()
25
    {
26 8
        $composer = Sequence::of(...get_declared_classes())->filter(static function(string $class): bool {
27 8
            return (string) Str::of($class)->substring(0, 22) === 'ComposerAutoloaderInit';
28 8
        });
29
30
        //use the last as there may be 2 classes, the first being the global autoloader
31 8
        $refl = new \ReflectionClass((string) $composer->last());
32 8
        $vendorPath = dirname(dirname($refl->getFileName()));
33
34 8
        $this->vendorPath = Str::of($vendorPath)->append('/');
35 8
    }
36
37 4
    public function __invoke(PathInterface $from, PathInterface $to): PathInterface
38
    {
39 4
        $target = Str::of((string) $to);
40
41 4
        if (!$target->matches('|^@[a-zA-Z0-9]+/[a-zA-Z0-9]+/.+|')) {
42 3
            throw new ValueNotSupported((string) $to);
43
        }
44
45 1
        return new Path(
46 1
            (string) $this->vendorPath->append((string) $target->substring(1))
47
        );
48
    }
49
}
50