Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
RestTest
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 3
240
0.00% covered (danger)
0.00%
0 / 1
 compileDeferred
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
72
 getParameter
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
30
 getDecodedParameter
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
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) 2017-2020 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoDeliveryRdf\controller;
24
25use common_exception_BadRequest as BadRequestException;
26use common_exception_Error as Error;
27use common_exception_MissingParameter as MissingParameterException;
28use common_exception_NotFound as NotFoundException;
29use common_exception_NotImplemented as NotImplementedException;
30use common_report_Report as Report;
31use oat\tao\model\import\ImporterNotFound;
32use oat\tao\model\taskQueue\TaskLogInterface;
33use oat\taoDeliveryRdf\model\tasks\ImportAndCompile;
34use Request;
35use tao_actions_RestController as RestController;
36use tao_helpers_Http as HttpHelper;
37
38/**
39 * Class RestTest
40 *
41 * @package oat\taoDeliveryRdf\controller
42 *
43 * @author Aleh Hutnikau, <hutnikau@1pt.com>
44 */
45class RestTest extends RestController
46{
47    public const REST_IMPORTER_ID = 'importerId';
48    public const REST_FILE_NAME = 'testPackage';
49    public const REST_DELIVERY_PARAMS = 'delivery-params';
50    public const REST_DELIVERY_CLASS_LABELS = 'delivery-class-labels';
51
52    /**
53     * @deprecated
54     * @see \oat\taoDeliveryRdf\controller\RestTest::REST_DELIVERY_CLASS_LABELS
55     */
56    public const REST_DELIVERY_CLASS_LABEL = 'delivery-class-label';
57
58    /**
59     * Import test and compile it into delivery
60     *
61     * @throws Error
62     * @throws MissingParameterException
63     * @throws NotImplementedException
64     */
65    public function compileDeferred(): void
66    {
67        if ($this->getRequestMethod() !== Request::HTTP_POST) {
68            throw new NotImplementedException('Only post method is accepted to compile test');
69        }
70
71        if (!$this->hasRequestParameter(self::REST_IMPORTER_ID)) {
72            throw new MissingParameterException(self::REST_IMPORTER_ID, $this->getRequestURI());
73        }
74
75        if (HttpHelper::hasUploadedFile(self::REST_FILE_NAME)) {
76            try {
77                $importerId = $this->getParameter(self::REST_IMPORTER_ID);
78                $file = HttpHelper::getUploadedFile(self::REST_FILE_NAME);
79                $customParams = $this->getDecodedParameter(self::REST_DELIVERY_PARAMS);
80                $deliveryClassLabels = $this->getParameter(self::REST_DELIVERY_CLASS_LABELS) === null
81                    ? [$this->getParameter(self::REST_DELIVERY_CLASS_LABEL)]
82                    : $this->getDecodedParameter(self::REST_DELIVERY_CLASS_LABELS);
83
84                $deliveryClassLabels = array_filter(
85                    $deliveryClassLabels,
86                    static function ($deliveryClassLabel): bool {
87                        return null !== $deliveryClassLabel;
88                    }
89                );
90
91                $task = ImportAndCompile::createTask($importerId, $file, $customParams, $deliveryClassLabels);
92            } catch (ImporterNotFound $e) {
93                $this->returnFailure(new NotFoundException($e->getMessage()));
94            }
95
96            $result = ['reference_id' => $task->getId()];
97
98            /** @var TaskLogInterface $taskLog */
99            $taskLog = $this->getServiceLocator()->get(TaskLogInterface::SERVICE_ID);
100            $report = $taskLog->getReport($task->getId());
101
102            if (!empty($report)) {
103                if ($report instanceof Report) {
104                    // Serialize report to array
105                    $report = json_decode(json_encode($report), true);
106                }
107
108                $result['common_report_Report'] = $report;
109            }
110
111            $this->returnSuccess($result);
112        } else {
113            $this->returnFailure(new BadRequestException('Test package file was not given'));
114        }
115    }
116
117    /**
118     * @param string $name
119     * @param mixed|null $default
120     *
121     * @return mixed|null
122     */
123    private function getParameter(string $name, $default = null)
124    {
125        $bodyParameters = $this->getPsrRequest()->getParsedBody();
126
127        if (is_array($bodyParameters) && isset($bodyParameters[$name])) {
128            return $bodyParameters[$name];
129        }
130
131        if (is_object($bodyParameters) && property_exists($bodyParameters, $name)) {
132            return $bodyParameters->{$name};
133        }
134
135        return $default;
136    }
137
138    /**
139     * @param string $name
140     *
141     * @return array
142     */
143    private function getDecodedParameter(string $name): array
144    {
145        $data = $this->getParameter($name, []);
146
147        return is_array($data)
148            ? $data
149            : json_decode(html_entity_decode($data), true);
150    }
151}