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

AnonymousViewCompiler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 10
c 5
b 0
f 0
dl 0
loc 20
rs 10
wmc 3

2 Methods

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