Completed
Push — develop ( 8852f9...b0ea9b )
by
unknown
11:33 queued 03:35
created

FileController::putAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 23
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 23
loc 23
ccs 0
cts 17
cp 0
rs 9.0856
cc 2
eloc 13
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 GravitonDyn\FileBundle\Model\File as FileModel;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
17
/**
18
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
19
 * @license  https://opensource.org/licenses/MIT MIT License
20
 * @link     http://swisscom.ch
21
 */
22
class FileController extends RestController
23
{
24
    /**
25
     * @var FileManager
26
     */
27
    private $fileManager;
28
29
    /**
30
     * @var RequestManager
31
     */
32
    private $requestManager;
33
34
    /**
35
     * On build time we inject the Manager
36
     *
37
     * @param FileManager $fileManager Service Manager
38
     *
39
     * @return void
40
     */
41
    public function setFileManager(FileManager $fileManager)
42
    {
43
        $this->fileManager = $fileManager;
44
    }
45
46
    /**
47
     * set RequestManager
48
     *
49
     * @param RequestManager $requestManager requestManager
50
     *
51
     * @return void
52
     */
53
    public function setRequestManager($requestManager)
54
    {
55
        $this->requestManager = $requestManager;
56
    }
57
58
    /**
59
     * Writes a new Entry to the database
60
     * Can accept either direct Post data or Form upload
61
     *
62
     * @param Request $request Current http request
63
     *
64
     * @return Response $response Result of action with data (if successful)
65
     */
66 View Code Duplication
    public function postAction(Request $request)
67
    {
68
        $file = new File();
69
        $request = $this->requestManager->updateFileRequest($request);
70
71
        if ($formData = $request->get('metadata')) {
72
            $file = $this->restUtils->validateRequest($formData, $this->getModel());
73
        }
74
75
        /** @var FileModel $model */
76
        $model = $this->getModel();
77
        $file = $this->fileManager->handleSaveRequest($file, $request, $model);
78
79
        // Set status code and content
80
        $response = $this->getResponse();
81
        $response->setStatusCode(Response::HTTP_CREATED);
82
        $response->headers->set(
83
            'Location',
84
            $this->getRouter()->generate('gravitondyn.file.rest.file.get', array('id' => $file->getId()))
85
        );
86
        return $response;
87
    }
88
89
    /**
90
     * respond with document if non json mime-type is requested
91
     *
92
     * @param Request $request Current http request
93
     * @param string  $id      id of file
94
     *
95
     * @return Response
96
     */
97
    public function getAction(Request $request, $id)
98
    {
99
        // If a json request, let parent handle it
100
        if (in_array('application/json', $request->getAcceptableContentTypes())) {
101
            return parent::getAction($request, $id);
102
        }
103
104
        /** @var File $file */
105
        $file = $this->getModel()->find($id);
106
107
        /** @var Response $response */
108
        return $this->fileManager->buildGetContentResponse(
109
            $this->getResponse(),
110
            $file
111
        );
112
    }
113
114
    /**
115
     * Update a record
116
     *
117
     * @param Number  $id      ID of record
118
     * @param Request $request Current http request
119
     *
120
     * @return Response $response Result of action with data (if successful)
121
     */
122 View Code Duplication
    public function putAction($id, Request $request)
123
    {
124
        $request = $this->requestManager->updateFileRequest($request);
125
        /** @var FileModel $model */
126
        $model = $this->getModel();
127
128
        $file = new File();
129
        if ($metadata = $request->get('metadata', false)) {
130
            $file = $this->restUtils->validateRequest($metadata, $model);
131
        }
132
133
        $file = $this->fileManager->handleSaveRequest($file, $request, $model);
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
            // Checking so update time is correct
176
            $now = new \DateTime();
177
            $patch = [
178
                'op' => 'replace',
179
                'path' => '/metadata/modificationDate',
180
                'value' => $now->format(DATE_ISO8601)
181
            ];
182
            // It can be a simple patch or a multi array patching.
183
            if (array_key_exists(0, $content)) {
184
                $content[] = $patch;
185
            } else {
186
                $content = [$content, $patch];
187
            }
188
189
            $request = new Request(
190
                $request->query->all(),
191
                $request->request->all(),
192
                $request->attributes->all(),
193
                $request->cookies->all(),
194
                $request->files->all(),
195
                $request->server->all(),
196
                json_encode($content)
197
            );
198
        }
199
200
        return parent::patchAction($id, $request);
201
    }
202
}
203