Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 55 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| ResourceFileSerializer | |
0.00% |
0 / 55 |
|
0.00% |
0 / 7 |
240 | |
0.00% |
0 / 1 |
| serialize | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
12 | |||
| unserialize | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| unserializeFile | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| unserializeDirectory | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| cleanUp | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getRootDirectory | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getResourceFilePropertiesValues | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
30 | |||
| 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) 2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\generis\model\fileReference; |
| 23 | |
| 24 | use oat\generis\model\GenerisRdf; |
| 25 | use oat\generis\model\OntologyAwareTrait; |
| 26 | use oat\oatbox\filesystem\Directory; |
| 27 | use oat\oatbox\filesystem\File; |
| 28 | use oat\oatbox\filesystem\FileSystemService; |
| 29 | use oat\oatbox\service\ConfigurableService; |
| 30 | |
| 31 | /** |
| 32 | * Class ResourceFileSerializer |
| 33 | * |
| 34 | * @see UrlFileSerializer |
| 35 | */ |
| 36 | class ResourceFileSerializer extends ConfigurableService implements FileReferenceSerializer |
| 37 | { |
| 38 | use OntologyAwareTrait; |
| 39 | |
| 40 | public const RESOURCE_FILE_FILESYSTEM_URI = 'fileSystemUri'; |
| 41 | public const RESOURCE_FILE_PATH = 'path'; |
| 42 | public const RESOURCE_FILE_NAME = 'fileName'; |
| 43 | |
| 44 | /** |
| 45 | * @see FileReferenceSerializer::serialize |
| 46 | */ |
| 47 | public function serialize($abstraction) |
| 48 | { |
| 49 | $fileClass = $this->getClass(GenerisRdf::CLASS_GENERIS_FILE); |
| 50 | |
| 51 | if ($abstraction instanceof File) { |
| 52 | $filename = $abstraction->getBasename(); |
| 53 | $filePath = dirname($abstraction->getPrefix()); |
| 54 | } elseif ($abstraction instanceof Directory) { |
| 55 | $filename = ''; |
| 56 | $filePath = $abstraction->getPrefix(); |
| 57 | } else { |
| 58 | throw new FileSerializerException( |
| 59 | __CLASS__ . '::' . __FUNCTION__ . ' expects parameter to be an instance of Directory or File' |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | $resource = $fileClass->createInstanceWithProperties( |
| 64 | [ |
| 65 | GenerisRdf::PROPERTY_FILE_FILENAME => $filename, |
| 66 | GenerisRdf::PROPERTY_FILE_FILEPATH => $filePath, |
| 67 | GenerisRdf::PROPERTY_FILE_FILESYSTEM => $this->getResource($abstraction->getFileSystemId()), |
| 68 | ] |
| 69 | ); |
| 70 | |
| 71 | return $resource->getUri(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * This implementation uses resource URI as serial |
| 76 | * |
| 77 | * @see FileReferenceSerializer::unserialize |
| 78 | */ |
| 79 | public function unserialize($serial) |
| 80 | { |
| 81 | $properties = $this->getResourceFilePropertiesValues($serial); |
| 82 | $dir = $this->getRootDirectory($properties[self::RESOURCE_FILE_FILESYSTEM_URI]); |
| 83 | |
| 84 | return (isset($properties[self::RESOURCE_FILE_NAME]) && !empty($properties[self::RESOURCE_FILE_NAME])) |
| 85 | ? $dir->getFile($properties[self::RESOURCE_FILE_PATH] . '/' . $properties[self::RESOURCE_FILE_NAME]) |
| 86 | : $dir->getDirectory($properties[self::RESOURCE_FILE_PATH]); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * This implementation uses resource URI as serial |
| 91 | * |
| 92 | * @see FileReferenceSerializer::unserializeFile |
| 93 | */ |
| 94 | public function unserializeFile($serial) |
| 95 | { |
| 96 | $properties = $this->getResourceFilePropertiesValues($serial); |
| 97 | |
| 98 | return $this->getRootDirectory($properties[self::RESOURCE_FILE_FILESYSTEM_URI]) |
| 99 | ->getFile($properties[self::RESOURCE_FILE_PATH] . '/' . $properties[self::RESOURCE_FILE_NAME]); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * This implementation uses resource URI as serial |
| 104 | * |
| 105 | * @see FileReferenceSerializer::unserializeDirectory |
| 106 | */ |
| 107 | public function unserializeDirectory($serial) |
| 108 | { |
| 109 | $properties = $this->getResourceFilePropertiesValues($serial); |
| 110 | |
| 111 | return $this->getRootDirectory($properties[self::RESOURCE_FILE_FILESYSTEM_URI]) |
| 112 | ->getDirectory($properties[self::RESOURCE_FILE_PATH]); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * This implementation uses resource URI as serial |
| 117 | * |
| 118 | * @see FileReferenceSerializer::cleanUp |
| 119 | */ |
| 120 | public function cleanUp($serial) |
| 121 | { |
| 122 | $resourceFile = $this->getResource($serial); |
| 123 | $file = new \core_kernel_classes_Resource($resourceFile); |
| 124 | |
| 125 | return $file->delete(); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Return root directory represented by the given uri |
| 130 | * |
| 131 | * @return Directory |
| 132 | */ |
| 133 | protected function getRootDirectory($id) |
| 134 | { |
| 135 | return $this->getServiceLocator()->get(FileSystemService::SERVICE_ID)->getDirectory($id); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Return an array with filesystem uri and path following serial |
| 140 | * Serial is Resource file uri, data are extracted from database |
| 141 | * |
| 142 | * This implementation uses resource URI as serial |
| 143 | * |
| 144 | * @param $serial |
| 145 | * @return array |
| 146 | * @throws \common_exception_InvalidArgumentType |
| 147 | * @throws FileSerializerException |
| 148 | */ |
| 149 | protected function getResourceFilePropertiesValues($serial) |
| 150 | { |
| 151 | $file = $this->getResource($serial); |
| 152 | |
| 153 | if (!$file->exists()) { |
| 154 | throw new \common_exception_NotFound('File reference serial "' . $serial . '" not exist as resource'); |
| 155 | } |
| 156 | |
| 157 | if (!$file->hasType($this->getClass(GenerisRdf::CLASS_GENERIS_FILE))) { |
| 158 | throw new \common_exception_NotFound('Resource ' . $serial . ' is not a file'); |
| 159 | } |
| 160 | |
| 161 | $properties = []; |
| 162 | $propertiesDefinition = [ |
| 163 | $this->getProperty(GenerisRdf::PROPERTY_FILE_FILEPATH), |
| 164 | $this->getProperty(GenerisRdf::PROPERTY_FILE_FILESYSTEM), |
| 165 | $this->getProperty(GenerisRdf::PROPERTY_FILE_FILENAME), |
| 166 | ]; |
| 167 | |
| 168 | $propertiesValues = $file->getPropertiesValues($propertiesDefinition); |
| 169 | $fileSystemProperty = current($propertiesValues[GenerisRdf::PROPERTY_FILE_FILESYSTEM]); |
| 170 | $properties[self::RESOURCE_FILE_FILESYSTEM_URI] = $fileSystemProperty instanceof \core_kernel_classes_Resource |
| 171 | ? $fileSystemProperty->getUri() |
| 172 | : $fileSystemProperty->literal; |
| 173 | |
| 174 | $filePath = current($propertiesValues[GenerisRdf::PROPERTY_FILE_FILEPATH])->literal; |
| 175 | $properties[self::RESOURCE_FILE_PATH] = trim(str_replace(DIRECTORY_SEPARATOR, '/', $filePath), '/'); |
| 176 | |
| 177 | if (!empty($propertiesValues[GenerisRdf::PROPERTY_FILE_FILENAME])) { |
| 178 | $fileName = current($propertiesValues[GenerisRdf::PROPERTY_FILE_FILENAME])->literal; |
| 179 | $properties[self::RESOURCE_FILE_NAME] = ltrim(str_replace(DIRECTORY_SEPARATOR, '/', $fileName), '/'); |
| 180 | } |
| 181 | |
| 182 | return $properties; |
| 183 | } |
| 184 | } |