-
Notifications
You must be signed in to change notification settings - Fork 17
/
AbstractUploadProcessor.php
168 lines (150 loc) · 4.38 KB
/
AbstractUploadProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace SRIO\RestUploadBundle\Processor;
use SRIO\RestUploadBundle\Exception\UploadException;
use SRIO\RestUploadBundle\Exception\UploadProcessorException;
use SRIO\RestUploadBundle\Model\UploadableFileInterface;
use SRIO\RestUploadBundle\Request\RequestContentHandler;
use SRIO\RestUploadBundle\Request\RequestContentHandlerInterface;
use SRIO\RestUploadBundle\Upload\StorageHandler;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
abstract class AbstractUploadProcessor implements ProcessorInterface
{
/**
* @var FormInterface
*/
protected $form;
/**
* @var array
*/
protected $config;
/**
* @var RequestContentHandler
*/
protected $contentHandler = null;
/**
* @var \SRIO\RestUploadBundle\Upload\StorageHandler
*/
protected $storageHandler;
/**
* Constructor.
*
* @param StorageHandler $storageHandler
*/
public function __construct(StorageHandler $storageHandler)
{
$this->storageHandler = $storageHandler;
}
/**
* Constructor.
*
* @param Request $request
* @param FormInterface $form
* @param array $config
*
* @return bool
*/
public function handleUpload(Request $request, FormInterface $form = null, array $config = array())
{
$this->form = $form;
$this->config = $config;
return $this->handleRequest($request);
}
/**
* Handle an upload request.
*
* This method return a Response object that will be sent back
* to the client or will be caught by controller.
*
* @param Request $request
*
* @return \SRIO\RestUploadBundle\Upload\UploadResult
*/
abstract public function handleRequest(Request $request);
/**
* Create the form data that the form will be able to handle.
*
* It walk one the form and make an intersection between its keys and
* provided data.
*
* @param array $data
*
* @return array
*/
protected function createFormData(array $data)
{
$keys = $this->getFormKeys($this->form);
return array_intersect_key($data, $keys);
}
/**
* Get keys of the form.
*
* @param FormInterface $form
*
* @return array
*/
protected function getFormKeys(FormInterface $form)
{
$keys = array();
foreach ($form->all() as $child) {
$keys[$child->getName()] = count($child->all()) > 0 ? $this->getFormKeys($child) : null;
}
return $keys;
}
/**
* Get a request content handler.
*
* @param Request $request
*
* @return RequestContentHandlerInterface
*/
protected function getRequestContentHandler(Request $request)
{
if ($this->contentHandler === null) {
$this->contentHandler = new RequestContentHandler($request);
}
return $this->contentHandler;
}
/**
* Check that needed headers are here.
*
* @param Request $request the request
* @param array $headers the headers to check
*
* @throws \SRIO\RestUploadBundle\Exception\UploadException
*/
protected function checkHeaders(Request $request, array $headers)
{
foreach ($headers as $header) {
$value = $request->headers->get($header, null);
if ($value === null) {
throw new UploadException(sprintf('%s header is needed', $header));
} elseif (!is_int($value) && empty($value) && $value !== '0') {
throw new UploadException(sprintf('%s header must not be empty', $header));
}
}
}
/**
* Set the uploaded file on the form data.
*
* @param UploadedFile $file
*
* @throws \SRIO\RestUploadBundle\Exception\UploadProcessorException
*
* @deprecated
*/
protected function setUploadedFile(UploadedFile $file)
{
$data = $this->form->getData();
if ($data instanceof UploadableFileInterface) {
$data->setFile($file);
} else {
throw new UploadProcessorException(sprintf(
'Unable to set file, %s do not implements %s',
get_class($data),
'SRIO\RestUploadBundle\Model\UploadableFileInterface'
));
}
}
}