Passed
Push — master ( e1151c...ed57da )
by Caen
03:25 queued 12s
created

AnonymousViewCompiler::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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