Passed
Push — dev ( 44a5a9...75307b )
by Yan
02:18
created

FileRequest::moveFileToTempDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lincable\Http;
4
5
use Illuminate\Http\Request;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Http\UploadedFile;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\UploadedFile was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Lincable\Concerns\BuildClassnames;
8
use Illuminate\Contracts\Container\Container;
9
use Symfony\Component\HttpFoundation\File\File;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\File\File was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
abstract class FileRequest
12
{
13
    use BuildClassnames;
14
15
    /**
16
     * The uploaded file instance.
17
     *
18
     * @var \Illuminate\Http\File
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\File was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
     */
20
    protected $file;
21
22
    /**
23
     * The request instance.
24
     *
25
     * @var \Illuminate\Http\Request
26
     */
27
    protected $request;
28
29
    /**
30
     * The directory where the uploaded file will be temporary moved.
31
     *
32
     * @var string
33
     */
34
    protected $tempDirectory = '/tmp';
35
36
    /**
37
     * Rules to validate the file on request.
38
     *
39
     * @param  \Illuminate\Http\UploadedFile $file
40
     * @return mixed
41
     */
42
    abstract protected function rules(UploadedFile $file): mixed;
0 ignored issues
show
Bug introduced by
The type Lincable\Http\mixed was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
44
    /**
45
     * Boot the instance with the request.
46
     *
47
     * @param  \Illuminate\Http\Request $request
48
     * @return void
49
     */
50
    public function boot(Request $request)
51
    {
52
        $this->request = $request;
53
54
        $this->guardFile($request->file($this->getParameter()));
55
    }
56
57
    /**
58
     * Return the file on request.
59
     *
60
     * @return \Illuminate\Http\UploadedFile
61
     */
62
    public function getFile()
63
    {
64
        return $this->file;
65
    }
66
67
    /**
68
     * Return the current request instance.
69
     *
70
     * @return \Illuminate\Http\Request
71
     */
72
    public function getRequest()
73
    {
74
        return $this->request;
75
    }
76
77
    /**
78
     * Prepared the file to send.
79
     *
80
     * @return \Symfony\Component\HttpFoundation\File\File
81
     */
82
    public function prepareFile()
83
    {
84
        $file = $this->moveFileToTempDirectory();
85
86
        $this->executeFileEvents(app(), $file);
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

86
        $this->executeFileEvents(/** @scrutinizer ignore-call */ app(), $file);
Loading history...
87
88
        return $file;
89
    }
90
91
    /**
92
     * Return the file parameter on request.
93
     *
94
     * @return string
95
     */
96
    protected function getParameter()
97
    {
98
        $className = static::class;
99
100
        return $this->nameFromClass($className);
101
    }
102
103
    /**
104
     * Guard the file through validations and then set
105
     * the file on class instance.
106
     *
107
     * @param  \Illuminate\Http\UploadedFile $file
108
     * @return void
109
     */
110
    protected function guardFile(UploadedFile $file)
111
    {
112
        $validationRules = $this->parseValidationRules($file);
113
114
        // Validate the request file from rules.
115
        $this->request->validate($validationRules);
116
117
        $this->file = $file;
118
    }
119
120
    /**
121
     * Get the rules for the file validation.
122
     *
123
     * @param  \Illuminate\Http\UploadedFile $file
124
     * @return array
125
     */
126
    protected function parseValidationRules(UploadedFile $file)
127
    {
128
        return [$this->getParameter() => $this->rules($file)];
129
    }
130
131
    /**
132
     * Move the file to a temporary destination.
133
     *
134
     * @return \Symfony\Component\HttpFoundation\File\File
135
     */
136
    protected function moveFileToTempDirectory()
137
    {
138
        $destination = $this->file->hashName($this->tempDirectory);
139
140
        return $this->file->move($destination);
141
    }
142
143
    /**
144
     * Execute some generic event methods on class if available.
145
     *
146
     * Here the file can be changed, optimized, etc...
147
     *
148
     * @return void
149
     */
150
    protected function executeFileEvents(Container $app, File $file)
151
    {
152
        $eventMethod = 'beforeSend';
153
154
        if (method_exists($this, $eventMethod)) {
155
            $app->call([$this, $method], $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

155
            $app->call([$this, $method], /** @scrutinizer ignore-type */ $file);
Loading history...
Comprehensibility Best Practice introduced by
The variable $method seems to be never defined.
Loading history...
156
        }
157
    }
158
159
    /**
160
     * Return the class suffix convention.
161
     *
162
     * @return string
163
     */
164
    protected function getSuffix()
165
    {
166
        return 'FileRequest';
167
    }
168
}
169