Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
77.14% covered (warning)
77.14%
27 / 35
87.50% covered (success)
87.50%
7 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ClientsService
77.14% covered (warning)
77.14%
27 / 35
87.50% covered (success)
87.50%
7 / 8
17.69
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 refresh
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 delete
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 update
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 cancel
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 notify
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 unload
70.37% covered (warning)
70.37%
19 / 27
0.00% covered (danger)
0.00%
0 / 1
9.66
1<?php
2
3declare(strict_types=1);
4
5namespace UppServices;
6
7/**
8 * Controls connection state for sync clients (create, refresh, update, delete, cancel, notify, unload).
9 * Delegates to App/Services/legacy/model/conncontrol.php, Legacy logout, and RegstService for unload.
10 */
11class ClientsService
12{
13    public function __construct(
14        private readonly RegstService $regstService,
15    ) {
16        require_once __DIR__.'/legacy/model/conncontrol.php';
17        require_once __DIR__.'/../Legacy/common/logout.php';
18    }
19
20    /**
21     * Refresh connection status for a session (polling). Updates TTL and returns status.
22     *
23     * @param \conx $conx Legacy db connection (must be connected when using DB backend).
24     * @param string $session Session id.
25     * @param string $refresh Output: 'UPDATED'|'REFRESH'|'EXPIRED'|'CANCELLED'.
26     * @return int ApplicationError code.
27     */
28    public function refresh(\conx $conx, string $session, string &$refresh): int
29    {
30        return \connection_refresh($conx, $session, $refresh);
31    }
32
33    /**
34     * Create or update connection entry for the session (initial sync registration).
35     *
36     * @param \conx $conx Legacy db connection.
37     * @param string $session Session id.
38     * @param array<string, mixed> $config Client config (incl. triggers).
39     * @return int ApplicationError code.
40     */
41    public function create(\conx $conx, string $session, array $config): int
42    {
43        return \connection_create($conx, $session, $config);
44    }
45
46    /**
47     * Delete connection entry (e.g. on logout).
48     *
49     * @param \conx $conx Legacy db connection.
50     * @param string $session Session id.
51     * @return int ApplicationError code.
52     */
53    public function delete(\conx $conx, string $session): int
54    {
55        return \connection_delete($conx, $session);
56    }
57
58    /**
59     * Update connection config (e.g. triggers).
60     *
61     * @param \conx $conx Legacy db connection.
62     * @param string $session Session id.
63     * @param array<string, mixed> $config Client config.
64     * @return int ApplicationError code.
65     */
66    public function update(\conx $conx, string $session, array $config): int
67    {
68        return \connection_update($conx, $session, $config);
69    }
70
71    /**
72     * Cancel connection (e.g. wake from another device).
73     *
74     * @param \conx $conx Legacy db connection.
75     * @param string $source Session id to cancel.
76     * @return int ApplicationError code.
77     */
78    public function cancel(\conx $conx, string $source): int
79    {
80        return \connection_cancel($conx, $source);
81    }
82
83    /**
84     * Notify connected sessions that match the given updates (trigger-based).
85     *
86     * @param \ConxHelper $conx Connection helper (uses ->global for central DB).
87     * @param string $origin Session id of the request origin.
88     * @param array<int, array{table: string, objid: mixed}> $updates List of table/objid changes.
89     * @param bool $toorigin Whether to force notify the origin as well.
90     * @return int ApplicationError code.
91     */
92    public function notify(\ConxHelper $conx, string $origin, array &$updates, bool $toorigin): int
93    {
94        return \connection_notify($conx, $origin, $updates, $toorigin);
95    }
96
97    /**
98     * Unload (logout) session: release, notify offline, register login/guest events.
99     *
100     * @param \ConxHelper $conx Connection helper (uses ->global for central DB).
101     * @param array{session: string, place?: mixed, table?: mixed} $body Payload with session id and optional place/table for registry.
102     * @return int ApplicationError code.
103     */
104    public function unload(\ConxHelper $conx, array $body): int
105    {
106        $global = $conx->global;
107        if ($global === null) {
108            return \ApplicationError::Database;
109        }
110        $when = $global->now();
111        $retval = \logout_session_and_notify($global, $when, $body['session']);
112        if (!\success($retval)) {
113            return $retval;
114        }
115        $results = [];
116        $res = $global->query('SESSION', ['id' => $body['session']], $results);
117        if (!$res || count($results) === 0) {
118            return \ApplicationError::NotFound;
119        }
120        $sessionObjid = $results[0]['objid'];
121        if (!empty($body['place'])) {
122            $event = [
123                'created' => $global->now(),
124                'place' => $body['place'],
125                'login' => '0',
126            ];
127            $retval = $this->regstService->registerLoginEvent($conx, (string) $sessionObjid, $event);
128        }
129        if (\success($retval) && !empty($body['table'])) {
130            $event = [
131                'created' => $global->now(),
132                'qrcode' => $body['table'],
133                'login' => '0',
134            ];
135            $retval = $this->regstService->registerGuestEvent($conx, (string) $sessionObjid, $event);
136        }
137        return $retval;
138    }
139}