Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.86% covered (success)
92.86%
13 / 14
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExitTestService
92.86% covered (success)
92.86%
13 / 14
50.00% covered (danger)
50.00%
1 / 2
4.01
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
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
3.01
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 common_Exception;
28use oat\taoQtiTest\model\Domain\Model\ItemResponseRepositoryInterface;
29use oat\taoQtiTest\model\Domain\Model\ToolsStateRepositoryInterface;
30use oat\taoQtiTest\models\runner\PersistableRunnerServiceInterface;
31use oat\taoQtiTest\models\runner\RunnerService;
32
33class ExitTestService
34{
35    /** @var RunnerService */
36    private $runnerService;
37
38    /** @var ItemResponseRepositoryInterface */
39    private $itemResponseRepository;
40
41    /** @var ToolsStateRepositoryInterface */
42    private $toolsStateRepository;
43
44    public function __construct(
45        RunnerService $runnerService,
46        ItemResponseRepositoryInterface $itemResponseRepository,
47        ToolsStateRepositoryInterface $toolsStateRepository
48    ) {
49        $this->runnerService = $runnerService;
50        $this->itemResponseRepository = $itemResponseRepository;
51        $this->toolsStateRepository = $toolsStateRepository;
52    }
53
54    /**
55     * @throws common_Exception
56     */
57    public function __invoke(ExitTestCommand $command): ActionResponse
58    {
59        $serviceContext = $command->getServiceContext();
60
61        $this->runnerService->check($serviceContext);
62        $serviceContext->init();
63
64        $this->itemResponseRepository->save($command->getItemResponse(), $serviceContext);
65        $this->toolsStateRepository->save($command->getToolsState(), $serviceContext);
66
67        $result = $this->runnerService->exitTest($serviceContext);
68
69        if ($this->runnerService instanceof PersistableRunnerServiceInterface) {
70            $this->runnerService->persist($serviceContext);
71        }
72
73        if ($result === false) {
74            return ActionResponse::empty();
75        }
76
77        return ActionResponse::success();
78    }
79}