Completed
Push — dev ( 23aa35...38a6c8 )
by Yan
01:57
created

FileRequest::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 6
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
     * @return mixed
47
     */
48
    abstract protected function rules();
49
50
    /**
51
     * Boot the instance with the request.
52
     *
53
     * @param  \Illuminate\Http\Request $request
54
     * @return void
55
     */
56
    public function boot(Request $request)
57
    {
58
        $this->request = $request;
59
60
        // Guard the file through validations.
61
        $this->validate();
62
        
63
        $this->file = $request->file($this->getParameter());
64
65
        $this->booted = true;
66
    }
67
68
    /**
69
     * Return wheter the file request is booted.
70
     *
71
     * If is booted that means the file has been validated and
72
     * a request instance is available on instance.
73
     *
74
     * @return bool
75
     */
76
    public function isBooted()
77
    {
78
        return $this->booted;
79
    }
80
81
    /**
82
     * Return the file on request.
83
     *
84
     * @return \Illuminate\Http\UploadedFile
85
     */
86
    public function getFile()
87
    {
88
        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...
89
    }
90
91
    /**
92
     * Return the current request instance.
93
     *
94
     * @return \Illuminate\Http\Request
95
     */
96
    public function getRequest()
97
    {
98
        return $this->request;
99
    }
100
101
    /**
102
     * Prepared the file to send.
103
     *
104
     * @param  \Illuminate\Contracts\Container\Container $app
105
     * @return \Symfony\Component\HttpFoundation\File\File
106
     */
107
    public function prepareFile(Container $app)
108
    {
109
        $file = $this->moveFileToTempDirectory();
110
111
        return $this->executeFileEvents($app, $file);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->executeFileEvents($app, $file) also could return the type callable which is incompatible with the documented return type Symfony\Component\HttpFoundation\File\File.
Loading history...
112
    }
113
114
    /**
115
     * Validate the file with the defined rules.
116
     * 
117
     * @return void
118
     */
119
    public function validate()
120
    {
121
        $validationRules = $this->parseValidationRules();
122
123
        // Validate the request file from rules.
124
        $this->request->validate($validationRules);
125
    }
126
127
    /**
128
     * Return the file parameter on request.
129
     *
130
     * @return string
131
     */
132
    protected function getParameter()
133
    {
134
        $className = static::class;
135
        
136
        return $this->nameFromClass($className, 'FileRequest');
137
    }
138
139
    /**
140
     * Get the rules for the file validation.
141
     *
142
     * @return array
143
     */
144
    protected function parseValidationRules()
145
    {
146
        return [$this->getParameter() => $this->rules()];
147
    }
148
149
    /**
150
     * Move the file to a temporary destination.
151
     *
152
     * @return \Symfony\Component\HttpFoundation\File\File
153
     */
154
    protected function moveFileToTempDirectory()
155
    {
156
        $destination = $this->file->hashName();
157
158
        return $this->file->move($this->tempDirectory, $destination);
159
    }
160
161
    /**
162
     * Execute some generic event methods on class if available.
163
     *
164
     * Here the file can be changed, optimized, etc...
165
     *
166
     * @param  \Illuminate\Contracts\Container\Container $app
167
     * @param  \Symfony\Component\HttpFoundation\File\File $file
168
     * @return mixed
169
     */
170
    protected function executeFileEvents(Container $app, File $file)
171
    {
172
        $callable = [$this, 'beforeSend'];
173
174
        if (method_exists($callable[0], $callable[1])) {
175
176
            // Handle the result from event call.
177
            if ($result =  $app->call($callable, [$file])) {
178
                return $result;
179
            }
180
        }
181
182
        return $file;
183
    }
184
}
185