|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Silverback API Component Bundle Project |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Daniel West <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentBundle\Helper; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
17
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
18
|
|
|
use Silverback\ApiComponentBundle\Annotation\File; |
|
19
|
|
|
use Silverback\ApiComponentBundle\Exception\InvalidArgumentException; |
|
20
|
|
|
use Silverback\ApiComponentBundle\Utility\ClassMetadataTrait; |
|
21
|
|
|
use Symfony\Component\HttpFoundation\FileBag; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @author Daniel West <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
final class FileHelper extends AbstractHelper |
|
27
|
|
|
{ |
|
28
|
|
|
use ClassMetadataTrait; |
|
29
|
|
|
|
|
30
|
9 |
|
public function __construct(Reader $reader, ManagerRegistry $registry) |
|
31
|
|
|
{ |
|
32
|
9 |
|
$this->reader = $reader; |
|
33
|
9 |
|
$this->initRegistry($registry); |
|
34
|
9 |
|
$this->initReader($reader); |
|
35
|
9 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param object|string $class |
|
39
|
|
|
*/ |
|
40
|
9 |
|
public function getConfiguration($class): File |
|
41
|
|
|
{ |
|
42
|
9 |
|
return $this->getAnnotationConfiguration($class, File::class); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function uploadFile(object $resource, FileBag $fileBag) |
|
46
|
|
|
{ |
|
47
|
|
|
if (!$this->isConfigured($resource)) { |
|
48
|
|
|
throw new InvalidArgumentException('%s is not configured as a File'); |
|
49
|
|
|
} |
|
50
|
|
|
$configuration = $this->getConfiguration($resource); |
|
51
|
|
|
$classMetadata = $this->getClassMetadata($resource); |
|
52
|
|
|
$uploadedFile = $fileBag->get($configuration->fileFieldName); |
|
53
|
|
|
$classMetadata->setFieldValue($resource, $configuration->filePathFieldName, '/uploaded_file_path'); |
|
54
|
|
|
|
|
55
|
|
|
return $resource; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|