Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
PauseService
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 saveItemResponse
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
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\models\runner\QtiRunnerServiceContext;
29use oat\taoQtiTest\models\runner\RunnerService;
30
31class PauseService
32{
33    /** @var RunnerService */
34    private $runnerService;
35
36    /** @var ItemResponseRepositoryInterface */
37    private $itemResponseRepository;
38
39    public function __construct(
40        RunnerService $runnerService,
41        ItemResponseRepositoryInterface $itemResponseRepository
42    ) {
43        $this->runnerService = $runnerService;
44        $this->itemResponseRepository = $itemResponseRepository;
45    }
46
47    public function __invoke(PauseCommand $command): ActionResponse
48    {
49        $serviceContext = $command->getServiceContext();
50
51        $this->saveItemResponse($command, $serviceContext);
52
53        $serviceContext->init();
54
55        $result = $this->runnerService->pause($serviceContext);
56
57        if ($result === false) {
58            return ActionResponse::empty();
59        }
60
61        return ActionResponse::success();
62    }
63
64    protected function saveItemResponse(
65        PauseCommand $command,
66        QtiRunnerServiceContext $serviceContext
67    ): void {
68        if ($this->runnerService->isTerminated($serviceContext)) {
69            return;
70        }
71
72        $itemResponse = $command->getItemResponse();
73
74        $timerTarget = $this->runnerService->getTestConfig()
75            ->getConfigValue('timer.target');
76
77        if ($timerTarget === 'server') {
78            $itemResponse->removeDuration();
79        }
80
81        $this->itemResponseRepository->save($itemResponse, $serviceContext);
82    }
83}