Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
32 / 32 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| InstanceContextApiController | |
100.00% |
31 / 31 |
|
100.00% |
4 / 4 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| actionList | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
3 | |||
| actionSwitch | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
| createJsonResponse | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** @license For full copyright and license information, please see the LICENSE.md file. */ |
| 6 | |
| 7 | namespace App\Core\Instance\Presentation\Api; |
| 8 | |
| 9 | defined('AMMONLY_APP') || exit('Direct script access is forbidden.'); |
| 10 | |
| 11 | use App\Core\Instance\Application\Service\InstanceContextManagerInterface; |
| 12 | use Psr\Http\Message\ResponseFactoryInterface; |
| 13 | use Psr\Http\Message\ResponseInterface; |
| 14 | use Psr\Http\Message\ServerRequestInterface; |
| 15 | |
| 16 | /** |
| 17 | * Instance Context REST API Controller. |
| 18 | * |
| 19 | * Provides endpoints for retrieving active instances and switching application context. |
| 20 | * |
| 21 | * @package App\Core\Instance\Presentation\Api |
| 22 | */ |
| 23 | final readonly class InstanceContextApiController |
| 24 | { |
| 25 | /** |
| 26 | * InstanceContextApiController constructor. |
| 27 | * |
| 28 | * @param InstanceContextManagerInterface $contextManager Context manager service. |
| 29 | * @param ResponseFactoryInterface $responseFactory PSR-7 response factory. |
| 30 | */ |
| 31 | public function __construct( |
| 32 | private InstanceContextManagerInterface $contextManager, |
| 33 | private ResponseFactoryInterface $responseFactory |
| 34 | ) { |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Returns list of available client instances and current active context. |
| 39 | * |
| 40 | * @return ResponseInterface JSON response. |
| 41 | */ |
| 42 | public function actionList(): ResponseInterface |
| 43 | { |
| 44 | $activeInstance = $this->contextManager->getActiveInstance(); |
| 45 | $instances = $this->contextManager->getAllActiveInstances(); |
| 46 | |
| 47 | $instancesData = []; |
| 48 | foreach ($instances as $inst) { |
| 49 | $instancesData[] = $inst->toArray(); |
| 50 | } |
| 51 | |
| 52 | $payload = [ |
| 53 | 'success' => true, |
| 54 | 'data' => [ |
| 55 | 'active_id' => $activeInstance !== null ? $activeInstance->id : 0, |
| 56 | 'active' => $activeInstance?->toArray(), |
| 57 | 'is_remote' => $this->contextManager->isRemote(), |
| 58 | 'instances' => $instancesData, |
| 59 | ], |
| 60 | ]; |
| 61 | |
| 62 | return $this->createJsonResponse($payload); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Switches active instance context. |
| 67 | * |
| 68 | * @param ServerRequestInterface $request Incoming HTTP request with JSON body. |
| 69 | * @return ResponseInterface JSON response. |
| 70 | */ |
| 71 | public function actionSwitch(ServerRequestInterface $request): ResponseInterface |
| 72 | { |
| 73 | $body = (string) $request->getBody(); |
| 74 | $data = json_decode($body, true); |
| 75 | $instanceId = (int) ($data['instance_id'] ?? 0); |
| 76 | |
| 77 | $success = $this->contextManager->switchToInstance($instanceId); |
| 78 | |
| 79 | $payload = [ |
| 80 | 'success' => $success, |
| 81 | 'data' => [ |
| 82 | 'active_id' => $this->contextManager->getActiveInstanceId() ?? 0, |
| 83 | 'is_remote' => $this->contextManager->isRemote(), |
| 84 | ], |
| 85 | ]; |
| 86 | |
| 87 | return $this->createJsonResponse($payload, $success ? 200 : 400); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Helper to create PSR-7 JSON response. |
| 92 | * |
| 93 | * @param array<string, mixed> $data Data to encode. |
| 94 | * @param int $status HTTP status code. |
| 95 | * @return ResponseInterface PSR-7 response. |
| 96 | */ |
| 97 | private function createJsonResponse(array $data, int $status = 200): ResponseInterface |
| 98 | { |
| 99 | $response = $this->responseFactory->createResponse($status); |
| 100 | $response->getBody()->write((string) json_encode($data, JSON_THROW_ON_ERROR)); |
| 101 | |
| 102 | return $response->withHeader('Content-Type', 'application/json; charset=UTF-8'); |
| 103 | } |
| 104 | } |