Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| AbstractTaskAction | |
0.00% |
0 / 23 |
|
0.00% |
0 / 4 |
42 | |
0.00% |
0 / 1 |
| saveFile | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
6 | |||
| getUniqueFilename | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getFileReferenceSerializer | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTaskClass | |
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) 2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\oatbox\task; |
| 23 | |
| 24 | use oat\generis\model\fileReference\ResourceFileSerializer; |
| 25 | use oat\oatbox\filesystem\FileSystemService; |
| 26 | use oat\oatbox\extension\AbstractAction; |
| 27 | |
| 28 | /** |
| 29 | * abstract base for extension actions |
| 30 | * |
| 31 | * @author Aleh Hutnikau <hutnikau@1pt.com> |
| 32 | * |
| 33 | * @deprecated since version 7.10.0, to be removed in 8.0. Use \oat\tao\model\taskQueue\Task\FilesystemAwareTrait |
| 34 | * instead. |
| 35 | */ |
| 36 | abstract class AbstractTaskAction extends AbstractAction |
| 37 | { |
| 38 | public const FILE_DIR = 'taskQueue'; |
| 39 | |
| 40 | /** |
| 41 | * Save and serialize file into task queue filesystem. |
| 42 | * |
| 43 | * @param string $path file path |
| 44 | * @param string $name file name |
| 45 | * @return string file reference uri |
| 46 | */ |
| 47 | protected function saveFile($path, $name) |
| 48 | { |
| 49 | $filename = $this->getUniqueFilename($name); |
| 50 | |
| 51 | /** @var \oat\oatbox\filesystem\Directory $dir */ |
| 52 | $dir = $this->getServiceManager()->get(FileSystemService::SERVICE_ID) |
| 53 | ->getDirectory(Queue::FILE_SYSTEM_ID); |
| 54 | /** @var \oat\oatbox\filesystem\FileSystem $filesystem */ |
| 55 | $filesystem = $dir->getFileSystem(); |
| 56 | |
| 57 | $stream = fopen($path, 'r+'); |
| 58 | $filesystem->writeStream($filename, $stream); |
| 59 | |
| 60 | if (is_resource($stream)) { |
| 61 | fclose($stream); |
| 62 | } else { |
| 63 | $this->logWarning( |
| 64 | sprintf( |
| 65 | 'Stream for file "%s" is not valid. It may be already closed', |
| 66 | $name |
| 67 | ) |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | $file = $dir->getFile($filename); |
| 72 | return $this->getFileReferenceSerializer()->serialize($file); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Create a new unique filename based on an existing filename |
| 77 | * |
| 78 | * @param string $fileName |
| 79 | * @return string |
| 80 | */ |
| 81 | protected function getUniqueFilename($fileName) |
| 82 | { |
| 83 | $value = uniqid(md5($fileName)); |
| 84 | $ext = pathinfo($fileName, PATHINFO_EXTENSION); |
| 85 | if (!empty($ext)) { |
| 86 | $value .= '.' . $ext; |
| 87 | } |
| 88 | return static::FILE_DIR . '/' . $value; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get serializer to persist filesystem object |
| 93 | * @return ResourceFileSerializer |
| 94 | */ |
| 95 | protected function getFileReferenceSerializer() |
| 96 | { |
| 97 | return $this->getServiceManager()->get(ResourceFileSerializer::SERVICE_ID); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @return \core_kernel_classes_Class |
| 102 | */ |
| 103 | protected static function getTaskClass() |
| 104 | { |
| 105 | return new \core_kernel_classes_Class(Task::TASK_CLASS); |
| 106 | } |
| 107 | } |