-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStatus.php
More file actions
72 lines (60 loc) · 2.18 KB
/
Status.php
File metadata and controls
72 lines (60 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
declare(strict_types=1);
namespace MatchBot\Application\Actions;
use Doctrine\Common\Proxy\ProxyGenerator;
use Doctrine\DBAL\Exception as DBALException;
use Doctrine\ORM\EntityManagerInterface;
use JetBrains\PhpStorm\Pure;
use MatchBot\Application\Assertion;
use MatchBot\Domain\Campaign;
use MatchBot\Domain\CampaignFunding;
use MatchBot\Domain\Charity;
use MatchBot\Domain\Donation;
use MatchBot\Domain\FundingWithdrawal;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface;
use Redis;
class Status extends Action
{
#[Pure]
public function __construct(
private EntityManagerInterface $entityManager,
private ?Redis $redis,
LoggerInterface $logger,
) {
parent::__construct($logger);
}
/**
* @return Response
*/
#[\Override]
protected function action(Request $request, Response $response, array $args): Response
{
/** @var string|null $errorMessage */
$errorMessage = null;
if ($this->redis === null || !$this->redis->isConnected()) {
$errorMessage = 'Redis not connected';
}
try {
$connection = $this->entityManager->getConnection();
// dummy query just to force DB connection to be made, since
// \Doctrine\DBAL\Connection::connect is marked @internal and will be protected in DBAL 4
$connection->executeQuery('SELECT 1');
$gotDbConnection = $connection->isConnected();
if (!$gotDbConnection) {
$errorMessage = 'Database not connected';
}
} catch (DBALException) {
$errorMessage = 'Database connection failed';
}
if ($errorMessage === null) {
if (($request->getQueryParams()['ping'] ?? null) === 'ping') {
return $this->respondWithData($response, ['pong']);
}
return $this->respondWithData($response, ['status' => 'OK']);
}
$error = new ActionError(ActionError::SERVER_ERROR, $errorMessage);
return $this->respond($response, new ActionPayload(500, ['error' => $errorMessage], $error));
}
}