Lines 84.00% 21 / 25
Methods 66.66% 2 / 3
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 readResponse 75.00% 12 / 16 0.00% 0 / 1 2.06
 assertStatusAccess 100.00% 6 / 6 100.00% 1 / 1 4
 [BO\Zmsbackend\Api\BaseController] __invoke 100.00% 3 / 3 100.00% 1 / 1 1
16class StatusGet extends \BO\Zmsbackend\Api\BaseController
17{
18    /**
19     * @SuppressWarnings(Param)
20     * @return \Psr\Http\Message\ResponseInterface
21     */
22    #[\Override]
23    public function readResponse(
24        \Psr\Http\Message\RequestInterface $request,
25        \Psr\Http\Message\ResponseInterface $response,
26        array $args
27    ) {
28        $this->assertStatusAccess($request);
29
30        $validator = $request->getAttribute('validator');
31        $includeProcessStats = $validator->getParameter('includeProcessStats')->isNumber()->setDefault(1)->getValue();
32        $status = (new \BO\Zmsbackend\Status\Service\Status())->readEntity(\App::$now, $includeProcessStats);
33        $status['version'] = \BO\Zmsbackend\Helper\Version::getArray();
34        $status['phpVersion'] = PHP_VERSION;
35        if (\App::DEBUG) {
36            $status['opcache'] = [
37                'config' => opcache_get_configuration(),
38                'status' => opcache_get_status(false)
39            ];
40        }
41
42        $message = \BO\Zmsbackend\Api\Response\Message::create($request);
43        $message->data = $status;
44
45        $response = Render::withLastModified($response, time(), '0');
46        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
47        return $response;
48    }
49
50    /**
51     * Allow access with a logged-in workstation (admin UI) or X-Token matching
52     * ZMS_CONFIG_SECURE_TOKEN (Grafana / status-logger). Same idea as ConfigGet,
53     * with strict token comparison and a narrow auth-exception catch.
54     */
55    private function assertStatusAccess(\Psr\Http\Message\RequestInterface $request): void
56    {
57        try {
58            (new \BO\Zmsbackend\Helper\User($request))->checkPermissions();
59            return;
60        } catch (UserAccountMissingLogin | UserAccountMissingRights $exception) {
61            // Fall through to X-Token for scrapers / unauthenticated callers.
62        }
63
64        $token = $request->getHeaderLine('X-Token');
65        if ($token === '' || !hash_equals((string) \App::SECURE_TOKEN, $token)) {
66            throw new StatusAuthenticationFailed();
67        }
68    }
69}

Inherited from BO\Zmsbackend\Api\BaseController

20    public function __invoke(RequestInterface $request, ResponseInterface $response, array $args)
21    {
22        $request = $this->initRequest($request);
23        $noCacheResponse = \BO\Slim\Render::withLastModified($response, time(), '0');
24        return $this->readResponse($request, $noCacheResponse, $args);
25    }