Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
66.67% |
36 / 54 |
|
50.00% |
7 / 14 |
CRAP | |
0.00% |
0 / 1 |
| TestRunnerAction | |
66.67% |
36 / 54 |
|
50.00% |
7 / 14 |
51.04 | |
0.00% |
0 / 1 |
| process | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| setOffline | |
77.78% |
7 / 9 |
|
0.00% |
0 / 1 |
3.10 | |||
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| hasRequestParameter | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRequestParameters | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getRequestParameter | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| getRawRequestParameter | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTime | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setTime | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTimestamp | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getRequiredFields | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| validate | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| getErrorResponse | |
57.69% |
15 / 26 |
|
0.00% |
0 / 1 |
15.13 | |||
| getPsrContainer | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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 (original work) Open Assessment Technologies SA ; |
| 19 | */ |
| 20 | |
| 21 | namespace oat\taoQtiTest\models\runner\synchronisation; |
| 22 | |
| 23 | use Laminas\ServiceManager\ServiceLocatorAwareInterface; |
| 24 | use Laminas\ServiceManager\ServiceLocatorAwareTrait; |
| 25 | use oat\oatbox\event\EventManager; |
| 26 | use oat\taoQtiTest\models\event\ItemOfflineEvent; |
| 27 | use oat\taoQtiTest\models\runner\QtiRunnerClosedException; |
| 28 | use oat\taoQtiTest\models\runner\QtiRunnerMessageService; |
| 29 | use oat\taoQtiTest\models\runner\QtiRunnerPausedException; |
| 30 | use oat\taoQtiTest\models\runner\RunnerParamParserTrait; |
| 31 | use oat\taoQtiTest\models\runner\RunnerToolStates; |
| 32 | use Psr\Container\ContainerInterface; |
| 33 | |
| 34 | /** |
| 35 | * Class TestRunnerAction |
| 36 | * |
| 37 | * @package oat\taoQtiTest\models\runner\synchronisation |
| 38 | */ |
| 39 | abstract class TestRunnerAction implements ServiceLocatorAwareInterface |
| 40 | { |
| 41 | use ServiceLocatorAwareTrait; |
| 42 | use RunnerParamParserTrait; |
| 43 | use RunnerToolStates; |
| 44 | |
| 45 | public const OFFLINE_VARIABLE = 'OFFLINE_ITEM'; |
| 46 | |
| 47 | /** @var double The timestamp of current action */ |
| 48 | protected $time; |
| 49 | |
| 50 | /** @var integer The timestamp of action */ |
| 51 | protected $timestamp; |
| 52 | |
| 53 | /** @var string The name of action */ |
| 54 | protected $name; |
| 55 | |
| 56 | /** @var array Parameters of the current action */ |
| 57 | protected $parameters; |
| 58 | |
| 59 | abstract public function process(): array; |
| 60 | |
| 61 | /** |
| 62 | * Method to set a trace variable telling that the item was offline |
| 63 | * |
| 64 | * @return bool |
| 65 | */ |
| 66 | protected function setOffline() |
| 67 | { |
| 68 | $serviceContext = $this->getServiceContext(); |
| 69 | $itemRef = ($this->hasRequestParameter('itemDefinition')) |
| 70 | ? $this->getRequestParameter('itemDefinition') |
| 71 | : null; |
| 72 | |
| 73 | if (!is_null($itemRef)) { |
| 74 | $event = new ItemOfflineEvent($serviceContext->getTestSession(), $itemRef); |
| 75 | $this->getServiceLocator()->get(EventManager::SERVICE_ID)->trigger($event); |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * TestRunnerAction constructor. |
| 84 | * |
| 85 | * Construct the action with required $name and $timestamp |
| 86 | * Parameters is optional |
| 87 | * |
| 88 | * @param $name |
| 89 | * @param $timestamp |
| 90 | * @param array $parameters |
| 91 | */ |
| 92 | public function __construct($name, $timestamp, array $parameters = []) |
| 93 | { |
| 94 | $this->name = $name; |
| 95 | $this->timestamp = $timestamp; |
| 96 | $this->parameters = $parameters; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Check if $name exists in parameters array |
| 101 | * |
| 102 | * @param $name |
| 103 | * @return bool |
| 104 | */ |
| 105 | public function hasRequestParameter($name) |
| 106 | { |
| 107 | return isset($this->parameters[$name]); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get action input parameters |
| 112 | * |
| 113 | * @return mixed |
| 114 | */ |
| 115 | public function getRequestParameters() |
| 116 | { |
| 117 | return $this->parameters; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Check get the $name from parameters array, false if does not exist |
| 122 | * |
| 123 | * @param $name |
| 124 | * @return bool|mixed |
| 125 | */ |
| 126 | public function getRequestParameter($name) |
| 127 | { |
| 128 | return $this->hasRequestParameter($name) ? $this->parameters[$name] : false; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * For RunnerToolStates |
| 133 | * |
| 134 | * @param $name |
| 135 | * @return bool|mixed |
| 136 | */ |
| 137 | public function getRawRequestParameter($name) |
| 138 | { |
| 139 | return $this->getRequestParameter($name); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Get the timestamp of current action in seconds |
| 144 | * |
| 145 | * @return double $time |
| 146 | */ |
| 147 | public function getTime() |
| 148 | { |
| 149 | return $this->time; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Set the timestamp of current action in seconds |
| 154 | * |
| 155 | * @param double $time |
| 156 | */ |
| 157 | public function setTime($time) |
| 158 | { |
| 159 | $this->time = $time; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Get the name of current action |
| 164 | * |
| 165 | * @return mixed |
| 166 | */ |
| 167 | public function getName() |
| 168 | { |
| 169 | return $this->name; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Get the timestamp of current action |
| 174 | * |
| 175 | * @return integer |
| 176 | */ |
| 177 | public function getTimestamp() |
| 178 | { |
| 179 | return $this->timestamp; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Provide the required fields for current action |
| 184 | * |
| 185 | * @return array |
| 186 | */ |
| 187 | protected function getRequiredFields() |
| 188 | { |
| 189 | return ['testDefinition', 'testCompilation', 'serviceCallId']; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Validate the class parameters against the getRequiredFields method |
| 194 | * |
| 195 | * @throws \common_exception_InconsistentData |
| 196 | */ |
| 197 | public function validate() |
| 198 | { |
| 199 | $requiredFields = array_unique($this->getRequiredFields()); |
| 200 | $isValid = ($requiredFields == array_unique(array_intersect($requiredFields, array_keys($this->parameters)))); |
| 201 | if (!$isValid) { |
| 202 | throw new \common_exception_InconsistentData( |
| 203 | 'Some parameters are missing. Required parameters are : ' . implode(', ', $requiredFields) |
| 204 | ); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Gets an error response object |
| 210 | * |
| 211 | * @param Exception [$e] Optional exception from which extract the error context |
| 212 | * @return array |
| 213 | */ |
| 214 | protected function getErrorResponse($e = null) |
| 215 | { |
| 216 | $response = [ |
| 217 | 'success' => false, |
| 218 | 'type' => 'error', |
| 219 | ]; |
| 220 | |
| 221 | if ($e) { |
| 222 | if ($e instanceof \Exception) { |
| 223 | $response['type'] = 'exception'; |
| 224 | $response['code'] = $e->getCode(); |
| 225 | } |
| 226 | |
| 227 | if ($e instanceof \common_exception_UserReadableException) { |
| 228 | $response['message'] = $e->getUserMessage(); |
| 229 | } else { |
| 230 | $response['message'] = __('An error occurred!'); |
| 231 | } |
| 232 | |
| 233 | switch (true) { |
| 234 | case $e instanceof QtiRunnerClosedException: |
| 235 | case $e instanceof QtiRunnerPausedException: |
| 236 | if ($this->serviceContext) { |
| 237 | $messageService = $this->getServiceLocator()->get(QtiRunnerMessageService::SERVICE_ID); |
| 238 | // phpcs:disable Generic.Files.LineLength |
| 239 | $response['message'] = __($messageService->getStateMessage($this->getServiceContext()->getTestSession())); |
| 240 | // phpcs:enable Generic.Files.LineLength |
| 241 | } |
| 242 | $response['type'] = 'TestState'; |
| 243 | break; |
| 244 | |
| 245 | case $e instanceof \tao_models_classes_FileNotFoundException: |
| 246 | $response['type'] = 'FileNotFound'; |
| 247 | $response['message'] = __('File not found'); |
| 248 | break; |
| 249 | |
| 250 | case $e instanceof \common_exception_Unauthorized: |
| 251 | $response['code'] = 403; |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | return $response; |
| 257 | } |
| 258 | |
| 259 | protected function getPsrContainer(): ContainerInterface |
| 260 | { |
| 261 | return $this->getServiceLocator()->getContainer(); |
| 262 | } |
| 263 | } |