Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.59% covered (success)
94.59%
35 / 37
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Node
94.59% covered (success)
94.59%
35 / 37
66.67% covered (warning)
66.67%
4 / 6
8.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 byPath
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 byUid
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 byHandle
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 get
95.00% covered (success)
95.00%
19 / 20
0.00% covered (danger)
0.00%
0 / 1
3
 find
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
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\Node\Factory;
11use Cosray\Node\Node as NodeWrapper;
12use Cosray\Node\Types;
13use Cosray\Plugin;
14
15class Node
16{
17    public function __construct(
18        private readonly Context $context,
19        private readonly Cms $cms,
20        private readonly Factory $nodeFactory,
21        private readonly Types $types,
22    ) {}
23
24    public function byPath(
25        string $path,
26        ?bool $deleted = false,
27        ?bool $published = true,
28    ): ?NodeWrapper {
29        return $this->get([
30            'path' => $path,
31            'published' => $published,
32            'deleted' => $deleted,
33        ]);
34    }
35
36    public function byUid(
37        string $uid,
38        ?bool $deleted = false,
39        ?bool $published = true,
40    ): ?NodeWrapper {
41        return $this->get([
42            'uid' => $uid,
43            'published' => $published,
44            'deleted' => $deleted,
45        ]);
46    }
47
48    public function byHandle(
49        string $handle,
50        ?bool $deleted = false,
51        ?bool $published = true,
52    ): ?NodeWrapper {
53        return $this->get([
54            'handle' => $handle,
55            'published' => $published,
56            'deleted' => $deleted,
57        ]);
58    }
59
60    public function get(
61        array $params,
62    ): ?NodeWrapper {
63        $data = $this->context
64            ->db
65            ->nodes
66            ->find($params)
67            ->first();
68
69        if (!$data) {
70            return null;
71        }
72
73        $data['content'] = json_decode($data['content'], true);
74        $data['editor_data'] = json_decode($data['editor_data'], true);
75        $data['creator_data'] = json_decode($data['creator_data'], true);
76        $data['paths'] = json_decode($data['paths'], true);
77        $class = $this->context
78            ->container
79            ->tag(Plugin::NODE_TAG)
80            ->entry($data['type_handle'])
81            ->definition();
82
83        if ($this->types->isNode($class)) {
84            $node = $this->nodeFactory->create($class, $this->context, $this->cms, $data);
85
86            return $this->nodeFactory->proxy($node, $this->context->request, $this->context, $this->cms);
87        }
88
89        throw new HttpBadRequest($this->context->request);
90    }
91
92    public function find(
93        string $query,
94    ): array {
95        return [];
96    }
97}