Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
39 / 39 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
StoreTraceVariablesService | |
100.00% |
39 / 39 |
|
100.00% |
4 / 4 |
9 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
__invoke | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
4 | |||
getItemRef | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
saveTraceVariable | |
100.00% |
12 / 12 |
|
100.00% |
1 / 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 (original work) Open Assessment Technologies SA; |
19 | * |
20 | * @author Ricardo Quintanilha <ricardo.quintanilha@taotesting.com> |
21 | */ |
22 | |
23 | declare(strict_types=1); |
24 | |
25 | namespace oat\taoQtiTest\model\Service; |
26 | |
27 | use Exception; |
28 | use oat\oatbox\event\EventManager; |
29 | use oat\taoQtiTest\models\event\TraceVariableStored; |
30 | use oat\taoQtiTest\models\runner\RunnerService; |
31 | use oat\taoQtiTest\models\runner\RunnerServiceContext; |
32 | use Psr\Log\LoggerInterface; |
33 | |
34 | class StoreTraceVariablesService |
35 | { |
36 | /** @var RunnerService */ |
37 | private $runnerService; |
38 | |
39 | /** @var EventManager */ |
40 | private $eventManager; |
41 | |
42 | /** @var LoggerInterface */ |
43 | private $logger; |
44 | |
45 | public function __construct(RunnerService $runnerService, EventManager $eventManager, LoggerInterface $logger) |
46 | { |
47 | $this->runnerService = $runnerService; |
48 | $this->eventManager = $eventManager; |
49 | $this->logger = $logger; |
50 | } |
51 | |
52 | public function __invoke(StoreTraceVariablesCommand $command): ActionResponse |
53 | { |
54 | $serviceContext = $command->getServiceContext(); |
55 | |
56 | $itemRef = $this->getItemRef($command); |
57 | |
58 | $storedVariables = []; |
59 | foreach ($command->getTraceVariables() as $variableIdentifier => $variableValue) { |
60 | $stored = $this->saveTraceVariable($serviceContext, $itemRef, $variableIdentifier, $variableValue); |
61 | |
62 | if ($stored) { |
63 | $storedVariables[$variableIdentifier] = $variableValue; |
64 | } |
65 | } |
66 | |
67 | if (empty($storedVariables)) { |
68 | return ActionResponse::success(); |
69 | } |
70 | |
71 | $this->eventManager->trigger( |
72 | new TraceVariableStored( |
73 | $serviceContext->getTestSession()->getSessionId(), |
74 | $storedVariables |
75 | ) |
76 | ); |
77 | |
78 | return ActionResponse::success(); |
79 | } |
80 | |
81 | private function getItemRef(StoreTraceVariablesCommand $command): ?string |
82 | { |
83 | if ($command->getItemIdentifier() === null) { |
84 | return null; |
85 | } |
86 | |
87 | $itemRef = $this->runnerService->getItemHref( |
88 | $command->getServiceContext(), |
89 | $command->getItemIdentifier() |
90 | ); |
91 | |
92 | $parts = explode('|', $itemRef); |
93 | |
94 | return $parts[0] ?? $itemRef; |
95 | } |
96 | |
97 | private function saveTraceVariable( |
98 | RunnerServiceContext $serviceContext, |
99 | ?string $itemRef, |
100 | string $variableIdentifier, |
101 | $variableValue |
102 | ): bool { |
103 | try { |
104 | $this->runnerService->storeTraceVariable( |
105 | $serviceContext, |
106 | $itemRef, |
107 | $variableIdentifier, |
108 | $variableValue |
109 | ); |
110 | |
111 | |
112 | return true; |
113 | } catch (Exception $exception) { |
114 | $this->logger->warning( |
115 | sprintf('Failed to store trace variable "%s": %s', $variableIdentifier, $exception->getMessage()) |
116 | ); |
117 | } |
118 | |
119 | return false; |
120 | } |
121 | } |