Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| PortableElementDirectoryParser | |
0.00% |
0 / 21 |
|
0.00% |
0 / 5 |
132 | |
0.00% |
0 / 1 |
| validate | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| extract | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getManifestContent | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| hasValidPortableElement | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| assertSourceAsDirectory | |
0.00% |
0 / 2 |
|
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) 2016 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoQtiItem\model\portableElement\parser\element; |
| 23 | |
| 24 | use oat\taoQtiItem\model\portableElement\exception\PortableElementInconsistencyModelException; |
| 25 | use oat\taoQtiItem\model\portableElement\exception\PortableElementParserException; |
| 26 | use oat\taoQtiItem\model\portableElement\model\PortableElementModelTrait; |
| 27 | use oat\taoQtiItem\model\portableElement\parser\PortableElementParser; |
| 28 | use oat\taoQtiItem\model\qti\exception\ExtractException; |
| 29 | use common_Exception; |
| 30 | |
| 31 | /** |
| 32 | * Parser of a QTI Portable element package |
| 33 | * A Portable element package must contain a manifest pciCreator.json in the root as well as a pciCreator.js creator |
| 34 | * file |
| 35 | * |
| 36 | * @package taoQtiItem |
| 37 | */ |
| 38 | abstract class PortableElementDirectoryParser implements PortableElementParser |
| 39 | { |
| 40 | use PortableElementModelTrait; |
| 41 | |
| 42 | /** |
| 43 | * Validate the source directory |
| 44 | * |
| 45 | * @param string $source Directory path to validate |
| 46 | * @return bool |
| 47 | * @throws ExtractException |
| 48 | * @throws PortableElementParserException |
| 49 | * @throws common_Exception |
| 50 | */ |
| 51 | public function validate($source) |
| 52 | { |
| 53 | $this->assertSourceAsDirectory($source); |
| 54 | |
| 55 | $definitionFiles = $this->getModel()->getDefinitionFiles(); |
| 56 | foreach ($definitionFiles as $file) { |
| 57 | if (! file_exists($source . DIRECTORY_SEPARATOR . $file)) { |
| 58 | throw new PortableElementParserException('A portable element package must contains a "' . $file . |
| 59 | '" file at the root of the directory: "' . $source . '"'); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | $this->getModel()->createDataObject($this->getManifestContent($source)); |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Return the source directory |
| 69 | * |
| 70 | * @param string $source Directory path |
| 71 | * @return string |
| 72 | * @throws ExtractException |
| 73 | */ |
| 74 | public function extract($source) |
| 75 | { |
| 76 | $this->assertSourceAsDirectory($source); |
| 77 | return $source; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Return the manifest content found in the source directory as json array |
| 82 | * |
| 83 | * @param string $source Directory path |
| 84 | * @return array |
| 85 | * @throws PortableElementInconsistencyModelException |
| 86 | * @throws common_Exception |
| 87 | */ |
| 88 | public function getManifestContent($source) |
| 89 | { |
| 90 | $content = json_decode(file_get_contents($source . DIRECTORY_SEPARATOR |
| 91 | . $this->getModel()->getManifestName()), true); |
| 92 | if (json_last_error() === JSON_ERROR_NONE) { |
| 93 | return $content; |
| 94 | } |
| 95 | throw new common_Exception('Portable element manifest is not a valid json file.'); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Check if the source directory contains a valid portable element model |
| 100 | * |
| 101 | * @param string $source Directory path |
| 102 | * @return bool |
| 103 | */ |
| 104 | public function hasValidPortableElement($source) |
| 105 | { |
| 106 | try { |
| 107 | if ($this->validate($source)) { |
| 108 | return true; |
| 109 | } |
| 110 | } catch (\common_Exception $e) { |
| 111 | } |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Check if $source is a directory |
| 117 | * |
| 118 | * @param string $source Directory path |
| 119 | * @throws ExtractException |
| 120 | */ |
| 121 | protected function assertSourceAsDirectory($source) |
| 122 | { |
| 123 | if (! is_dir($source)) { |
| 124 | throw new ExtractException('Invalid directory'); |
| 125 | } |
| 126 | } |
| 127 | } |