Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.33% covered (success)
93.33%
28 / 30
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Render
93.33% covered (success)
93.33%
28 / 30
50.00% covered (danger)
50.00%
1 / 2
5.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
92.86% covered (success)
92.86%
26 / 28
0.00% covered (danger)
0.00%
0 / 1
4.01
 __toString
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Finder;
6
7use Cosray\Bootstrap;
8use Cosray\Cms;
9use Cosray\Context;
10use Cosray\Exception\RuntimeException;
11use Cosray\Node\Factory;
12use Cosray\Node\Types;
13use Cosray\Node\View;
14use Override;
15
16class Render
17{
18    protected object $node;
19
20    /** @param array<string, mixed> $templateContext */
21    public function __construct(
22        private readonly Context $context,
23        private readonly Cms $cms,
24        private readonly Factory $nodeFactory,
25        private readonly Types $types,
26        string $id,
27        private readonly array $templateContext = [],
28        ?bool $deleted = false,
29        ?bool $published = true,
30    ) {
31        $data = $this->context
32            ->db
33            ->nodes
34            ->find([
35                'handle' => $id,
36                'published' => $published,
37                'deleted' => $deleted,
38            ])
39            ->first() ?: $this->context
40            ->db
41            ->nodes
42            ->find([
43                'uid' => $id,
44                'published' => $published,
45                'deleted' => $deleted,
46            ])
47            ->first();
48
49        if (!$data) {
50            throw new RuntimeException('Renderable node not found: ' . $id);
51        }
52        $class = $this->context
53            ->container
54            ->tag(Bootstrap::NODE_TAG)
55            ->entry($data['type_handle'])
56            ->definition();
57
58        if (!(bool) $this->types->get($class, 'renderable', false)) {
59            throw new RuntimeException('Invalid renderable node class ' . $class);
60        }
61
62        $data['content'] = json_decode($data['content'], true);
63        $this->node = $this->nodeFactory->create($class, $context, $cms, $data);
64    }
65
66    #[Override]
67    public function __toString(): string
68    {
69        return new View($this->node, $this->cms, $this->context, $this->types)
70            ->output($this->templateContext);
71    }
72}