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

Composer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 10 2
A __construct() 0 11 1
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