Completed
Push — master ( 76446a...8cb42d )
by
unknown
14:07
created

FileController::putAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 21
loc 21
ccs 0
cts 16
cp 0
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 2
crap 6
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 View Code Duplication
    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 View Code Duplication
    public function putAction($id, Request $request)
120
    {
121
        $request = $this->requestManager->updateFileRequest($request);
122
123
        $file = new File();
124
        if ($metadata = $request->get('metadata', false)) {
125
            $file = $this->validateRequest($metadata, $this->getModel());
126
        }
127
128
        $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...
129
130
        // Set status code and content
131
        $response = $this->getResponse();
132
        $response->setStatusCode(Response::HTTP_NO_CONTENT);
133
        $response->headers->set(
134
            'Location',
135
            $this->getRouter()->generate('gravitondyn.file.rest.file.get', array('id' => $file->getId()))
136
        );
137
138
        return $response;
139
    }
140
141
    /**
142
     * Deletes a record
143
     *
144
     * @param Number $id ID of record
145
     *
146
     * @return Response $response Result of the action
147
     */
148
    public function deleteAction($id)
149
    {
150
        $response = parent::deleteAction($id);
151
        $this->fileManager->remove($id);
152
        return $response;
153
    }
154
155
    /**
156
     * Patch a record, we add here a patch on Modification Data.
157
     *
158
     * @param Number  $id      ID of record
159
     * @param Request $request Current http request
160
     *
161
     * @throws MalformedInputException
162
     *
163
     * @return Response $response Result of action with data (if successful)
164
     */
165
    public function patchAction($id, Request $request)
166
    {
167
        // Update modified date
168
        $content = json_decode($request->getContent(), true);
169
        if ($content) {
170
            $now = new \DateTime();
171
            $patch = [
172
                'op' => 'replace',
173
                'path' => '/metadata/modificationDate',
174
                'value' => $now->format(DATE_ISO8601)
175
            ];
176
            // It can be a simple patch or a multi array patching.
177
            if (array_key_exists(0, $content)) {
178
                $content[] = $patch;
179
            } else {
180
                $content = [$content, $patch];
181
            }
182
183
            $request = new Request(
184
                $request->query->all(),
185
                $request->request->all(),
186
                $request->attributes->all(),
187
                $request->cookies->all(),
188
                $request->files->all(),
189
                $request->server->all(),
190
                json_encode($content)
191
            );
192
        }
193
194
        return parent::patchAction($id, $request);
195
    }
196
}
197