Completed
Push — feature/EVO-8294-fileUpload ( 50faa4...37e7e8 )
by Narcotic
61:38
created

FileController::setRequestManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * controller for gaufrette based file store
4
 */
5
6
namespace Graviton\FileBundle\Controller;
7
8
use Graviton\ExceptionBundle\Exception\MalformedInputException;
9
use Graviton\FileBundle\Manager\FileManager;
10
use Graviton\FileBundle\Manager\RequestManager;
11
use Graviton\RestBundle\Controller\RestController;
12
use GravitonDyn\FileBundle\Document\File;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
16
/**
17
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
18
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
19
 * @link     http://swisscom.ch
20
 */
21
class FileController extends RestController
22
{
23
    /**
24
     * @var FileManager
25
     */
26
    private $fileManager;
27
28
    /**
29
     * @var RequestManager
30
     */
31
    private $requestManager;
32
33
    /**
34
     * On build time we inject the Manager
35
     *
36
     * @param FileManager $fileManager Service Manager
37
     *
38
     * @return void
39
     */
40
    public function setFileManager(FileManager $fileManager)
41
    {
42
        $this->fileManager = $fileManager;
43
    }
44
45
    /**
46
     * set RequestManager
47
     *
48
     * @param RequestManager $requestManager requestManager
49
     *
50
     * @return void
51
     */
52
    public function setRequestManager($requestManager)
53
    {
54
        $this->requestManager = $requestManager;
55
    }
56
57
    /**
58
     * Writes a new Entry to the database
59
     * Can accept either direct Post data or Form upload
60
     *
61
     * @param Request $request Current http request
62
     *
63
     * @return Response $response Result of action with data (if successful)
64
     */
65
    public function postAction(Request $request)
66
    {
67
        $file = new File();
68
        if ($formData = $request->get('metadata')) {
69
            $file = $this->validateRequest($formData, $this->getModel());
70
        }
71
72
        $request = $this->requestManager->updateFileRequest($request);
73
74
        $file = $this->fileManager->handleSaveRequest($file, $request, $this->getModel());
0 ignored issues
show
Documentation introduced by
$this->getModel() is of type object<Graviton\RestBundle\Model\DocumentModel>, but the function expects a object<GravitonDyn\FileBundle\Model\File>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
76
        // Set status code and content
77
        $response = $this->getResponse();
78
        $response->setStatusCode(Response::HTTP_CREATED);
79
        $response->headers->set(
80
            'Location',
81
            $this->getRouter()->generate('gravitondyn.file.rest.file.get', array('id' => $file->getId()))
82
        );
83
        return $response;
84
    }
85
86
    /**
87
     * respond with document if non json mime-type is requested
88
     *
89
     * @param Request $request Current http request
90
     * @param string  $id      id of file
91
     *
92
     * @return Response
93
     */
94
    public function getAction(Request $request, $id)
95
    {
96
        // If a json request, let parent handle it
97
        if (in_array('application/json', $request->getAcceptableContentTypes())) {
98
            return parent::getAction($request, $id);
99
        }
100
101
        /** @var File $file */
102
        $file = $this->findRecord($id);
103
104
        /** @var Response $response */
105
        return $this->fileManager->buildGetContentResponse(
106
            $this->getResponse(),
107
            $file
108
        );
109
    }
110
111
    /**
112
     * Update a record
113
     *
114
     * @param Number  $id      ID of record
115
     * @param Request $request Current http request
116
     *
117
     * @return Response $response Result of action with data (if successful)
118
     */
119
    public function putAction($id, Request $request)
120
    {
121
        // If a json request, let parent handle it
122
        if ('application/json' == $request->getContentType()) {
123
            return parent::putAction($id, $request);
124
        }
125
126
        $request = $this->requestManager->updateFileRequest($request);
127
128
        $file = new File();
129
        if ($metadata = $request->get('metadata', false)) {
130
            $file = $this->validateRequest($metadata, $this->getModel());
131
        }
132
133
        $file = $this->fileManager->handleSaveRequest($file, $request, $this->getModel());
0 ignored issues
show
Documentation introduced by
$this->getModel() is of type object<Graviton\RestBundle\Model\DocumentModel>, but the function expects a object<GravitonDyn\FileBundle\Model\File>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
134
135
        // Set status code and content
136
        $response = $this->getResponse();
137
        $response->setStatusCode(Response::HTTP_NO_CONTENT);
138
        $response->headers->set(
139
            'Location',
140
            $this->getRouter()->generate('gravitondyn.file.rest.file.get', array('id' => $file->getId()))
141
        );
142
143
        return $response;
144
    }
145
146
    /**
147
     * Deletes a record
148
     *
149
     * @param Number $id ID of record
150
     *
151
     * @return Response $response Result of the action
152
     */
153
    public function deleteAction($id)
154
    {
155
        $response = parent::deleteAction($id);
156
        $this->fileManager->remove($id);
157
        return $response;
158
    }
159
160
    /**
161
     * Patch a record, we add here a patch on Modification Data.
162
     *
163
     * @param Number  $id      ID of record
164
     * @param Request $request Current http request
165
     *
166
     * @throws MalformedInputException
167
     *
168
     * @return Response $response Result of action with data (if successful)
169
     */
170
    public function patchAction($id, Request $request)
171
    {
172
        // Update modified date
173
        $content = json_decode($request->getContent(), true);
174
        if ($content) {
175
            $now = new \DateTime();
176
            $patch = [
177
                'op' => 'replace',
178
                'path' => '/metadata/modificationDate',
179
                'value' => $now->format(DATE_ISO8601)
180
            ];
181
            // It can be a simple patch or a multi array patching.
182
            if (array_key_exists(0, $content)) {
183
                $content[] = $patch;
184
            } else {
185
                $content = [$content, $patch];
186
            }
187
188
            $request = new Request(
189
                $request->query->all(),
190
                $request->request->all(),
191
                $request->attributes->all(),
192
                $request->cookies->all(),
193
                $request->files->all(),
194
                $request->server->all(),
195
                json_encode($content)
196
            );
197
        }
198
199
        return parent::patchAction($id, $request);
200
    }
201
}
202