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

RegularDirectory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 35
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getAbsoluteDirectory() 0 17 2
A getViewName() 0 6 1
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
}