Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
35.19% |
19 / 54 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| Nodes | |
35.19% |
19 / 54 |
|
50.00% |
1 / 2 |
67.37 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
33.96% |
18 / 53 |
|
0.00% |
0 / 1 |
61.67 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Controller; |
| 6 | |
| 7 | use Celemas\Container\Container; |
| 8 | use Celemas\Core\Exception\HttpBadRequest; |
| 9 | use Celemas\Core\Factory\Factory; |
| 10 | use Celemas\Core\Request; |
| 11 | use Celemas\Core\Response; |
| 12 | use Cosray\Cms; |
| 13 | use Cosray\Config; |
| 14 | use Cosray\Locales; |
| 15 | use Cosray\Middleware\Permission; |
| 16 | use Cosray\Node\Factory as NodeFactory; |
| 17 | use Cosray\Node\Serializer; |
| 18 | use Cosray\Node\Types; |
| 19 | use stdClass; |
| 20 | |
| 21 | class Nodes |
| 22 | { |
| 23 | public function __construct( |
| 24 | protected readonly Request $request, |
| 25 | protected readonly Config $config, |
| 26 | protected readonly Container $container, |
| 27 | protected readonly Locales $locales, |
| 28 | protected readonly Types $types, |
| 29 | ) {} |
| 30 | |
| 31 | #[Permission('panel')] |
| 32 | public function get(Cms $cms, Factory $factory): Response |
| 33 | { |
| 34 | if ($this->request->method() === 'GET') { |
| 35 | $query = new GetQuery($this->request); |
| 36 | } else { |
| 37 | $query = new PostQuery($this->request); |
| 38 | } |
| 39 | |
| 40 | if ($query->query) { |
| 41 | $nodes = $cms->nodes($query->query); |
| 42 | } elseif (count($query->uids) > 0) { |
| 43 | if (count($query->uids) > 1) { |
| 44 | $quoted = implode(',', array_map(static fn($uid) => "'{$uid}'", $query->uids)); |
| 45 | $queryString = "uid @ [{$quoted}]"; |
| 46 | } else { |
| 47 | $queryString = "uid = '{$query->uids[0]}'"; |
| 48 | } |
| 49 | |
| 50 | $nodes = $cms->nodes($queryString); |
| 51 | } else { |
| 52 | throw new HttpBadRequest($this->request); |
| 53 | } |
| 54 | |
| 55 | $nodeFactory = $cms->nodeFactory(); |
| 56 | $hydrator = $nodeFactory->hydrator(); |
| 57 | $serializer = new Serializer($hydrator, $this->types, $nodeFactory->uid()); |
| 58 | $result = []; |
| 59 | |
| 60 | foreach ($nodes |
| 61 | ->published($query->published) |
| 62 | ->hidden($query->hidden) |
| 63 | ->order($query->order) |
| 64 | ->deleted($query->deleted) as $node) { |
| 65 | $uid = $node->meta->uid; |
| 66 | $n = [ |
| 67 | 'uid' => $uid, |
| 68 | 'title' => $node->title(), |
| 69 | 'handle' => $node->meta->handle, |
| 70 | 'published' => $node->meta->published, |
| 71 | 'hidden' => $node->meta->hidden, |
| 72 | 'locked' => $node->meta->locked, |
| 73 | 'created' => $node->meta->created, |
| 74 | 'changed' => $node->meta->changed, |
| 75 | 'deleted' => $node->meta->deleted, |
| 76 | 'paths' => $node->meta->paths, |
| 77 | ]; |
| 78 | |
| 79 | foreach ($query->fields as $field) { |
| 80 | if (!$field) { |
| 81 | continue; |
| 82 | } |
| 83 | |
| 84 | $fieldName = trim($field); |
| 85 | $fieldObj = $hydrator->getField($node, $fieldName); |
| 86 | $value = $fieldObj->value(); |
| 87 | $n[$field] = $value->isset() ? $value->unwrap() : null; |
| 88 | } |
| 89 | |
| 90 | if ($query->content) { |
| 91 | $n['content'] = $serializer->content( |
| 92 | $node, |
| 93 | NodeFactory::dataFor($node), |
| 94 | NodeFactory::fieldNamesFor($node), |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | if ($query->map) { |
| 99 | $result[$uid] = $n; |
| 100 | } else { |
| 101 | $result[] = $n; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if (count($result) === 0 && $query->map) { |
| 106 | $result = new stdClass(); |
| 107 | } |
| 108 | |
| 109 | return new Response($factory->response())->json($result); |
| 110 | } |
| 111 | } |