Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
63.16% |
24 / 38 |
|
37.50% |
3 / 8 |
CRAP | |
0.00% |
0 / 1 |
| UrlFileSerializer | |
63.16% |
24 / 38 |
|
37.50% |
3 / 8 |
28.80 | |
0.00% |
0 / 1 |
| serialize | |
38.46% |
5 / 13 |
|
0.00% |
0 / 1 |
5.10 | |||
| unserialize | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |||
| unserializeFile | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| unserializeDirectory | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| cleanSerial | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
4.37 | |||
| extract | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getRootDirectory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| cleanUp | |
0.00% |
0 / 1 |
|
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) 2016 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\generis\model\fileReference; |
| 23 | |
| 24 | use oat\oatbox\filesystem\Directory; |
| 25 | use oat\oatbox\filesystem\File; |
| 26 | use oat\oatbox\filesystem\FileSystemService; |
| 27 | use oat\oatbox\service\ConfigurableService; |
| 28 | |
| 29 | class UrlFileSerializer extends ConfigurableService implements FileReferenceSerializer |
| 30 | { |
| 31 | /** |
| 32 | * {@inheritDoc} |
| 33 | * @see FileReferenceSerializer::serialize() |
| 34 | */ |
| 35 | public function serialize($abstraction) |
| 36 | { |
| 37 | if ($abstraction instanceof File) { |
| 38 | $baseDir = $this->getRootDirectory($abstraction->getFileSystemId()); |
| 39 | |
| 40 | return 'file://' . urlencode($abstraction->getFileSystemId()) . '/' . urlencode( |
| 41 | $baseDir->getRelPath($abstraction) |
| 42 | ); |
| 43 | } elseif ($abstraction instanceof Directory) { |
| 44 | $baseDir = $this->getRootDirectory($abstraction->getFileSystemId()); |
| 45 | |
| 46 | return 'dir://' . urlencode($abstraction->getFileSystemId()) . '/' . urlencode( |
| 47 | $baseDir->getRelPath($abstraction) |
| 48 | ); |
| 49 | } else { |
| 50 | throw new FileSerializerException( |
| 51 | __CLASS__ . '::' . __FUNCTION__ . ' expects parameter to be an instance of Directory or File' |
| 52 | ); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * {@inheritDoc} |
| 58 | * @see FileReferenceSerializer::unserialize() |
| 59 | */ |
| 60 | public function unserialize($serial) |
| 61 | { |
| 62 | $serial = $this->cleanSerial($serial); |
| 63 | $type = substr($serial, 0, strpos($serial, ':')); |
| 64 | if ($type == 'file') { |
| 65 | return $this->unserializeFile($serial); |
| 66 | } elseif ($type == 'dir') { |
| 67 | return $this->unserializeDirectory($serial); |
| 68 | } else { |
| 69 | throw new FileSerializerException('Unsupported type "' . $type . '" in ' . __CLASS__); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * {@inheritDoc} |
| 75 | * @see FileReferenceSerializer::unserializeFile() |
| 76 | */ |
| 77 | public function unserializeFile($serial) |
| 78 | { |
| 79 | $parts = $this->extract($serial); |
| 80 | |
| 81 | return $this->getRootDirectory(urldecode($parts['fs']))->getFile(urldecode($parts['path'])); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * {@inheritDoc} |
| 86 | * @see FileReferenceSerializer::unserializeDirectory() |
| 87 | */ |
| 88 | public function unserializeDirectory($serial) |
| 89 | { |
| 90 | $parts = $this->extract($serial); |
| 91 | |
| 92 | return $this->getRootDirectory(urldecode($parts['fs']))->getDirectory(urldecode($parts['path'])); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Ensure serial is a string |
| 97 | * |
| 98 | * @param string $serial |
| 99 | * @throws FileSerializerException |
| 100 | * @return string |
| 101 | */ |
| 102 | protected function cleanSerial($serial) |
| 103 | { |
| 104 | if ($serial instanceof \core_kernel_classes_Resource) { |
| 105 | $serial = $serial->getUri(); |
| 106 | } elseif ($serial instanceof \core_kernel_classes_Literal) { |
| 107 | $serial = $serial->__toString(); |
| 108 | } elseif (!is_string($serial)) { |
| 109 | throw new FileSerializerException('Unsupported serial "' . gettype($serial) . '" in ' . __CLASS__); |
| 110 | } |
| 111 | |
| 112 | return $serial; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Extract filesystem id and path from serial |
| 117 | * |
| 118 | * @param string $serial |
| 119 | * @throws FileSerializerException |
| 120 | * @return string[] |
| 121 | */ |
| 122 | protected function extract($serial) |
| 123 | { |
| 124 | $serial = $this->cleanSerial($serial); |
| 125 | $parts = explode('/', substr($serial, strpos($serial, '://') + 3), 2); |
| 126 | if (count($parts) != 2) { |
| 127 | throw new FileSerializerException('Unsupported dir in ' . __CLASS__); |
| 128 | } |
| 129 | |
| 130 | return ['fs' => $parts[0], 'path' => $parts[1]]; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Return root directory represented by the given uri |
| 135 | * |
| 136 | * @return Directory |
| 137 | */ |
| 138 | protected function getRootDirectory($id) |
| 139 | { |
| 140 | return $this->getServiceLocator()->get(FileSystemService::SERVICE_ID)->getDirectory($id); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * {@inheritDoc} |
| 145 | * @see FileReferenceSerializer::cleanUp() |
| 146 | */ |
| 147 | public function cleanUp($serial) |
| 148 | { |
| 149 | // nothing to do |
| 150 | return true; |
| 151 | } |
| 152 | } |