Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.98% covered (success)
92.98%
53 / 57
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConnectionService
92.86% covered (success)
92.86%
52 / 56
71.43% covered (warning)
71.43%
5 / 7
20.15
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
 withValidSession
90.00% covered (success)
90.00%
18 / 20
0.00% covered (danger)
0.00%
0 / 1
9.08
 create
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 update
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 delete
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 cancel
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 unload
86.67% covered (success)
86.67%
13 / 15
0.00% covered (danger)
0.00%
0 / 1
5.06
1<?php
2
3declare(strict_types=1);
4
5namespace UppServices;
6
7require_once __DIR__ . '/legacy/ddbb/conx.php';
8
9/**
10 * HTTP-facing connection actions: create, update, delete, cancel, unload.
11 * Uses SessionService for validation and ClientsService for all connection state (including unload).
12 */
13class ConnectionService
14{
15    private readonly ClientsService $clientsService;
16    private readonly SessionService $sessionService;
17
18    public function __construct(
19        ClientsService $clientsService,
20        SessionService $sessionService,
21    ) {
22        $this->sessionService = $sessionService;
23        $this->clientsService = $clientsService;
24    }
25
26    /**
27     * Validate device/session from query; return error code or call $fn with central connection and session.
28     * Connection is released before returning.
29     *
30     * @param array<string, mixed> $query Query params (device, session).
31     * @param callable(\conx, string): array $fn Called with central connection and session id on valid params.
32     * @return array{errorcode: int, connection?: string}
33     */
34    private function withValidSession(array $query, callable $fn): array
35    {
36        $retval = \ApplicationError::Success;
37        $result = null;
38        $conx = new \ConxHelper(__FILE__);
39        $device = isset($query['device']) ? (string) $query['device'] : '';
40        $session = isset($query['session']) ? (string) $query['session'] : '';
41
42        // Validate device and session from query.
43        if (\success($retval)) {
44            if ($device === '' || $session === '') {
45                \addlog(__FILE__, \LogLevel::Error, "ConnectionService: missing device or session in query.");
46                $retval = \ApplicationError::Parameters;
47            }
48        }
49
50        // Check session (central connection is lazy via ConxHelper).
51        if (\success($retval)) {
52            $cg = $conx->global;
53            if ($cg === null) {
54                \addlog(__FILE__, \LogLevel::Error, "ConnectionService: failed to connect to central database.");
55                $retval = \ApplicationError::Database;
56            } 
57            else {
58                $userid = null;
59                $retval = $this->sessionService->checkSession($conx, $session, $device, $userid);
60            }
61        }
62
63        // Run callback and capture result.
64        if (\success($retval)) {
65            $result = $fn($conx->global, $session);
66        }
67
68        $conx->release();
69
70        return $result ?? ['errorcode' => $retval];
71    }
72
73    /**
74     * Create connection for session (initial sync registration).
75     *
76     * @param array<string, mixed> $query Query params (device, session).
77     * @param array<string, mixed> $config Client config (e.g. triggers).
78     * @return array{errorcode: int, connection?: string} JSON response payload.
79     */
80    public function create(array $query, array $config): array
81    {
82        return $this->withValidSession($query, function (\conx $conx, string $session) use ($config): array {
83            $retval = $this->clientsService->create($conx, $session, $config);
84            $out = ['errorcode' => $retval];
85            if (\success($retval)) {
86                $out['connection'] = $session;
87            }
88            return $out;
89        });
90    }
91
92    /**
93     * Update connection config (e.g. triggers).
94     *
95     * @param array<string, mixed> $query Query params (device, session).
96     * @param array<string, mixed> $config Client config.
97     * @return array{errorcode: int} JSON response payload.
98     */
99    public function update(array $query, array $config): array
100    {
101        return $this->withValidSession($query, function (\conx $conx, string $session) use ($config): array {
102            $retval = $this->clientsService->update($conx, $session, $config);
103            return ['errorcode' => $retval];
104        });
105    }
106
107    /**
108     * Delete connection entry (e.g. on disconnect).
109     *
110     * @param array<string, mixed> $query Query params (device, session).
111     * @return array{errorcode: int} JSON response payload.
112     */
113    public function delete(array $query): array
114    {
115        return $this->withValidSession($query, function (\conx $conx, string $session): array {
116            $retval = $this->clientsService->delete($conx, $session);
117            return ['errorcode' => $retval];
118        });
119    }
120
121    /**
122     * Cancel connection (e.g. wake from another device).
123     *
124     * @param array<string, mixed> $query Query params (device, session).
125     * @return array{errorcode: int} JSON response payload.
126     */
127    public function cancel(array $query): array
128    {
129        return $this->withValidSession($query, function (\conx $conx, string $session): array {
130            $retval = $this->clientsService->cancel($conx, $session);
131            return ['errorcode' => $retval];
132        });
133    }
134
135    /**
136     * Unload (logout) session: release, notify offline, register login/guest events.
137     *
138     * @param array{session: string, place?: mixed, table?: mixed} $body JSON body with session and optional place/table.
139     * @return array{errorcode: int} JSON response payload.
140     */
141    public function unload(array $body): array
142    {
143        $retval = \ApplicationError::Success;
144        $result = null;
145        $conx = new \ConxHelper(__FILE__);
146
147        // Validate session in body.
148        if (\success($retval)) {
149            if (empty($body['session'])) {
150                \addlog(__FILE__, \LogLevel::Error, "ConnectionService::unload: missing session in body.");
151                $retval = \ApplicationError::Parameters;
152            }
153        }
154
155        // Run unload and build result.
156        if (\success($retval)) {
157            if ($conx->global === null) {
158                \addlog(__FILE__, \LogLevel::Error, "ConnectionService::unload: failed to connect to central database.");
159                $retval = \ApplicationError::Database;
160            } else {
161                $retval = $this->clientsService->unload($conx, $body);
162                $result = ['errorcode' => $retval];
163            }
164        }
165
166        $conx->release();
167
168        return $result ?? ['errorcode' => $retval];
169    }
170}