Completed
Push — master ( 48ff0c...a157cb )
by wen
03:02
created

UploadController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 28
c 1
b 0
f 0
wmc 5
lcom 0
cbo 6
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B formElement() 0 25 5
1
<?php
2
3
namespace Sco\Admin\Http\Controllers;
4
5
use App\Http\Controllers\Controller;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\UploadedFile;
8
use Sco\Admin\Contracts\ComponentInterface;
9
use Sco\Admin\Exceptions\InvalidArgumentException;
10
use Sco\Admin\Form\Elements\File;
11
12
class UploadController extends Controller
13
{
14
    public function formElement(Request $request, ComponentInterface $component, $field, $id = null)
15
    {
16
        $file = $request->file($field);
17
        if (is_null($file) || !($file instanceof UploadedFile)) {
18
            throw new InvalidArgumentException('must upload file');
19
        }
20
        if (is_null($id)) {
21
            $form = $component->fireCreate();
22
        } else {
23
            $form = $component->fireEdit($id);
24
        }
25
26
        $element = $form->getElement($field);
27
        if (!($element instanceof File)) {
28
            throw new InvalidArgumentException(
29
                sprintf(
30
                    '[%s] element must be instanced of "Sco\Admin\Form\Elements\File".',
31
                    $field
32
                )
33
            );
34
        }
35
36
        $data = $element->saveFile($file);
37
        return response()->json($data);
38
    }
39
}
40