Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Usage
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
5 / 5
12
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 forAsset
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 forNode
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 row
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 title
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\References;
6
7use Celema\Quma\Database;
8
9/**
10 * Answers "who points at this?" from the reference indexes, enriched
11 * for display: node owners carry their type handle, published state,
12 * and best-effort title (the conventional `title` field; menu owners
13 * fall back to the item's title map).
14 */
15final class Usage
16{
17    public function __construct(
18        private readonly Database $db,
19    ) {}
20
21    /** @return list<array{ownerType: string, ownerUid: string, title: string, nodeType: ?string, published: ?bool}> */
22    public function forAsset(string $uid): array
23    {
24        return array_map(
25            $this->row(...),
26            $this->db->references->assetUsage(['uid' => $uid])->all(),
27        );
28    }
29
30    /** @return list<array{ownerType: string, ownerUid: string, title: string, nodeType: ?string, published: ?bool}> */
31    public function forNode(string $uid): array
32    {
33        return array_map(
34            $this->row(...),
35            $this->db->references->nodeUsage(['uid' => $uid])->all(),
36        );
37    }
38
39    /**
40     * @param array<string, mixed> $row
41     * @return array{ownerType: string, ownerUid: string, title: string, nodeType: ?string, published: ?bool}
42     */
43    private function row(array $row): array
44    {
45        return [
46            'ownerType' => (string) $row['ownerType'],
47            'ownerUid' => (string) $row['ownerUid'],
48            'title' => $this->title($row['title'] ?? null),
49            'nodeType' => is_string($row['nodeType'] ?? null) ? $row['nodeType'] : null,
50            'published' => is_bool($row['published'] ?? null) ? $row['published'] : null,
51        ];
52    }
53
54    private function title(mixed $value): string
55    {
56        $map = is_string($value) ? json_decode($value, true) : null;
57
58        foreach (is_array($map) ? $map : [] as $title) {
59            if (is_string($title) && $title !== '') {
60                return $title;
61            }
62        }
63
64        return '';
65    }
66}