HealthAction::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Rest\Action;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Laminas\Diactoros\Response\JsonResponse;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Shlinkio\Shlink\Core\Options\AppOptions;
12
use Throwable;
13
14
class HealthAction extends AbstractRestAction
15
{
16
    private const HEALTH_CONTENT_TYPE = 'application/health+json';
17
    private const STATUS_PASS = 'pass';
18
    private const STATUS_FAIL = 'fail';
19
20
    protected const ROUTE_PATH = '/health';
21
    protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
22
23
    private EntityManagerInterface $em;
24
    private AppOptions $options;
25
26 3
    public function __construct(EntityManagerInterface $em, AppOptions $options)
27
    {
28 3
        $this->em = $em;
29 3
        $this->options = $options;
30 3
    }
31
32
    /**
33
     * Handles a request and produces a response.
34
     *
35
     * May call other collaborating code to generate the response.
36
     */
37 3
    public function handle(ServerRequestInterface $request): ResponseInterface
38
    {
39
        try {
40 3
            $connected = $this->em->getConnection()->ping();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::ping() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

40
            $connected = /** @scrutinizer ignore-deprecated */ $this->em->getConnection()->ping();
Loading history...
41 1
        } catch (Throwable $e) {
42 1
            $connected = false;
43
        }
44
45 3
        $statusCode = $connected ? self::STATUS_OK : self::STATUS_SERVICE_UNAVAILABLE;
46 3
        return new JsonResponse([
47 3
            'status' => $connected ? self::STATUS_PASS : self::STATUS_FAIL,
48 3
            'version' => $this->options->getVersion(),
49
            'links' => [
50
                'about' => 'https://shlink.io',
51
                'project' => 'https://github.com/shlinkio/shlink',
52
            ],
53 3
        ], $statusCode, ['Content-type' => self::HEALTH_CONTENT_TYPE]);
54
    }
55
}
56