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
TestRunnerActionResolver
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
 resolve
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 checkData
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 checkClass
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
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) 2020  (original work) Open Assessment Technologies SA;
19 *
20 * @author Oleksandr Zagovorychev <zagovorichev@gmail.com>
21 */
22
23declare(strict_types=1);
24
25namespace oat\taoQtiTest\models\runner\synchronisation\synchronisationService;
26
27use common_exception_InconsistentData;
28use oat\oatbox\service\ConfigurableService;
29use oat\taoQtiTest\models\runner\synchronisation\TestRunnerAction;
30use ResolverException;
31
32class TestRunnerActionResolver extends ConfigurableService
33{
34    /**
35     * @param array $data
36     * @param array $availableActions
37     * @return TestRunnerAction
38     * @throws common_exception_InconsistentData
39     * @throws ResolverException
40     */
41    public function resolve(array $data, array $availableActions): TestRunnerAction
42    {
43        $this->checkData($data);
44
45        $actionName = $data['action'];
46        $actionClass = (string) ($availableActions[$actionName] ?? '');
47        $this->checkClass($actionClass, $actionName);
48
49        return $this->propagate(new $actionClass($actionName, $data['timestamp'], $data['parameters']));
50    }
51
52    /**
53     * @param $data
54     * @throws common_exception_InconsistentData
55     */
56    private function checkData($data): void
57    {
58        if (
59            !isset(
60                $data['action'],
61                $data['timestamp'],
62                $data['parameters']
63            )
64        ) {
65            throw new common_exception_InconsistentData(
66                'Action parameters have to contain "action", "timestamp" and "parameters" fields.'
67            );
68        }
69
70        if (!is_array($data['parameters'])) {
71            throw new common_exception_InconsistentData(
72                'Action parameters have to contain "parameters" field as an array.'
73            );
74        }
75    }
76
77    /**
78     * @param $actionClass
79     * @param string $actionName
80     * @throws ResolverException
81     */
82    private function checkClass(string $actionClass, string $actionName): void
83    {
84        if (!is_a($actionClass, TestRunnerAction::class, true)) {
85            throw new ResolverException('Action name "' . $actionName . '" could not be resolved.');
86        }
87    }
88}