Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
QtiFlysystemFileManager
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 7
90
0.00% covered (danger)
0.00%
0 / 1
 setFilePrefix
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 createFromFile
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 createFromData
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 retrieve
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 delete
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFileSystem
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 generateId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
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
22namespace oat\taoQtiTest\models\files;
23
24use qtism\common\datatypes\QtiFile;
25use qtism\common\datatypes\files\FileManager;
26use oat\oatbox\service\ConfigurableService;
27use oat\oatbox\filesystem\FileSystemService;
28
29class QtiFlysystemFileManager extends ConfigurableService implements FileManager
30{
31    public const SERVICE_ID = 'taoQtiTest/qtiFilesystem';
32    private $filePrefix = '';
33
34    public function setFilePrefix($filePrefix)
35    {
36        $this->filePrefix = $filePrefix;
37    }
38
39    public function createFromFile($path, $mimeType, $filename = '')
40    {
41        $id = $this->generateId();
42        $this->getFileSystem()->write($id, file_get_contents($path));
43
44        if (empty($filename) === false) {
45            $this->getFileSystem()->write($id . QtiFlysystemFile::FILENAME_MD_PREFIX, $filename);
46        }
47
48        $file = new QtiFlysystemFile('taoQtiTestSessionFilesystem', $id);
49        $file->setServiceLocator($this->getServiceLocator());
50
51        return $file;
52    }
53
54    public function createFromData($data, $mimeType, $filename = '')
55    {
56        $id = $this->generateId();
57        $this->getFileSystem()->write($id, $data);
58
59        if (empty($filename) === false) {
60            $this->getFileSystem()->write($id . QtiFlysystemFile::FILENAME_MD_PREFIX, $filename);
61        }
62
63        $file = new QtiFlysystemFile('taoQtiTestSessionFilesystem', $id);
64        $file->setServiceLocator($this->getServiceLocator());
65
66        return $file;
67    }
68
69    public function retrieve($identifier)
70    {
71        $file = new QtiFlysystemFile('taoQtiTestSessionFilesystem', $identifier);
72        $file->setServiceLocator($this->getServiceLocator());
73
74        return $file;
75    }
76
77    public function delete(QtiFile $file)
78    {
79        $file->delete();
80    }
81
82    private function getFileSystem()
83    {
84        return $this
85            ->getServiceLocator()
86            ->get(FileSystemService::SERVICE_ID)
87            ->getFileSystem('taoQtiTestSessionFilesystem');
88    }
89
90    private function generateId()
91    {
92        return $this->filePrefix . uniqid() . mt_rand(0, 1000000);
93    }
94}