GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b864c9...ed9166 )
by butschster
12:41
created

UploadController::fromField()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 53
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 53
c 0
b 0
f 0
cc 7
eloc 32
nc 8
nop 4
ccs 0
cts 39
cp 0
crap 56
rs 7.5251

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SleepingOwl\Admin\Http\Controllers;
4
5
use Validator;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\JsonResponse;
8
use Illuminate\Routing\Controller;
9
use SleepingOwl\Admin\Form\Element\File;
10
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface;
11
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12
13
class UploadController extends Controller
14
{
15
    /**
16
     * @param Request $request
17
     * @param ModelConfigurationInterface $model
18
     * @param string $field
19
     * @param int|null $id
20
     *
21
     * @return JsonResponse
22
     */
23
    public function fromField(Request $request, ModelConfigurationInterface $model, $field, $id = null)
24
    {
25
        if (! is_null($id)) {
26
            $item = $model->getRepository()->find($id);
27
            if (is_null($item) || ! $model->isEditable($item)) {
28
                return new JsonResponse([
29
                    'message' => trans('lang.message.access_denied'),
30
                ], 403);
31
            }
32
33
            $form = $model->fireEdit($id);
34
        } else {
35
            if (! $model->isCreatable()) {
36
                return new JsonResponse([
37
                    'message' => trans('lang.message.access_denied'),
38
                ], 403);
39
            }
40
41
            $form = $model->fireCreate();
42
        }
43
44
        /** @var File $element */
45
        if (is_null($element = $form->getElement($field))) {
46
            throw new NotFoundHttpException("Field [{$field}] not found");
47
        }
48
49
        $rules = $element->getUploadValidationRules();
50
        $messages = $element->getUploadValidationMessages();
51
        $labels = $element->getUploadValidationLabels();
52
53
        /** @var \Illuminate\Contracts\Validation\Validator $validator */
54
        $validator = Validator::make($request->all(), $rules, $messages, $labels);
55
56
        $element->customValidation($validator);
57
58
        if ($validator->fails()) {
59
            return new JsonResponse([
60
                'message' => trans('lang.message.validation_error'),
61
                'errors'  => $validator->errors()->get('file'),
62
            ], 400);
63
        }
64
65
        $file = $request->file('file');
66
67
        $filename = $element->getUploadFileName($file);
68
        $path = $element->getUploadPath($file);
69
        $settings = $element->getUploadSettings();
70
71
        $result = $element->saveFile($file, public_path($path), $filename, $settings);
72
73
        /* When driver not file */
74
        return new JsonResponse($result);
75
    }
76
}
77