Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.74% covered (success)
94.74%
36 / 38
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Node
94.74% covered (success)
94.74%
36 / 38
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.24% covered (success)
95.24%
20 / 21
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 Celema\Core\Exception\HttpBadRequest;
8use Cosray\Bootstrap;
9use Cosray\Cms;
10use Cosray\Context;
11use Cosray\Node\Factory;
12use Cosray\Node\Types;
13use Cosray\Node\Wrapper;
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    ): ?Wrapper {
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    ): ?Wrapper {
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    ): ?Wrapper {
53        return $this->get([
54            'handle' => $handle,
55            'published' => $published,
56            'deleted' => $deleted,
57        ]);
58    }
59
60    public function get(
61        array $params,
62    ): ?Wrapper {
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['title'] = json_decode($data['title'], true);
75        $data['editor_data'] = json_decode($data['editor_data'], true);
76        $data['creator_data'] = json_decode($data['creator_data'], true);
77        $data['paths'] = json_decode($data['paths'], true);
78        $class = $this->context
79            ->container
80            ->tag(Bootstrap::NODE_TAG)
81            ->entry($data['type_handle'])
82            ->definition();
83
84        if ($this->types->isNode($class)) {
85            $node = $this->nodeFactory->create($class, $this->context, $this->cms, $data);
86
87            return $this->nodeFactory->proxy($node, $this->context, $this->cms);
88        }
89
90        throw new HttpBadRequest($this->context->request);
91    }
92
93    public function find(
94        string $query,
95    ): array {
96        return [];
97    }
98}