Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ImportResultInputFactory
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
2 / 2
11
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
 createFromRequest
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
1 / 1
10
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2023 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoResultServer\models\Import\Factory;
24
25use common_exception_MissingParameter;
26use common_exception_NotFound;
27use common_exception_ResourceNotFound;
28use oat\taoDelivery\model\execution\DeliveryExecutionService;
29use oat\taoResultServer\models\Import\Input\ImportResultInput;
30use Psr\Http\Message\ServerRequestInterface;
31
32class ImportResultInputFactory
33{
34    private const Q_PARAM_DELIVERY_EXECUTION_ID = 'execution';
35    private const Q_PARAM_TRIGGER_AGS_SEND = 'send_ags';
36
37    private DeliveryExecutionService $deliveryExecutionService;
38
39    public function __construct(DeliveryExecutionService $deliveryExecutionService)
40    {
41        $this->deliveryExecutionService = $deliveryExecutionService;
42    }
43
44    /**
45     * @param ServerRequestInterface $request
46     * @return ImportResultInput
47     * @throws common_exception_MissingParameter
48     * @throws common_exception_NotFound
49     * @throws common_exception_ResourceNotFound
50     */
51    public function createFromRequest(ServerRequestInterface $request): ImportResultInput
52    {
53        $params = $request->getQueryParams();
54        $body = json_decode((string)$request->getBody(), true);
55
56        if (!isset($params[self::Q_PARAM_DELIVERY_EXECUTION_ID])) {
57            throw new common_exception_MissingParameter(self::Q_PARAM_DELIVERY_EXECUTION_ID);
58        };
59
60        $deliveryExecution = $this->deliveryExecutionService
61            ->getDeliveryExecution($params[self::Q_PARAM_DELIVERY_EXECUTION_ID]);
62
63        if (!$deliveryExecution->getFinishTime()) {
64            throw new common_exception_ResourceNotFound(
65                sprintf(
66                    'Finished delivery execution %s not found',
67                    $params[self::Q_PARAM_DELIVERY_EXECUTION_ID]
68                )
69            );
70        }
71
72        $new = new ImportResultInput(
73            $params[self::Q_PARAM_DELIVERY_EXECUTION_ID],
74            isset($params[self::Q_PARAM_TRIGGER_AGS_SEND]) &&
75            filter_var($params[self::Q_PARAM_TRIGGER_AGS_SEND], FILTER_VALIDATE_BOOLEAN)
76        );
77
78        foreach ($body['itemVariables'] ?? [] as $item) {
79            if (!isset($item['itemId'], $item['outcomes'])) {
80                throw new common_exception_MissingParameter('itemId|outcomes');
81            }
82
83            foreach ($item['outcomes'] ?? [] as $outcome) {
84                if (!isset($outcome['id'], $outcome['value'])) {
85                    throw new common_exception_MissingParameter('id|value');
86                }
87
88                $new->addOutcome((string)$item['itemId'], (string)$outcome['id'], (float)$outcome['value']);
89            }
90
91            foreach ($item['responses'] ?? [] as $response) {
92                if (!isset($response['id'], $response['correctResponse'])) {
93                    throw new common_exception_MissingParameter('id|correctResponse');
94                }
95
96                $new->addResponse(
97                    (string)$item['itemId'],
98                    (string)$response['id'],
99                    [
100                        'correctResponse' => boolval($response['correctResponse']),
101                    ]
102                );
103            }
104        }
105
106        return $new;
107    }
108}