Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
30.00% |
21 / 70 |
|
13.33% |
2 / 15 |
CRAP | |
0.00% |
0 / 1 |
| PersistDataService | |
30.00% |
21 / 70 |
|
13.33% |
2 / 15 |
221.57 | |
0.00% |
0 / 1 |
| persist | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| remove | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| getDataStoreFilesystem | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFolderName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| removeArchive | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| persistArchive | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
| moveExportedZipTest | |
38.46% |
5 / 13 |
|
0.00% |
0 / 1 |
5.10 | |||
| getTenantId | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getTestExporter | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| getFileSystemManager | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getZipFileName | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| getZipFileDirectory | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| addMetadataToZipFile | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| saveMetaData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getZipArchive | |
0.00% |
0 / 2 |
|
0.00% |
0 / 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) 2021 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoDeliveryRdf\model\DataStore; |
| 24 | |
| 25 | use common_Exception; |
| 26 | use common_exception_Error; |
| 27 | use common_exception_NotFound; |
| 28 | use oat\oatbox\filesystem\FilesystemInterface; |
| 29 | use oat\oatbox\filesystem\FileSystemService; |
| 30 | use oat\oatbox\service\ConfigurableService; |
| 31 | use oat\tao\helpers\FileHelperService; |
| 32 | use tao_helpers_Uri; |
| 33 | use tao_models_classes_export_ExportHandler as ExporterInterface; |
| 34 | use oat\taoQtiTest\models\export\Formats\Package2p2\TestPackageExport; |
| 35 | use Throwable; |
| 36 | use ZipArchive; |
| 37 | |
| 38 | class PersistDataService extends ConfigurableService |
| 39 | { |
| 40 | public const OPTION_EXPORTER_SERVICE = 'exporter_service'; |
| 41 | public const OPTION_ZIP_ARCHIVE_SERVICE = 'zipArchive'; |
| 42 | |
| 43 | private const PACKAGE_FILENAME = 'QTIPackage'; |
| 44 | private const ZIP_EXTENSION = '.zip'; |
| 45 | |
| 46 | /** |
| 47 | * @throws Throwable |
| 48 | * @throws common_Exception |
| 49 | * @throws common_exception_Error |
| 50 | * @throws common_exception_NotFound |
| 51 | */ |
| 52 | public function persist(ResourceSyncDTO $resourceSyncDTO): void |
| 53 | { |
| 54 | $this->persistArchive($resourceSyncDTO); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @throws common_exception_NotFound |
| 59 | * @throws common_exception_Error |
| 60 | */ |
| 61 | public function remove(ResourceSyncDTO $resourceSyncDTO): void |
| 62 | { |
| 63 | $this->removeArchive( |
| 64 | $resourceSyncDTO->getResourceId(), |
| 65 | $resourceSyncDTO->getFileSystemId(), |
| 66 | $this->getTenantId($resourceSyncDTO), |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @throws common_exception_Error |
| 72 | * @throws common_exception_NotFound |
| 73 | */ |
| 74 | private function getDataStoreFilesystem(string $fileSystemId): FilesystemInterface |
| 75 | { |
| 76 | return $this->getFileSystemManager()->getFileSystem($fileSystemId); |
| 77 | } |
| 78 | |
| 79 | private function getFolderName(string $resourceId): string |
| 80 | { |
| 81 | return tao_helpers_Uri::encode($resourceId); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @throws common_exception_Error |
| 86 | * @throws common_exception_NotFound |
| 87 | */ |
| 88 | private function removeArchive(string $resourceId, string $fileSystemId, string $tenantId): void |
| 89 | { |
| 90 | // There is a bug for gcp storage adapter - when deleting a dir with only one file exception is thrown |
| 91 | // This is why the file itself is removed first |
| 92 | $zipFileName = $this->getZipFileName($resourceId, $tenantId); |
| 93 | if ($this->getDataStoreFilesystem($fileSystemId)->fileExists($zipFileName)) { |
| 94 | $this->getDataStoreFilesystem($fileSystemId)->delete($zipFileName); |
| 95 | } |
| 96 | |
| 97 | $directoryPath = $this->getZipFileDirectory($resourceId, $tenantId); |
| 98 | if ($this->getDataStoreFilesystem($fileSystemId)->directoryExists($directoryPath)) { |
| 99 | $this->getDataStoreFilesystem($fileSystemId)->deleteDirectory($directoryPath); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @throws Throwable |
| 105 | * @throws common_Exception |
| 106 | * @throws common_exception_Error |
| 107 | * @throws common_exception_NotFound |
| 108 | */ |
| 109 | private function persistArchive(ResourceSyncDTO $resourceSyncDTO): void |
| 110 | { |
| 111 | /** @var FileHelperService $tempDir */ |
| 112 | $tempDir = $this->getServiceLocator()->get(FileHelperService::class); |
| 113 | $folder = $tempDir->createTempDir(); |
| 114 | |
| 115 | try { |
| 116 | $this->getTestExporter()->export( |
| 117 | [ |
| 118 | 'filename' => self::PACKAGE_FILENAME, |
| 119 | 'instances' => $resourceSyncDTO->getTestUri(), |
| 120 | 'uri' => $resourceSyncDTO->getTestUri() |
| 121 | ], |
| 122 | $folder |
| 123 | ); |
| 124 | |
| 125 | $this->moveExportedZipTest($folder, $resourceSyncDTO->getResourceId(), $resourceSyncDTO); |
| 126 | } finally { |
| 127 | $tempDir->removeDirectory($folder); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @throws common_exception_Error |
| 133 | * @throws common_exception_NotFound |
| 134 | */ |
| 135 | private function moveExportedZipTest( |
| 136 | string $folder, |
| 137 | string $resourceId, |
| 138 | ResourceSyncDTO $resourceSyncDTO |
| 139 | ): void { |
| 140 | $zipFiles = glob( |
| 141 | sprintf('%s%s*%s', $folder, self::PACKAGE_FILENAME, self::ZIP_EXTENSION) |
| 142 | ); |
| 143 | |
| 144 | $fileSystemId = $resourceSyncDTO->getFileSystemId(); |
| 145 | |
| 146 | if (!empty($zipFiles)) { |
| 147 | foreach ($zipFiles as $zipFile) { |
| 148 | $zipFileName = $this->getZipFileName($resourceId, $this->getTenantId($resourceSyncDTO)); |
| 149 | $this->addMetadataToZipFile($zipFile, $resourceSyncDTO); |
| 150 | |
| 151 | $contents = file_get_contents($zipFile); |
| 152 | |
| 153 | $this->getDataStoreFilesystem($fileSystemId)->write( |
| 154 | $zipFileName, |
| 155 | $contents |
| 156 | ); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | private function getTenantId(ResourceSyncDTO $resourceSyncDTO): string |
| 162 | { |
| 163 | if (!empty($resourceSyncDTO->getTenantId())) { |
| 164 | return $resourceSyncDTO->getTenantId(); |
| 165 | } |
| 166 | |
| 167 | if (!empty($resourceSyncDTO->getFirstTenantId())) { |
| 168 | return $resourceSyncDTO->getFirstTenantId(); |
| 169 | } |
| 170 | |
| 171 | return ""; |
| 172 | } |
| 173 | |
| 174 | private function getTestExporter(): ExporterInterface |
| 175 | { |
| 176 | $exporter = $this->getOption(self::OPTION_EXPORTER_SERVICE); |
| 177 | |
| 178 | if ($exporter) { |
| 179 | return $exporter; |
| 180 | } |
| 181 | |
| 182 | return new TestPackageExport(); |
| 183 | } |
| 184 | |
| 185 | private function getFileSystemManager(): FileSystemService |
| 186 | { |
| 187 | return $this->getServiceLocator()->get(FileSystemService::SERVICE_ID); |
| 188 | } |
| 189 | |
| 190 | private function getZipFileName(string $resourceId, string $tenantId): string |
| 191 | { |
| 192 | return sprintf( |
| 193 | '%s%s%s', |
| 194 | $this->getZipFileDirectory($resourceId, $tenantId), |
| 195 | self::PACKAGE_FILENAME, |
| 196 | self::ZIP_EXTENSION |
| 197 | ); |
| 198 | } |
| 199 | |
| 200 | private function getZipFileDirectory(string $resourceId, string $tenantId): string |
| 201 | { |
| 202 | return sprintf( |
| 203 | '%s-%s%s', |
| 204 | $this->getFolderName($resourceId), |
| 205 | $tenantId, |
| 206 | DIRECTORY_SEPARATOR, |
| 207 | ); |
| 208 | } |
| 209 | |
| 210 | private function addMetadataToZipFile(string $zipFile, ResourceSyncDTO $resourceSyncDTO): void |
| 211 | { |
| 212 | $zipArchive = $this->getZipArchive(); |
| 213 | |
| 214 | $zipArchive->open($zipFile); |
| 215 | |
| 216 | foreach ($resourceSyncDTO->getMetadata() as $metadataName => $metadata) { |
| 217 | if (!empty($metadata)) { |
| 218 | $this->saveMetaData($zipArchive, $metadataName . '.json', json_encode($metadata)); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | $zipArchive->close(); |
| 223 | } |
| 224 | |
| 225 | private function saveMetaData(ZipArchive $zipFile, string $fileNameToAdd, string $content): void |
| 226 | { |
| 227 | $zipFile->addFromString($fileNameToAdd, $content); |
| 228 | } |
| 229 | |
| 230 | private function getZipArchive(): ZipArchive |
| 231 | { |
| 232 | $zipArchive = $this->getOption(self::OPTION_ZIP_ARCHIVE_SERVICE); |
| 233 | |
| 234 | return $zipArchive ?? new ZipArchive(); |
| 235 | } |
| 236 | } |