UploadController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 7
dl 0
loc 73
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B upload() 0 23 4
1
<?php
2
3
namespace App\Controller;
4
5
use App\Model\MediaFile;
6
use Slim\Http\Request;
7
use Slim\Http\Response;
8
use App\Common\JsonException;
9
10
class UploadController extends BaseController
11
{
12
    /**
13
     * @api {post} /upload Upload media
14
     * @apiName PostUpload
15
     * @apiGroup Upload
16
     *
17
     * @apiPermission user
18
     * @apiHeader {String} Authorization Bearer TOKEN
19
     * @apiUse UnauthorizedError
20
     *
21
     * @apiDescription Метод для загрузки фотографий. Загружать form-data, где ключ - "image", а значение - файл.
22
     *
23
     * @apiParam {File} image Загружаемый файл
24
     *
25
     * @apiParamExample {json} Example request:
26
     *    {
27
     *      "image": "/path/to/image.jpg"
28
     *    }
29
     *
30
     * @apiSuccessExample {json} Success (200)
31
     *     HTTP/1.1 200 OK
32
     *     {
33
     *       "data": {
34
     *         "type": "media-file",
35
     *         "id": "2",
36
     *         "attributes": {
37
     *           "file": "bUinMEAa0UBNeJBOVDcUHZckHpor3a74.jpg",
38
     *           "file_info": {
39
     *             "mime": "image/jpeg",
40
     *             "size": 133807
41
     *           }
42
     *         },
43
     *         "links": {
44
     *           "self": "http://london.dev/api/media-file/2"
45
     *         }
46
     *       }
47
     *     }
48
     *
49
     * @apiUse StandardErrors
50
     */
51
    /**
52
     * @param Request  $request
53
     * @param Response $response
54
     * @param array    $args
55
     *
56
     * @return \Psr\Http\Message\ResponseInterface
57
     * @throws JsonException
58
     */
59
    public function upload(Request $request, Response $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
    {
61
        $files = $request->getUploadedFiles();
62
        if (!isset($files['image'])) {
63
            throw new JsonException('media-file', 400, 'Bad request', 'Not found file');
64
        }
65
66
        /** @var \Slim\Http\UploadedFile $file */
67
        $file = $files['image'];
68
        if ($file->getError() !== UPLOAD_ERR_OK) {
69
            throw new JsonException('media-file', 500, 'Internal server error', 'Internal server error');
70
        }
71
72
        try {
73
            $mediaFile = MediaFile::create($file, $this->settings['params']['uploadDir']);
74
        } catch (\Exception $e) {
75
            throw new JsonException('media-file', 500, 'Internal server error', $e->getMessage());
76
        }
77
78
        $result = $this->encoder->encode($request, $mediaFile);
79
80
        return $this->apiRenderer->jsonResponse($response, 200, $result);
81
    }
82
}
83