Passed
Push — master ( b815ca...9da17d )
by Caen
03:13 queued 12s
created

AnonymousViewCompiler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Actions;
6
7
use Hyde\Facades\Filesystem;
8
use Hyde\Framework\Exceptions\FileNotFoundException;
9
use Illuminate\Support\Facades\Blade;
10
11
/**
12
 * Compile any Blade file using the Blade facade as it allows us to render
13
 * it without having to register the directory with the view finder.
14
 */
15
class AnonymousViewCompiler
16
{
17
    protected string $viewPath;
18
    protected array $data;
19
20
    public static function call(string $viewPath, array $data = []): string
21
    {
22
        return (new self($viewPath, $data))->__invoke();
23
    }
24
25
    public function __construct(string $viewPath, array $data = [])
26
    {
27
        $this->viewPath = $viewPath;
28
        $this->data = $data;
29
    }
30
31
    public function __invoke(): string
32
    {
33
        if (Filesystem::missing($this->viewPath)) {
34
            throw new FileNotFoundException($this->viewPath);
35
        }
36
37
        return Blade::render(
38
            Filesystem::getContents($this->viewPath),
39
            $this->data
40
        );
41
    }
42
}
43