Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
CompiledTestConverterFactory
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
8
100.00% covered (success)
100.00%
1 / 1
 createConverter
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 getOutputCompilationService
100.00% covered (success)
100.00%
11 / 11
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) 2019-2021  (original work) Open Assessment Technologies SA;
19 */
20
21namespace oat\taoDeliveryRdf\model\assembly;
22
23use InvalidArgumentException;
24use oat\oatbox\service\ConfigurableService;
25use oat\taoQtiTest\models\CompilationDataService;
26use oat\taoQtiTest\models\PhpCodeCompilationDataService;
27use oat\taoQtiTest\models\PhpSerializationCompilationDataService;
28use oat\taoQtiTest\models\XmlCompilationDataService;
29
30class CompiledTestConverterFactory extends ConfigurableService
31{
32    public const COMPILED_TEST_FORMAT_XML            = 'xml';
33    public const COMPILED_TEST_FORMAT_PHP            = 'php';
34    public const COMPILED_TEST_FORMAT_PHP_SERIALIZED = 'php_serialized';
35
36    /**
37     * @param $outputTestFormat
38     * @return CompiledTestConverterService|null
39     * @throws UnsupportedCompiledTestFormatException
40     */
41    public function createConverter($outputTestFormat)
42    {
43        if (!is_string($outputTestFormat)) {
44            throw new InvalidArgumentException('Output compiled test type parameter must be a string.');
45        }
46
47        $outputCompilationService = $this->getOutputCompilationService($outputTestFormat);
48        /** @var CompilationDataService $systemCompilationService */
49        $systemCompilationService = $this->getServiceLocator()->get(CompilationDataService::SERVICE_ID);
50
51        if (get_class($outputCompilationService) === get_class($systemCompilationService)) {
52            return null;
53        }
54
55        return new CompiledTestConverterService($systemCompilationService, $outputCompilationService);
56    }
57
58    /**
59     * @param $outputTestFormat
60     * @return CompilationDataService
61     * @throws UnsupportedCompiledTestFormatException
62     */
63    private function getOutputCompilationService($outputTestFormat)
64    {
65        $outputTestFormat = strtolower(trim($outputTestFormat));
66        switch ($outputTestFormat) {
67            case self::COMPILED_TEST_FORMAT_PHP:
68                $outputCompilationService = new PhpCodeCompilationDataService();
69
70                break;
71            case self::COMPILED_TEST_FORMAT_PHP_SERIALIZED:
72                $outputCompilationService = new PhpSerializationCompilationDataService();
73
74                break;
75            case self::COMPILED_TEST_FORMAT_XML:
76                $outputCompilationService = new XmlCompilationDataService();
77
78                break;
79            default:
80                throw new UnsupportedCompiledTestFormatException(
81                    "Unsupported compiled test format provided: {$outputTestFormat}"
82                );
83        }
84
85        return $this->propagate($outputCompilationService);
86    }
87}