Completed
Push — dev ( 72bf0e...23aa35 )
by Yan
02:34
created

FileRequest::isBooted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lincable\Http;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\UploadedFile;
7
use Lincable\Concerns\BuildClassnames;
8
use Illuminate\Contracts\Container\Container;
9
use Symfony\Component\HttpFoundation\File\File;
10
11
abstract class FileRequest
12
{
13
    use BuildClassnames;
14
15
    /**
16
     * The uploaded file instance.
17
     *
18
     * @var \Illuminate\Http\File
19
     */
20
    protected $file;
21
22
    /**
23
     * The request instance.
24
     *
25
     * @var \Illuminate\Http\Request
26
     */
27
    protected $request;
28
29
    /**
30
     * Determine wheter has been booted with a request.
31
     *
32
     * @var bool
33
     */
34
    protected $booted = false;
35
36
    /**
37
     * The directory where the uploaded file will be temporary moved.
38
     *
39
     * @var string
40
     */
41
    protected $tempDirectory = '/tmp';
42
43
    /**
44
     * Rules to validate the file on request.
45
     *
46
     * @param  \Illuminate\Http\UploadedFile $file
47
     * @return mixed
48
     */
49
    abstract protected function rules(UploadedFile $file);
50
51
    /**
52
     * Boot the instance with the request.
53
     *
54
     * @param  \Illuminate\Http\Request $request
55
     * @return void
56
     */
57
    public function boot(Request $request)
58
    {
59
        $this->request = $request;
60
61
        $this->guardFile($request->file($this->getParameter()));
62
63
        $this->booted = true;
64
    }
65
66
    /**
67
     * Return wheter the file request is booted.
68
     *
69
     * If is booted that means the file has been validated and
70
     * a request instance is available on instance.
71
     *
72
     * @return bool
73
     */
74
    public function isBooted()
75
    {
76
        return $this->booted;
77
    }
78
79
    /**
80
     * Return the file on request.
81
     *
82
     * @return \Illuminate\Http\UploadedFile
83
     */
84
    public function getFile()
85
    {
86
        return $this->file;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->file returns the type Illuminate\Http\File which is incompatible with the documented return type Illuminate\Http\UploadedFile.
Loading history...
87
    }
88
89
    /**
90
     * Return the current request instance.
91
     *
92
     * @return \Illuminate\Http\Request
93
     */
94
    public function getRequest()
95
    {
96
        return $this->request;
97
    }
98
99
    /**
100
     * Prepared the file to send.
101
     *
102
     * @param  \Illuminate\Contracts\Container\Container $app
103
     * @return \Symfony\Component\HttpFoundation\File\File
104
     */
105
    public function prepareFile(Container $app)
106
    {
107
        $file = $this->moveFileToTempDirectory();
108
109
        $this->executeFileEvents($app, $file);
110
111
        return $file;
112
    }
113
114
    /**
115
     * Return the file parameter on request.
116
     *
117
     * @return string
118
     */
119
    protected function getParameter()
120
    {
121
        $className = static::class;
122
123
        return $this->nameFromClass($className);
124
    }
125
126
    /**
127
     * Guard the file through validations and then set
128
     * the file on class instance.
129
     *
130
     * @param  \Illuminate\Http\UploadedFile $file
131
     * @return void
132
     */
133
    protected function guardFile(UploadedFile $file)
134
    {
135
        $validationRules = $this->parseValidationRules($file);
136
137
        // Validate the request file from rules.
138
        $this->request->validate($validationRules);
139
140
        $this->file = $file;
0 ignored issues
show
Documentation Bug introduced by
It seems like $file of type Illuminate\Http\UploadedFile is incompatible with the declared type Illuminate\Http\File of property $file.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
141
    }
142
143
    /**
144
     * Get the rules for the file validation.
145
     *
146
     * @param  \Illuminate\Http\UploadedFile $file
147
     * @return array
148
     */
149
    protected function parseValidationRules(UploadedFile $file)
150
    {
151
        return [$this->getParameter() => $this->rules($file)];
152
    }
153
154
    /**
155
     * Move the file to a temporary destination.
156
     *
157
     * @return \Symfony\Component\HttpFoundation\File\File
158
     */
159
    protected function moveFileToTempDirectory()
160
    {
161
        $destination = $this->file->hashName($this->tempDirectory);
162
163
        return $this->file->move($destination);
164
    }
165
166
    /**
167
     * Execute some generic event methods on class if available.
168
     *
169
     * Here the file can be changed, optimized, etc...
170
     *
171
     * @param  \Illuminate\Contracts\Container\Container $app
172
     * @param  \Symfony\Component\HttpFoundation\File\File $file
173
     * @return void
174
     */
175
    protected function executeFileEvents(Container $app, File $file)
176
    {
177
        $eventMethod = 'beforeSend';
178
179
        if (method_exists($this, $eventMethod)) {
180
            $app->call([$this, $eventMethod], $file);
0 ignored issues
show
Bug introduced by
$file of type Symfony\Component\HttpFoundation\File\File is incompatible with the type array expected by parameter $parameters of Illuminate\Container\Container::call(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

180
            $app->call([$this, $eventMethod], /** @scrutinizer ignore-type */ $file);
Loading history...
181
        }
182
    }
183
184
    /**
185
     * Return the class suffix convention.
186
     *
187
     * @return string
188
     */
189
    protected function getSuffix()
190
    {
191
        return class_basename(__CLASS__);
192
    }
193
}
194