Completed
Push — master ( 3c7450...96f371 )
by Song
05:12
created

Image::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Form\Field;
4
5
use Intervention\Image\ImageManagerStatic;
6
use Symfony\Component\HttpFoundation\File\UploadedFile;
7
8
class Image extends File
9
{
10
    protected $rules = 'image';
11
12
    protected $calls = [];
13
14
    public function defaultStorePath()
15
    {
16
        return config('admin.upload.directory.image');
17
    }
18
19
    public function prepare(UploadedFile $image = null)
20
    {
21
        if (is_null($image)) {
22
            if ($this->isDeleteRequest()) {
23
                return '';
24
            }
25
26
            return $this->original;
27
        }
28
29
        $this->directory = $this->directory ?: $this->defaultStorePath();
30
31
        $this->name = $this->name ?: $image->getClientOriginalName();
32
33
        $target = $this->uploadAndDeleteOriginal($image);
34
35
        $target = $this->executeCalls($target);
36
37
        return $target;
38
    }
39
40
    /**
41
     * @param $target
42
     *
43
     * @return mixed
44
     */
45
    public function executeCalls($target)
46
    {
47
        if (!empty($this->calls)) {
48
            $image = ImageManagerStatic::make($target);
49
50
            foreach ($this->calls as $call) {
51
                call_user_func_array([$image, $call['method']], $call['arguments'])->save($target);
52
            }
53
        }
54
55
        return $target;
56
    }
57
58
    protected function preview()
59
    {
60
        return '<img src="'.$this->objectUrl($this->value).'" class="file-preview-image">';
61
    }
62
63
    public function render()
64
    {
65
        $this->options(['allowedFileTypes' => ['image']]);
66
67
        return parent::render();
68
    }
69
70
    public function __call($method, $arguments)
71
    {
72
        $this->calls[] = [
73
            'method'    => $method,
74
            'arguments' => $arguments,
75
        ];
76
77
        return $this;
78
    }
79
}
80