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

AnonymousViewCompiler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 25
rs 10
wmc 4

3 Methods

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