Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
13.43% |
9 / 67 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| AssemblyExporterService | |
13.43% |
9 / 67 |
|
0.00% |
0 / 5 |
204.48 | |
0.00% |
0 / 1 |
| __construct | |
56.25% |
9 / 16 |
|
0.00% |
0 / 1 |
3.75 | |||
| exportCompiledDelivery | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
| doExportCompiledDelivery | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
42 | |||
| setupCompiledTestConverter | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getAssemblyFilesReader | |
0.00% |
0 / 3 |
|
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) 2019 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | namespace oat\taoDeliveryRdf\model\export; |
| 22 | |
| 23 | use oat\taoDeliveryRdf\model\assembly\CompiledTestConverterFactory; |
| 24 | use ZipArchive; |
| 25 | use Exception; |
| 26 | use InvalidArgumentException; |
| 27 | use common_Exception; |
| 28 | use core_kernel_classes_EmptyProperty; |
| 29 | use tao_helpers_Display; |
| 30 | use tao_helpers_Export; |
| 31 | use tao_helpers_File; |
| 32 | use core_kernel_classes_Resource; |
| 33 | use common_ext_ExtensionsManager; |
| 34 | use tao_models_classes_service_ServiceCall; |
| 35 | use tao_models_classes_export_RdfExporter; |
| 36 | use oat\generis\model\OntologyAwareTrait; |
| 37 | use oat\oatbox\log\LoggerAwareTrait; |
| 38 | use oat\oatbox\service\ConfigurableService; |
| 39 | use oat\tao\model\service\ServiceFileStorage; |
| 40 | use oat\taoDeliveryRdf\model\assembly\AssemblyFilesReaderInterface; |
| 41 | use oat\taoDeliveryRdf\model\assembly\UnsupportedCompiledTestFormatException; |
| 42 | use oat\taoDeliveryRdf\model\DeliveryAssemblyService; |
| 43 | |
| 44 | class AssemblyExporterService extends ConfigurableService |
| 45 | { |
| 46 | use LoggerAwareTrait; |
| 47 | use OntologyAwareTrait; |
| 48 | |
| 49 | |
| 50 | public const SERVICE_ID = 'taoDeliveryRdf/AssemblyExporterService'; |
| 51 | public const OPTION_ASSEMBLY_FILES_READER = 'assembly_files_reader'; |
| 52 | public const OPTION_RDF_EXPORTER = 'rdf_exporter'; |
| 53 | public const MANIFEST_FILENAME = 'manifest.json'; |
| 54 | public const DELIVERY_RDF_FILENAME = 'delivery.rdf'; |
| 55 | /** |
| 56 | * @var AssemblyFilesReaderInterface |
| 57 | */ |
| 58 | private $assemblyFilesReader; |
| 59 | /** |
| 60 | * @var tao_models_classes_export_RdfExporter |
| 61 | */ |
| 62 | private $rdfExporter; |
| 63 | /** |
| 64 | * AssemblyExporterService constructor. |
| 65 | * @param array $options |
| 66 | */ |
| 67 | public function __construct($options = []) |
| 68 | { |
| 69 | parent::__construct($options); |
| 70 | if (!$this->getOption(self::OPTION_ASSEMBLY_FILES_READER) instanceof AssemblyFilesReaderInterface) { |
| 71 | throw new InvalidArgumentException( |
| 72 | sprintf( |
| 73 | '%s option value must be an instance of %s', |
| 74 | self::OPTION_ASSEMBLY_FILES_READER, |
| 75 | AssemblyFilesReaderInterface::class |
| 76 | ) |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | $this->rdfExporter = $this->getOption(self::OPTION_RDF_EXPORTER); |
| 81 | if (!$this->rdfExporter instanceof tao_models_classes_export_RdfExporter) { |
| 82 | throw new InvalidArgumentException( |
| 83 | '%s option value must be an instance of %s', |
| 84 | self::OPTION_RDF_EXPORTER, |
| 85 | tao_models_classes_export_RdfExporter::class |
| 86 | ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Export Compiled Delivery |
| 92 | * |
| 93 | * Exports a delivery into its compiled form. In case of the $fsExportPath argument is set, |
| 94 | * the compiled delivery will be stored in the 'taoDelivery' shared file system, at $fsExportPath location. |
| 95 | * |
| 96 | * @param core_kernel_classes_Resource $compiledDelivery |
| 97 | * @param string $outputTestFormat Format compiled test file in output assembly package. |
| 98 | * |
| 99 | * @return string The path to the compiled delivery on the local file system OR the 'taoDelivery' shared file |
| 100 | * system, depending on whether $fsExportPath is set. |
| 101 | * |
| 102 | * @throws common_Exception |
| 103 | * @throws core_kernel_classes_EmptyProperty |
| 104 | */ |
| 105 | public function exportCompiledDelivery(core_kernel_classes_Resource $compiledDelivery, $outputTestFormat) |
| 106 | { |
| 107 | $this->logDebug("Exporting Delivery Assembly '" . $compiledDelivery->getUri() . "'..."); |
| 108 | $fileName = tao_helpers_Display::textCleaner($compiledDelivery->getLabel()) . '.zip'; |
| 109 | $path = tao_helpers_File::concat([tao_helpers_Export::getExportPath(), $fileName]); |
| 110 | if (!tao_helpers_File::securityCheck($path, true)) { |
| 111 | throw new Exception('Unauthorized file name'); |
| 112 | } |
| 113 | |
| 114 | // If such a target zip file exists, remove it from local filesystem. It prevents some synchronicity issues |
| 115 | // to occur while dealing with ZIP Archives (not explained yet). |
| 116 | if (file_exists($path)) { |
| 117 | unlink($path); |
| 118 | } |
| 119 | |
| 120 | $zipArchive = new ZipArchive(); |
| 121 | if ($zipArchive->open($path, ZipArchive::CREATE) !== true) { |
| 122 | throw new Exception('Unable to create archive at ' . $path); |
| 123 | } |
| 124 | |
| 125 | $this->setupCompiledTestConverter($outputTestFormat); |
| 126 | $this->doExportCompiledDelivery($path, $compiledDelivery, $zipArchive); |
| 127 | $zipArchive->close(); |
| 128 | return $path; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Do Export Compiled Delivery |
| 133 | * |
| 134 | * Method containing the main behavior of exporting a compiled delivery into a ZIP archive. |
| 135 | * |
| 136 | * For developers wanting to override this method, the following information has to be taken into account: |
| 137 | * |
| 138 | * - The value of the $zipArgive argument is an already open ZipArchive object. |
| 139 | * - The method must keep the archive open after its execution (calling code will take care of it). |
| 140 | * |
| 141 | * @param $path |
| 142 | * @param core_kernel_classes_Resource $compiledDelivery |
| 143 | * @param ZipArchive $zipArchive |
| 144 | * @throws common_Exception |
| 145 | * @throws core_kernel_classes_EmptyProperty |
| 146 | */ |
| 147 | protected function doExportCompiledDelivery( |
| 148 | $path, |
| 149 | core_kernel_classes_Resource $compiledDelivery, |
| 150 | ZipArchive $zipArchive |
| 151 | ) { |
| 152 | $taoDeliveryVersion = common_ext_ExtensionsManager::singleton()->getInstalledVersion('taoDelivery'); |
| 153 | $data = [ |
| 154 | 'dir' => [], |
| 155 | 'label' => $compiledDelivery->getLabel(), |
| 156 | 'version' => $taoDeliveryVersion |
| 157 | ]; |
| 158 | $directories = $compiledDelivery->getPropertyValues( |
| 159 | $this->getProperty(DeliveryAssemblyService::PROPERTY_DELIVERY_DIRECTORY) |
| 160 | ); |
| 161 | foreach ($directories as $id) { |
| 162 | $directory = $this->getServiceLocator()->get(ServiceFileStorage::SERVICE_ID)->getDirectoryById($id); |
| 163 | foreach ($this->getAssemblyFilesReader()->getFiles($directory) as $filePath => $fileStream) { |
| 164 | tao_helpers_File::addFilesToZip($zipArchive, $fileStream, $filePath); |
| 165 | } |
| 166 | $data['dir'][$id] = $directory->getPrefix(); |
| 167 | } |
| 168 | |
| 169 | $runtime = $compiledDelivery->getOnePropertyValue( |
| 170 | $this->getProperty(DeliveryAssemblyService::PROPERTY_DELIVERY_RUNTIME) |
| 171 | ); |
| 172 | $serviceCall = $runtime instanceof core_kernel_classes_Resource |
| 173 | ? tao_models_classes_service_ServiceCall::fromResource($runtime) |
| 174 | : tao_models_classes_service_ServiceCall::fromString((string)$runtime); |
| 175 | $data['runtime'] = base64_encode(json_encode($serviceCall)); |
| 176 | $rdfData = $this->rdfExporter->getRdfString([$compiledDelivery]); |
| 177 | if (!$zipArchive->addFromString(self::DELIVERY_RDF_FILENAME, $rdfData)) { |
| 178 | throw new common_Exception('Unable to add metadata to exported delivery assembly'); |
| 179 | } |
| 180 | $data['meta'] = self::DELIVERY_RDF_FILENAME; |
| 181 | $content = json_encode($data); |
| 182 | if (!$zipArchive->addFromString(self::MANIFEST_FILENAME, $content)) { |
| 183 | $zipArchive->close(); |
| 184 | unlink($path); |
| 185 | throw new common_Exception('Unable to add manifest to exported delivery assembly'); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @param string $outputTestFormat |
| 191 | * @return void |
| 192 | * |
| 193 | * @throws UnsupportedCompiledTestFormatException |
| 194 | */ |
| 195 | private function setupCompiledTestConverter($outputTestFormat) |
| 196 | { |
| 197 | /** @var CompiledTestConverterFactory $compiledTestConverterFactory */ |
| 198 | $compiledTestConverterFactory = $this->getServiceLocator()->get(CompiledTestConverterFactory::class); |
| 199 | $converter = $compiledTestConverterFactory->createConverter($outputTestFormat); |
| 200 | |
| 201 | if ($converter) { |
| 202 | $this->getAssemblyFilesReader()->setCompiledTestConverter($converter); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * @return AssemblyFilesReaderInterface |
| 208 | */ |
| 209 | private function getAssemblyFilesReader() |
| 210 | { |
| 211 | if (!$this->assemblyFilesReader instanceof AssemblyFilesReaderInterface) { |
| 212 | $this->assemblyFilesReader = $this->getOption(self::OPTION_ASSEMBLY_FILES_READER); |
| 213 | } |
| 214 | |
| 215 | return $this->assemblyFilesReader; |
| 216 | } |
| 217 | } |