Passed
Push — develop ( 6302a9...3db2fb )
by Septianata
04:45
created

StoreRequest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 25
dl 0
loc 62
ccs 19
cts 20
cp 0.95
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A authorize() 0 3 1
A storeImage() 0 14 2
A attributes() 0 10 1
A rules() 0 10 1
1
<?php
2
3
namespace App\Http\Requests\Denomination;
4
5
use App\Enum\DenominationType;
6
use App\Infrastructure\Foundation\Http\FormRequest;
7
use App\Models\Denomination;
8
9
class StoreRequest extends FormRequest
10
{
11
    /**
12
     * {@inheritDoc}
13
     */
14 1
    public function authorize()
15
    {
16 1
        return !is_null($this->user());
17
    }
18
19
    /**
20
     * {@inheritDoc}
21
     */
22 1
    public function rules()
23
    {
24
        return [
25 1
            'name' => 'required|string|max:255',
26
            'value' => 'required|numeric|min:0',
27
            'type' => 'required|enum:' . DenominationType::class,
28
            'quantity_per_bundle' => 'required|numeric|min:0',
29
            'minimum_order_bundle' => 'required|numeric|min:0',
30
            'maximum_order_bundle' => 'required|numeric|gte:minimum_order_bundle',
31
            'image' => 'sometimes|nullable|image',
32
        ];
33
    }
34
35
    /**
36
     * {@inheritDoc}
37
     */
38 1
    public function attributes()
39
    {
40
        return [
41 1
            'name' => trans('Name'),
42 1
            'value' => trans('Value'),
43 1
            'type' => trans('Type'),
44 1
            'quantity_per_bundle' => trans('Quantity Per Bundle'),
45 1
            'minimum_order_bundle' => trans('Minimum Order Bundle'),
46 1
            'maximum_order_bundle' => trans('Maximum Order Bundle'),
47 1
            'image' => trans('Image'),
48
        ];
49
    }
50
51
    /**
52
     * Store the image file from the incoming request.
53
     *
54
     * @param  string  $key
55
     * @return string|null
56
     */
57 1
    public function storeImage(string $key = 'image'): ?string
58
    {
59 1
        if (!$this->hasFile($key)) {
60
            return null;
61
        }
62
63 1
        $file = $this->file($key);
64
65 1
        $file->storeAs(
66 1
            Denomination::IMAGE_PATH,
67 1
            $filename = ($this->input('value') . '.' . $file->getClientOriginalExtension())
68
        );
69
70 1
        return $filename;
71
    }
72
}
73