Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
MoveService
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
5
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 MoveService
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(MoveCommand $command): ActionResponse
58    {
59        $serviceContext = $command->getServiceContext();
60
61        $this->itemResponseRepository->save($command->getItemResponse(), $serviceContext);
62        $this->toolsStateRepository->save($command->getToolsState(), $serviceContext);
63
64        $this->runnerService->check($serviceContext);
65        $serviceContext->init();
66
67        $serviceContext->getTestSession()->initItemTimer($command->getTimestamp());
68
69        $result = $this->runnerService->move(
70            $serviceContext,
71            $command->getDirection(),
72            $command->getScope(),
73            $command->getRef()
74        );
75
76        if ($command->hasStartTimer()) {
77            $this->runnerService->startTimer($serviceContext, $command->getTimestamp());
78        }
79
80        if ($this->runnerService instanceof PersistableRunnerServiceInterface) {
81            $this->runnerService->persist($serviceContext);
82        }
83
84        if ($result === false) {
85            return ActionResponse::empty();
86        }
87
88        $testMap = $serviceContext->containsAdaptive()
89            ? $this->runnerService->getTestMap($serviceContext, true)
90            : null;
91
92        return ActionResponse::success(
93            $this->runnerService->getTestContext($serviceContext),
94            $testMap
95        );
96    }
97}