Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.96% covered (warning)
86.96%
40 / 46
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Render
86.96% covered (warning)
86.96%
40 / 46
0.00% covered (danger)
0.00%
0 / 2
7.11
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
77.78% covered (warning)
77.78%
14 / 18
0.00% covered (danger)
0.00%
0 / 1
3.10
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Finder;
6
7use Celemas\Core\Exception\HttpBadRequest;
8use Cosray\Cms;
9use Cosray\Context;
10use Cosray\Exception\RuntimeException;
11use Cosray\Node\Factory;
12use Cosray\Node\Types;
13use Cosray\Node\ViewRenderer;
14use Cosray\Plugin;
15use Throwable;
16
17class Render
18{
19    protected object $node;
20
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            ->one() ?: $this->context
40            ->db
41            ->nodes
42            ->find([
43                'uid' => $id,
44                'published' => $published,
45                'deleted' => $deleted,
46            ])
47            ->one();
48
49        if (!$data) {
50            throw new RuntimeException('Renderable node not found: ' . $id);
51        }
52        $class = $this->context
53            ->container
54            ->tag(Plugin::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    public function __toString(): string
67    {
68        try {
69            $renderer = new ViewRenderer(
70                $this->context->container,
71                $this->context->factory,
72                $this->nodeFactory->hydrator(),
73                $this->types,
74            );
75
76            return $renderer->renderNode(
77                $this->node,
78                Factory::fieldNamesFor($this->node),
79                $this->cms,
80                $this->context->request,
81                $this->context->config,
82                $this->templateContext,
83            );
84        } catch (Throwable $e) {
85            if ($this->context->config->debug()) {
86                throw $e;
87            }
88
89            throw new HttpBadRequest($this->context->request, previous: $e);
90        }
91    }
92}