Completed
Push — feature/EVO-8294-fileUpload ( f76403 )
by
unknown
65:32
created

FileController::putAction()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 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
     * On build time we inject the Manager
30
     *
31
     * @param FileManager $fileManager Service Manager
32
     *
33
     * @return void
34
     */
35
    public function setFileManager(FileManager $fileManager)
36
    {
37
        $this->fileManager = $fileManager;
38
    }
39
40
    /**
41
     * Writes a new Entry to the database
42
     * Can accept either direct Post data or Form upload
43
     *
44
     * @param Request $request Current http request
45
     *
46
     * @return Response $response Result of action with data (if successful)
47
     */
48
    public function postAction(Request $request)
49
    {
50
        $file = new File();
51
        if ($formData = $request->get('metadata')) {
52
            $file = $this->validateRequest($formData, $this->getModel());
53
        }
54
55
        /** @var RequestManager $requestManager */
56
        // $requestManager = $this->getContainer()->get('graviton.file.request_manager');
57
        // TODO handle raw upload $request = $requestManager->updateFileRequest($request);
58
59
        $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...
60
61
        // Set status code and content
62
        $response = $this->getResponse();
63
        $response->setStatusCode(Response::HTTP_CREATED);
64
        $response->headers->set(
65
            'Location',
66
            $this->getRouter()->generate('gravitondyn.file.rest.file.get', array('id' => $file->getId()))
67
        );
68
        return $response;
69
    }
70
71
    /**
72
     * respond with document if non json mime-type is requested
73
     *
74
     * @param Request $request Current http request
75
     * @param string  $id      id of file
76
     *
77
     * @return Response
78
     */
79
    public function getAction(Request $request, $id)
80
    {
81
        // If a json request, let parent handle it
82
        if (in_array('application/json', $request->getAcceptableContentTypes())) {
83
            return parent::getAction($request, $id);
84
        }
85
86
        /** @var File $file */
87
        $file = $this->findRecord($id);
88
89
        /** @var Response $response */
90
        return $this->fileManager->buildGetContentResponse(
91
            $this->getResponse(),
92
            $file
93
        );
94
    }
95
96
    /**
97
     * Update a record
98
     *
99
     * @param Number  $id      ID of record
100
     * @param Request $request Current http request
101
     *
102
     * @return Response $response Result of action with data (if successful)
103
     */
104
    public function putAction($id, Request $request)
105
    {
106
        // If a json request, let parent handle it
107
        if (in_array('application/json', $request->getAcceptableContentTypes())) {
108
            return parent::putAction($id, $request);
109
        }
110
111
        /** @var RequestManager $requestManager */
112
        $requestManager = $this->getContainer()->get('graviton.file.request_manager');
113
        $request = $requestManager->updateFileRequest($request);
114
115
        $file = new File();
116
        if ($metadata = $request->get('metadata', false)) {
117
            $file = $this->validateRequest($metadata, $this->getModel());
118
        }
119
120
        $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...
121
122
        // Set status code and content
123
        $response = $this->getResponse();
124
        $response->setStatusCode(Response::HTTP_NO_CONTENT);
125
        $response->headers->set(
126
            'Location',
127
            $this->getRouter()->generate('gravitondyn.file.rest.file.get', array('id' => $file->getId()))
128
        );
129
130
        return $response;
131
    }
132
133
    /**
134
     * Deletes a record
135
     *
136
     * @param Number $id ID of record
137
     *
138
     * @return Response $response Result of the action
139
     */
140
    public function deleteAction($id)
141
    {
142
        $response = parent::deleteAction($id);
143
        $this->fileManager->remove($id);
144
        return $response;
145
    }
146
147
    /**
148
     * Patch a record, we add here a patch on Modification Data.
149
     *
150
     * @param Number  $id      ID of record
151
     * @param Request $request Current http request
152
     *
153
     * @throws MalformedInputException
154
     *
155
     * @return Response $response Result of action with data (if successful)
156
     */
157
    public function patchAction($id, Request $request)
158
    {
159
        // Update modified date
160
        $content = json_decode($request->getContent(), true);
161
        if ($content) {
162
            $now = new \DateTime();
163
            $patch = [
164
                'op' => 'replace',
165
                'path' => '/metadata/modificationDate',
166
                'value' => $now->format(DATE_ISO8601)
167
            ];
168
            // It can be a simple patch or a multi array patching.
169
            if (array_key_exists(0, $content)) {
170
                $content[] = $patch;
171
            } else {
172
                $content = [$content, $patch];
173
            }
174
175
            $request = new Request(
176
                $request->query->all(),
177
                $request->request->all(),
178
                $request->attributes->all(),
179
                $request->cookies->all(),
180
                $request->files->all(),
181
                $request->server->all(),
182
                json_encode($content)
183
            );
184
        }
185
186
        return parent::patchAction($id, $request);
187
    }
188
}
189