Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.55% covered (success)
96.55%
28 / 29
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
TimeoutService
96.55% covered (success)
96.55%
28 / 29
50.00% covered (danger)
50.00%
1 / 2
8
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
96.15% covered (success)
96.15%
25 / 26
0.00% covered (danger)
0.00%
0 / 1
7
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
23declare(strict_types=1);
24
25namespace oat\taoQtiTest\model\Service;
26
27use oat\taoQtiTest\model\Domain\Model\ItemResponseRepositoryInterface;
28use oat\taoQtiTest\model\Domain\Model\ToolsStateRepositoryInterface;
29use oat\taoQtiTest\models\classes\runner\QtiRunnerInvalidResponsesException;
30use oat\taoQtiTest\models\runner\PersistableRunnerServiceInterface;
31use oat\taoQtiTest\models\runner\QtiRunnerEmptyResponsesException;
32use oat\taoQtiTest\models\runner\RunnerService;
33
34class TimeoutService
35{
36    /** @var RunnerService */
37    private $runnerService;
38
39    /** @var ItemResponseRepositoryInterface */
40    private $itemResponseRepository;
41
42    /** @var ToolsStateRepositoryInterface */
43    private $toolsStateRepository;
44
45    public function __construct(
46        RunnerService $runnerService,
47        ItemResponseRepositoryInterface $itemResponseRepository,
48        ToolsStateRepositoryInterface $toolsStateRepository
49    ) {
50        $this->runnerService = $runnerService;
51        $this->itemResponseRepository = $itemResponseRepository;
52        $this->toolsStateRepository = $toolsStateRepository;
53    }
54
55    public function __invoke(TimeoutCommand $command): ActionResponse
56    {
57        $serviceContext = $command->getServiceContext();
58
59        if ($command->isLateSubmissionAllowed()) {
60            try {
61                $this->itemResponseRepository->save($command->getItemResponse(), $serviceContext);
62            } catch (QtiRunnerInvalidResponsesException|QtiRunnerEmptyResponsesException) {
63                // allow the session to time out omitting the invalid response
64            }
65        }
66
67        $this->toolsStateRepository->save($command->getToolsState(), $serviceContext);
68        $this->runnerService->check($serviceContext);
69        $serviceContext->init();
70
71        $result = $this->runnerService->timeout(
72            $serviceContext,
73            $command->getScope(),
74            $command->getRef(),
75            $command->isLateSubmissionAllowed()
76        );
77
78        if ($command->hasStartTimer()) {
79            $this->runnerService->startTimer($serviceContext, $command->getTimestamp());
80        }
81
82        if ($this->runnerService instanceof PersistableRunnerServiceInterface) {
83            $this->runnerService->persist($serviceContext);
84        }
85
86        if ($result === false) {
87            return ActionResponse::empty();
88        }
89
90        $testMap = $serviceContext->containsAdaptive()
91            ? $this->runnerService->getTestMap($serviceContext, true)
92            : null;
93
94        return ActionResponse::success(
95            $this->runnerService->getTestContext($serviceContext),
96            $testMap
97        );
98    }
99}