Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.02% covered (success)
83.02%
44 / 53
n/a
0 / 0
CRAP
n/a
0 / 0
1<?php
2
3/**
4 * @file
5 * Reports action: load partial graph updates by table+objid items.
6 */
7
8declare(strict_types=1);
9
10require_once __DIR__.'/../legacy/model/loadcomplete.php';
11
12/**
13 * Action callable: load partial graph updates by table+objid items.
14 *
15 * Body format:
16 * {
17 *   "items": [
18 *     { "table": "TICKET", "objid": 123 }
19 *   ]
20 * }
21 *
22 * @param string $body JSON body.
23 * @param array<string, mixed> $query Query params: device, session, place.
24 * @param \ConxHelper $conx Connection helper.
25 * @param \Psr\Log\LoggerInterface $logger Logger.
26 * @param \UppServices\SessionService $sessionService Session service for check.
27 * @return array{output: string, contentType: string}
28 */
29return function (
30    string $body,
31    array $query,
32    \ConxHelper $conx,
33    \Psr\Log\LoggerInterface $logger,
34    \UppServices\SessionService $sessionService
35): array {
36    $retval = \ApplicationError::Success;
37    $completed = time();
38    $updates = [];
39    $items = [];
40
41    if ($body === '') {
42        $logger->error("No post data provided.");
43        $retval = \ApplicationError::Parameters;
44    }
45
46    if (\success($retval)) {
47        $payload = json_decode($body, true);
48        if (!is_array($payload) || !isset($payload['items']) || !is_array($payload['items']) || (count($payload['items']) === 0)) {
49            $logger->error("Invalid JSON payload. Expected {items:[{table,objid}]}.");
50            $retval = \ApplicationError::Parameters;
51        } else {
52            $items = $payload['items'];
53        }
54    }
55
56    if (\success($retval)) {
57        $device = $query['device'] ?? null;
58        if (!$device) {
59            $logger->error("No device provided on URL. Operation cancelled.");
60            $retval = \ApplicationError::Parameters;
61        }
62    }
63
64    if (\success($retval)) {
65        $session = $query['session'] ?? null;
66        if (!$session) {
67            $logger->error("No session provided on URL. Operation cancelled.");
68            $retval = \ApplicationError::Parameters;
69        } else {
70            $userid = null;
71            $sessionObjid = null;
72            $retval = $sessionService->checkSession($conx, (string) $session, (string) $device, $userid, $sessionObjid);
73        }
74    }
75
76    if (\success($retval)) {
77        $place = $query['place'] ?? null;
78        if (($place === null) || ($place === '')) {
79            $logger->error("Mandatory argument 'place' not provided in url parameters.");
80            $retval = \ApplicationError::Parameters;
81        } else {
82            $conx->tenant = $place;
83        }
84    }
85
86    if (\success($retval) && ($conx->tenant === null)) {
87        $logger->error("Connection to place database failed.");
88        $retval = \ApplicationError::Database;
89    }
90
91    if (\success($retval)) {
92        $timestamp = isset($query['timestamp']) ? (int) $query['timestamp'] : 0;
93        $retval = load_complete($conx, $items, $timestamp, $updates);
94    }
95
96    $result = [
97        "errorcode" => $retval,
98        "updates" => \success($retval) ? $updates : null,
99        "completed" => \success($retval) ? $completed : null,
100    ];
101    return ['output' => json_encode($result), 'contentType' => 'application/json; charset=utf-8'];
102};