Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
66.04% |
35 / 53 |
|
44.44% |
4 / 9 |
CRAP | |
0.00% |
0 / 1 |
| SendAgsScoreTask | |
66.04% |
35 / 53 |
|
44.44% |
4 / 9 |
38.28 | |
0.00% |
0 / 1 |
| __invoke | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
4 | |||
| validateParams | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
7 | |||
| retryTask | |
17.65% |
3 / 17 |
|
0.00% |
0 / 1 |
8.03 | |||
| isRetryEnabled | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| increaseRetryCount | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isMaxRetryCountReached | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| reportError | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getQueueDispatcher | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFeatureFlagChecker | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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) 2021-2022 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | declare(strict_types=1); |
| 23 | |
| 24 | namespace oat\ltiDeliveryProvider\model\tasks; |
| 25 | |
| 26 | use InvalidArgumentException; |
| 27 | use OAT\Library\Lti1p3Core\Message\Payload\Claim\AgsClaim; |
| 28 | use oat\oatbox\extension\AbstractAction; |
| 29 | use oat\oatbox\reporting\Report; |
| 30 | use oat\tao\model\featureFlag\FeatureFlagChecker; |
| 31 | use oat\tao\model\featureFlag\FeatureFlagCheckerInterface; |
| 32 | use oat\tao\model\taskQueue\QueueDispatcherInterface; |
| 33 | use oat\taoLti\models\classes\LtiAgs\LtiAgsException; |
| 34 | use oat\taoLti\models\classes\LtiAgs\LtiAgsScoreService; |
| 35 | use oat\taoLti\models\classes\LtiAgs\LtiAgsScoreServiceInterface; |
| 36 | use oat\taoLti\models\classes\Platform\Repository\Lti1p3RegistrationRepository; |
| 37 | |
| 38 | class SendAgsScoreTask extends AbstractAction |
| 39 | { |
| 40 | public const FEATURE_FLAG_AGS_SCORE_SENDING_RETRY = 'FEATURE_FLAG_AGS_SCORE_SENDING_RETRY'; |
| 41 | |
| 42 | public const RETRY_COUNT = 'retryCount'; |
| 43 | public const RETRY_MAX = 'retryMax'; |
| 44 | |
| 45 | private array $params = [self::RETRY_COUNT => 0]; |
| 46 | |
| 47 | public function __invoke($params): Report |
| 48 | { |
| 49 | $this->params = array_merge($this->params, $params); |
| 50 | $this->getLogger()->info('Start AGS score sending task:' . print_r($params, true)); |
| 51 | |
| 52 | try { |
| 53 | $this->validateParams($params); |
| 54 | } catch (InvalidArgumentException $e) { |
| 55 | return $this->reportError($e->getMessage()); |
| 56 | } |
| 57 | |
| 58 | $registrationId = $params['registrationId']; |
| 59 | $deliveryExecutionId = $params['deliveryExecutionId'] ?? 'not passed to sender task'; |
| 60 | $agsClaim = AgsClaim::denormalize($params['agsClaim']); |
| 61 | $data = $params['data']; |
| 62 | |
| 63 | /** @var Lti1p3RegistrationRepository $repository */ |
| 64 | $repository = $this->getServiceLocator()->get(Lti1p3RegistrationRepository::SERVICE_ID); |
| 65 | $registration = $repository->find($registrationId); |
| 66 | |
| 67 | if (null === $registration) { |
| 68 | return $this->reportError(sprintf('Registration with identifier "%s" not found', $registrationId)); |
| 69 | } |
| 70 | |
| 71 | /** @var LtiAgsScoreService $agsScoreService */ |
| 72 | $agsScoreService = $this->getServiceLocator()->getContainer()->get(LtiAgsScoreServiceInterface::class); |
| 73 | |
| 74 | try { |
| 75 | $agsScoreService->send($registration, $agsClaim, $data); |
| 76 | } catch (LtiAgsException $e) { |
| 77 | $this->retryTask($e, $deliveryExecutionId); |
| 78 | |
| 79 | return $this->reportError($e->getMessage()); |
| 80 | } |
| 81 | |
| 82 | $this->logInfo('Finish AGS score sending task successfully'); |
| 83 | |
| 84 | return Report::createSuccess('AGS score has been sent successfully'); |
| 85 | } |
| 86 | |
| 87 | private function validateParams(array $params): void |
| 88 | { |
| 89 | if (!is_string($params['registrationId'] ?? null)) { |
| 90 | throw new InvalidArgumentException('Parameter "registrationId" must be a string'); |
| 91 | } |
| 92 | |
| 93 | if (isset($params['deliveryExecutionId']) && !is_string($params['deliveryExecutionId'])) { |
| 94 | throw new InvalidArgumentException('Parameter "deliveryExecutionId" must be a string'); |
| 95 | } |
| 96 | |
| 97 | if (!is_array($params['agsClaim'] ?? null) || !is_array($params['agsClaim']['scope'] ?? null)) { |
| 98 | throw new InvalidArgumentException('Parameter "agsClaim" must be an array and include "scope" as an array'); |
| 99 | } |
| 100 | |
| 101 | if (!is_array($params['data'] ?? null)) { |
| 102 | throw new InvalidArgumentException('Parameter "data" must be an array'); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | private function retryTask(LtiAgsException $exception, string $deliveryExecutionId): void |
| 107 | { |
| 108 | if (!$this->isRetryEnabled()) { |
| 109 | $this->logNotice('Retry is disabled'); |
| 110 | |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | if ($this->isMaxRetryCountReached()) { |
| 115 | $this->logCritical( |
| 116 | 'Failed to send AGS Score message: the max number of retries has been reached', |
| 117 | [ |
| 118 | 'agsClaim' => $exception->getAgsClaim()->normalize(), |
| 119 | 'score' => json_encode($exception->getScore()), |
| 120 | 'registration' => $exception->getRegistration()->getIdentifier(), |
| 121 | 'deliveryExecution' => $deliveryExecutionId |
| 122 | ] |
| 123 | ); |
| 124 | |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | $this->increaseRetryCount(); |
| 129 | $this->getQueueDispatcher()->createTask(new self(), $this->params); |
| 130 | $this->logInfo('AGS Score message has been rescheduled for another try'); |
| 131 | } |
| 132 | |
| 133 | private function isRetryEnabled(): bool |
| 134 | { |
| 135 | return !empty($this->params[self::RETRY_MAX]) |
| 136 | && $this->getFeatureFlagChecker()->isEnabled(self::FEATURE_FLAG_AGS_SCORE_SENDING_RETRY); |
| 137 | } |
| 138 | |
| 139 | private function increaseRetryCount(): void |
| 140 | { |
| 141 | $this->params[self::RETRY_COUNT]++; |
| 142 | } |
| 143 | |
| 144 | private function isMaxRetryCountReached(): bool |
| 145 | { |
| 146 | return $this->params[self::RETRY_COUNT] >= $this->params[self::RETRY_MAX]; |
| 147 | } |
| 148 | |
| 149 | private function reportError(string $message): Report |
| 150 | { |
| 151 | $this->logError($message); |
| 152 | |
| 153 | return Report::createError($message); |
| 154 | } |
| 155 | |
| 156 | private function getQueueDispatcher(): QueueDispatcherInterface |
| 157 | { |
| 158 | return $this->getServiceLocator()->get(QueueDispatcherInterface::SERVICE_ID); |
| 159 | } |
| 160 | |
| 161 | private function getFeatureFlagChecker(): FeatureFlagCheckerInterface |
| 162 | { |
| 163 | return $this->getServiceLocator()->get(FeatureFlagChecker::class); |
| 164 | } |
| 165 | } |