HealthzController   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 52
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 35 1
A __construct() 0 4 1
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Controller/HealthzController.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Controller;
10
11
use App\Rest\Interfaces\ResponseHandlerInterface;
12
use App\Rest\ResponseHandler;
13
use App\Utils\HealthzService;
14
use OpenApi\Attributes as OA;
15
use OpenApi\Attributes\JsonContent;
16
use OpenApi\Attributes\Property;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpKernel\Attribute\AsController;
20
use Symfony\Component\Routing\Annotation\Route;
21
use Throwable;
22
23
/**
24
 * Class HealthzController
25
 *
26
 * @package App\Controller
27
 * @author TLe, Tarmo Leppänen <[email protected]>
28
 */
29
#[AsController]
30
class HealthzController
31
{
32 3
    public function __construct(
33
        private readonly ResponseHandler $responseHandler,
34
        private readonly HealthzService $healthzService,
35
    ) {
36 3
    }
37
38
    /**
39
     * Route for application health check. This action will make some simple
40
     * tasks to ensure that application is up and running like expected.
41
     *
42
     * @see https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/
43
     *
44
     * @throws Throwable
45
     */
46 3
    #[Route(
47
        path: '/healthz',
48
        methods: [Request::METHOD_GET],
49
    )]
50
    #[OA\Get(
51
        operationId: 'healthz',
52
        responses: [
53
            new OA\Response(
54
                response: 200,
55
                description: 'success',
56
                content: new JsonContent(
57
                    properties: [
58
                        new Property(
59
                            property: 'timestamp',
60
                            description: 'Timestamp when health check was performed',
61
                            type: 'string',
62
                        ),
63
                    ],
64
                    type: 'object',
65
                    example: [
66
                        'timestamp' => '2018-01-01T13:08:05+00:00',
67
                    ],
68
                ),
69
            ),
70
        ],
71
    )]
72
    public function __invoke(Request $request): Response
73
    {
74 3
        return $this->responseHandler->createResponse(
75 3
            $request,
76 3
            $this->healthzService->check(),
77 3
            format: ResponseHandlerInterface::FORMAT_JSON,
78 3
            context: [
79 3
                'groups' => [
80 3
                    'Healthz.timestamp',
81 3
                ],
82 3
            ],
83 3
        );
84
    }
85
}
86