Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| Index | |
100.00% |
23 / 23 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| index | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
3 | |||
| missing | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Api\Controller; |
| 6 | |
| 7 | use Celema\Core\Exception\HttpNotFound; |
| 8 | use Celema\Core\Request; |
| 9 | use Cosray\Api\Responses; |
| 10 | use Cosray\Config; |
| 11 | use Cosray\Locales; |
| 12 | use Cosray\Node\Types; |
| 13 | use Cosray\Security\NodeAccess; |
| 14 | use Cosray\User; |
| 15 | use Psr\Http\Message\ResponseInterface as Response; |
| 16 | |
| 17 | final class Index |
| 18 | { |
| 19 | public function __construct( |
| 20 | private readonly Responses $responses, |
| 21 | ) {} |
| 22 | |
| 23 | /** What a client needs to start: the locales, the types it may read and who its token acts as. */ |
| 24 | public function index( |
| 25 | Request $request, |
| 26 | Config $config, |
| 27 | Locales $locales, |
| 28 | NodeAccess $access, |
| 29 | Types $types, |
| 30 | ): Response { |
| 31 | $user = $request->get('user', null); |
| 32 | $user = $user instanceof User ? $user : null; |
| 33 | $readable = $access->readable($user); |
| 34 | $registered = $access->registered(); |
| 35 | $handles = [...$readable['any'], ...$readable['live']]; |
| 36 | sort($handles); |
| 37 | |
| 38 | return $this->responses->json([ |
| 39 | 'name' => $config->app->name, |
| 40 | 'defaultLocale' => $locales->getDefault()->id, |
| 41 | 'locales' => array_values(array_map(static fn($locale): array => [ |
| 42 | 'id' => $locale->id, |
| 43 | 'title' => $locale->title, |
| 44 | 'fallbacks' => $locale->fallbacks(), |
| 45 | ], iterator_to_array($locales))), |
| 46 | 'types' => array_map(static fn(string $handle): array => [ |
| 47 | 'handle' => $handle, |
| 48 | 'label' => __((string) $types->get($registered[$handle], 'label')), |
| 49 | 'routable' => (bool) $types->get($registered[$handle], 'routable', false), |
| 50 | ], $handles), |
| 51 | 'user' => $user === null ? null : ['uid' => $user->uid, 'name' => $user->name ?? $user->loginName()], |
| 52 | ]); |
| 53 | } |
| 54 | |
| 55 | public function missing(Request $request): never |
| 56 | { |
| 57 | throw new HttpNotFound($request, payload: ['message' => 'The API has no such endpoint.']); |
| 58 | } |
| 59 | } |