1 | <?php |
||
8 | class UploadFileStorageProvider extends FileStorageProvider |
||
9 | { |
||
10 | |||
11 | private $targetDirectory; |
||
12 | |||
13 | 4 | public function __construct($targetDirectory, StorageFactoryInterface $storageFactory=null) |
|
14 | { |
||
15 | 4 | parent::__construct($storageFactory); |
|
16 | |||
17 | 4 | if (!is_dir($targetDirectory) || !is_writeable($targetDirectory)) { |
|
18 | 1 | throw new \InvalidArgumentException("Targetdirectory $targetDirectory is not writable!"); |
|
19 | } |
||
20 | |||
21 | 3 | $this->targetDirectory = realpath($targetDirectory); |
|
22 | 3 | } |
|
23 | |||
24 | /** |
||
25 | * (non-PHPdoc) |
||
26 | * @see \Mathielen\ImportEngine\Storage\Provider\StorageProviderInterface::select() |
||
27 | */ |
||
28 | 2 | public function select($id = null) |
|
29 | { |
||
30 | 2 | if ($id instanceof UploadedFile) { |
|
31 | 2 | if (!$id->isValid()) { |
|
32 | 1 | throw new \InvalidArgumentException("Upload was not successful"); |
|
33 | } |
||
34 | |||
35 | 1 | $newFile = $id->move($this->targetDirectory, $this->generateTargetFilename($id)); |
|
36 | |||
37 | 1 | $selection = new StorageSelection( |
|
38 | 1 | new \SplFileInfo($newFile), |
|
39 | 1 | $this->targetDirectory. '/' . $newFile->getFilename(), |
|
40 | 1 | $id->getClientOriginalName()); |
|
41 | |||
42 | 1 | return $selection; |
|
43 | } |
||
44 | |||
45 | return parent::select($id); |
||
46 | } |
||
47 | |||
48 | 1 | private function generateTargetFilename(UploadedFile $file) |
|
52 | |||
53 | } |
||
54 |