Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 17 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Packer | |
0.00% |
0 / 17 |
|
0.00% |
0 / 3 |
72 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getTestPacker | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
| pack | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 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) 2015 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoTests\models\pack; |
| 23 | |
| 24 | use core_kernel_classes_Resource; |
| 25 | use taoTests_models_classes_TestsService; |
| 26 | use common_exception_NoImplementation; |
| 27 | use ReflectionClass; |
| 28 | use common_Exception; |
| 29 | use Exception; |
| 30 | |
| 31 | /** |
| 32 | * The Test Packer calls the packable class for the given test |
| 33 | * |
| 34 | * @package taoTests |
| 35 | * @author Bertrand Chevrier <bertrand@taotesting.com> |
| 36 | */ |
| 37 | class Packer |
| 38 | { |
| 39 | /** |
| 40 | * The test to pack |
| 41 | * @var core_kernel_classes_Resource |
| 42 | */ |
| 43 | private $test; |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * The test service |
| 48 | * @var \taoTests_models_classes_TestsService |
| 49 | */ |
| 50 | private $testService; |
| 51 | |
| 52 | /** |
| 53 | * Create a packer for a test |
| 54 | * @param core_kernel_classes_Resource $test |
| 55 | */ |
| 56 | public function __construct(core_kernel_classes_Resource $test) |
| 57 | { |
| 58 | $this->test = $test; |
| 59 | $this->testService = taoTests_models_classes_TestsService::singleton(); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get the packer for the test regarding it's implementation. |
| 64 | * |
| 65 | * @return Packable the test packer implementation |
| 66 | * @throws common_exception_NoImplementation |
| 67 | */ |
| 68 | private function getTestPacker() |
| 69 | { |
| 70 | |
| 71 | //look at the item model |
| 72 | $testModel = $this->testService->getTestModel($this->test); |
| 73 | if (is_null($testModel)) { |
| 74 | throw new common_exception_NoImplementation('No test model for test ' . $this->test->getUri()); |
| 75 | } |
| 76 | |
| 77 | //get the testModel implementation for this model |
| 78 | $impl = $this->testService->getTestModelImplementation($testModel); |
| 79 | if (is_null($impl)) { |
| 80 | throw new common_exception_NoImplementation('No implementation for model ' . $testModel->getUri()); |
| 81 | } |
| 82 | |
| 83 | //then retrieve the packer class and instantiate it |
| 84 | $packerClass = new ReflectionClass($impl->getPackerClass()); |
| 85 | if (is_null($packerClass) || !$packerClass->implementsInterface('oat\taoTests\models\pack\Packable')) { |
| 86 | throw new common_exception_NoImplementation('The packer class seems to be not implemented'); |
| 87 | } |
| 88 | |
| 89 | return $packerClass->newInstance(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Pack a test. |
| 94 | * |
| 95 | * @return TestPack of the test. It can be serialized directly. |
| 96 | * @throws common_Exception |
| 97 | */ |
| 98 | public function pack() |
| 99 | { |
| 100 | |
| 101 | try { |
| 102 | //call the factory to get the itemPacker implementation |
| 103 | $testPacker = $this->getTestPacker(); |
| 104 | |
| 105 | //then create the pack |
| 106 | $testPack = $testPacker->packTest($this->test); |
| 107 | } catch (Exception $e) { |
| 108 | throw new common_Exception('The test ' . $this->test->getUri() . ' cannot be packed : ' . $e->getMessage()); |
| 109 | } |
| 110 | |
| 111 | return $testPack; |
| 112 | } |
| 113 | } |