Completed
Push — master ( d0a237...a1b9c8 )
by Freek
9s
created

RegularDirectory::getAbsoluteDirectory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Spatie\BladeX\ComponentDirectory;
4
5
use Illuminate\Support\Facades\View;
6
use Spatie\BladeX\Exceptions\CouldNotRegisterComponent;
7
use Symfony\Component\Finder\SplFileInfo;
8
9
class RegularDirectory extends ComponentDirectory
10
{
11
    /** @var string */
12
    protected $viewDirectory;
13
14
    public function __construct(string $viewDirectory)
15
    {
16
        $this->viewDirectory = str_before($viewDirectory, '.*');
17
    }
18
19
    public function getAbsoluteDirectory(): string
20
    {
21
        $viewPath = str_replace('.', '/', $this->viewDirectory);
22
23
        $absoluteDirectory = collect(View::getFinder()->getPaths())
24
            ->map(function(string $path) use ($viewPath) {
25
                return realpath($path . '/' . $viewPath);
26
            })
27
            ->filter()
28
            ->first();
29
30
        if (! $absoluteDirectory) {
31
            throw CouldNotRegisterComponent::viewPathNotFound($viewPath);
32
        }
33
34
        return $absoluteDirectory;
35
    }
36
37
    public function getViewName(SplFileInfo $viewFile): string
38
    {
39
        $view = str_replace_last('.blade.php', '', $viewFile->getFilename());
40
41
        return "{$this->viewDirectory}.{$view}";
42
    }
43
}