Passed
Push — main ( af6a91...92bf86 )
by Thomas
03:24
created

Render::response()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.9666
cc 2
nc 2
nop 3
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Conia\Chuck\Renderer;
6
7
use Attribute;
8
use Closure;
9
use Conia\Chuck\Registry\Registry;
10
use Conia\Chuck\Renderer\Renderer;
11
use Conia\Chuck\Request;
12
use Conia\Chuck\Response;
13
14
#[Attribute]
15
class Render
16
{
17
    protected array $args;
18
19 14
    public function __construct(protected string $renderer, mixed ...$args)
20
    {
21 14
        $this->args = $args;
22
    }
23
24 9
    public function response(Request $request, Registry $registry, mixed $data): Response
25
    {
26 9
        $entry = $registry->tag(Renderer::class)->entry($this->renderer);
27 9
        $class = $entry->definition();
28 9
        $options = $entry->getArgs();
29
30 9
        if ($options instanceof Closure) {
31
            /** @var mixed */
32 1
            $options = $options();
33
        }
34
35 9
        assert(is_string($class));
36 9
        assert(is_subclass_of($class, Renderer::class));
37 9
        $renderer = new $class($request, $registry, $this->args, $options);
38
39 9
        return $renderer->response($data);
40
    }
41
}
42