Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
SyncController
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
3 / 3
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 sync
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
6
 objidRequest
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5use Psr\Http\Message\ResponseInterface as Response;
6use Psr\Http\Message\ServerRequestInterface as Request;
7use UppServices\SyncService;
8
9/**
10 * Slim controller for sync endpoint: apply changes and return updates (long-poll).
11 */
12class SyncController
13{
14    private readonly SyncService $syncService;
15
16    public function __construct(SyncService $syncService)
17    {
18        $this->syncService = $syncService;
19    }
20
21    /**
22     * GET or POST /sync - sync flow (query params + optional POST body with changes).
23     */
24    public function sync(Request $request, Response $response): Response
25    {
26        $result = $this->syncService->synchronize($request);
27
28        $json = json_encode($result);
29        $acceptEncoding = $request->getHeaderLine('Accept-Encoding');
30        $useGzip = $json !== false && str_contains($acceptEncoding, 'gzip');
31
32        if ($useGzip && $json !== false) {
33            $body = gzencode($json, 9, FORCE_GZIP);
34            $response->getBody()->write($body !== false ? $body : $json);
35            $response = $response
36                ->withHeader('Content-Type', 'application/json; charset=utf-8')
37                ->withHeader('Content-Encoding', 'gzip')
38                ->withHeader('Keep-Alive', 'timeout=0, max=0')
39                ->withHeader('Connection', 'close');
40        } else {
41            $response->getBody()->write($json !== false ? $json : '{}');
42            $response = $response
43                ->withHeader('Content-Type', 'application/json; charset=utf-8')
44                ->withHeader('Content-Encoding', 'none')
45                ->withHeader('Keep-Alive', 'timeout=0, max=0')
46                ->withHeader('Connection', 'close');
47        }
48
49        return $response;
50    }
51
52    /**
53     * GET /sync/objidrequest - fetch a single row by objid from a tenant table (on-demand refresh).
54     */
55    public function objidRequest(Request $request, Response $response): Response
56    {
57        $result = $this->syncService->objidRequest($request);
58        $json = json_encode($result);
59        $response->getBody()->write($json !== false ? $json : '{}');
60        return $response->withHeader('Content-Type', 'application/json; charset=utf-8');
61    }
62}