Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.24% covered (success)
95.24%
20 / 21
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
OnlineService
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
3 / 3
9
100.00% covered (success)
100.00%
1 / 1
 checkPlace
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
6
 openHelper
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 ping
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace UppServices;
6
7require_once __DIR__.'/legacy/ddbb/conx.php';
8
9/**
10 * Place-aware connectivity check for GET /online?place=.
11 *
12 * Probes the central DB and the tenant DB resolved via PLACE.tenant / TENANTNODE.
13 */
14class OnlineService
15{
16    /**
17     * Probe global + tenant for the given place.
18     *
19     * @param int|string $place Place objid.
20     * @return array{global: bool, tenant: bool}
21     */
22    public function checkPlace($place): array
23    {
24        $globalOk = false;
25        $tenantOk = false;
26
27        $conx = $this->openHelper();
28        try {
29            $global = $conx->getGlobal();
30            if ($global !== null && $global->connected()) {
31                $globalOk = $this->ping($global);
32            }
33
34            if ($globalOk) {
35                $conx->tenant = $place;
36                $tenant = $conx->getTenant();
37                if ($tenant !== null && $tenant->connected()) {
38                    $tenantOk = $this->ping($tenant);
39                }
40            }
41        } finally {
42            $conx->release();
43        }
44
45        return [
46            'global' => $globalOk,
47            'tenant' => $tenantOk,
48        ];
49    }
50
51    /**
52     * Opens a connection helper for this probe.
53     */
54    protected function openHelper(): \ConxHelper
55    {
56        return new \ConxHelper(__FILE__);
57    }
58
59    /**
60     * Trivial SELECT 1 against an open connection.
61     */
62    private function ping(\conx $conn): bool
63    {
64        $rows = [];
65        $ok = $conn->runsql([], 'SELECT 1 AS ok', $rows);
66
67        return $ok && count($rows) > 0;
68    }
69}