Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.45% covered (success)
92.45%
49 / 53
n/a
0 / 0
CRAP
n/a
0 / 0
1<?php
2
3/**
4 * @file
5 * Place action: license extended period info (exndnfo).
6 */
7
8declare(strict_types=1);
9
10require_once __DIR__.'/../../Legacy/common/session.php';
11
12/**
13 * Action callable: get license extended period info for place.
14 *
15 * @param string $body Unused.
16 * @param array<string, mixed> $query Query params: device, session, place.
17 * @param \ConxHelper $conx Connection helper.
18 * @param \Psr\Log\LoggerInterface $logger Logger.
19 * @param \UppServices\SessionService $sessionService Session service for check.
20 * @return array{output: string, contentType: string}
21 */
22return function (string $body, array $query, \ConxHelper $conx, \Psr\Log\LoggerInterface $logger, \UppServices\SessionService $sessionService): array {
23    $retval = \ApplicationError::Success;
24    $placeinfo = null;
25
26    $device = $query['device'] ?? null;
27    if (!$device) {
28        $logger->error("No device provided on URL. Operation cancelled.");
29        $retval = \ApplicationError::Parameters;
30    }
31
32    if (\success($retval)) {
33        $session = $query['session'] ?? null;
34        if (!$session) {
35            $logger->error("No session provided on URL. Operation cancelled.");
36            $retval = \ApplicationError::Parameters;
37        } else {
38            $userid = null;
39            $retval = $sessionService->checkSession($conx, (string) $session, (string) $device, $userid);
40        }
41    }
42
43    if (\success($retval)) {
44        $place = $query['place'] ?? null;
45        if (!$place) {
46            $logger->error("Mandatory parameter 'place' not provided on URL. Operation cancelled.");
47            $retval = \ApplicationError::Parameters;
48        }
49    }
50
51    if (\success($retval)) {
52        $results = [];
53        $res = $conx->global->query('PLACE', ['objid' => $place, 'status' => ['PA', 'AC']], $results);
54        if ($res) {
55            if (count($results) > 0) {
56                $placeinfo = $results[0];
57            } else {
58                $logger->error("No active place found with objid '".$place."'.");
59                $retval = \ApplicationError::NotFound;
60            }
61        } else {
62            $logger->error("Query failed for PLACE objid ".$place);
63            $retval = \ApplicationError::Database;
64        }
65    }
66
67    $license = null;
68    if (\success($retval)) {
69        $extend_period = [
70            '1M' => ['period' => \AppConstants::Extend_Period_1M, 'price' => \AppConstants::Extend_Price_1M],
71            '3M' => ['period' => \AppConstants::Extend_Period_3M, 'price' => \AppConstants::Extend_Price_3M],
72            '1Y' => ['period' => \AppConstants::Extend_Period_1Y, 'price' => \AppConstants::Extend_Price_1Y],
73        ];
74        $license = [];
75        $expiresBase = $placeinfo['expires'] ?? $conx->global->now();
76        foreach ($extend_period as $period => $extend) {
77            $dt1 = date("Y-m-d H:i:s", strtotime($extend['period'], strtotime($conx->global->now())));
78            $dt2 = date("Y-m-d H:i:s", strtotime($extend['period'], strtotime($expiresBase)));
79            $license[$period] = [
80                "expires" => ($dt1 > $dt2) ? $dt1 : $dt2,
81                'price' => $extend['price'],
82                'currency' => \AppConstants::Currency,
83            ];
84        }
85    }
86
87    $result = [
88        "errorcode" => $retval,
89        "license" => \success($retval) ? $license : null,
90    ];
91    return ['output' => json_encode($result), 'contentType' => 'application/json; charset=utf-8'];
92};