Passed
Push — develop ( b4818b...dcecff )
by Septianata
16:14
created

AbstractRequest::getBranchFromRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace App\Http\Requests\OrderStatus;
4
5
use App\Infrastructure\Foundation\Http\FormRequest;
6
use App\Models\Branch;
7
use App\Models\User;
8
use Illuminate\Support\Carbon;
9
use Illuminate\Support\Facades\Auth;
10
11
abstract class AbstractRequest extends FormRequest
12
{
13
    /**
14
     * {@inheritDoc}
15
     */
16
    public static function getAttributes()
17
    {
18
        return [
19
            'branch_id' => trans('admin-lang.branch'),
20
            'schedule_date' => trans('Schedule Date'),
21
            'order_status.note' => trans('Note'),
22
        ];
23
    }
24
25
    /**
26
     * Return branch model instance from the request.
27
     *
28
     * @param  string  $key
29
     * @return \App\Models\Branch
30
     *
31
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
32
     */
33
    public function getBranchFromRequest(string $key = 'branch_id'): Branch
34
    {
35
        return Branch::findOrFail($this->input($key));
36
    }
37
38
    /**
39
     * Return user model instance from the request.
40
     *
41
     * @param  string  $key
42
     * @return \App\Models\User
43
     *
44
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
45
     */
46
    public function getUserFromRequest(string $key = 'user_id'): User
47
    {
48
        return User::find($this->input($key)) ?? Auth::user();
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\User::...rt\Facades\Auth::user() could return the type null which is incompatible with the type-hinted return App\Models\User. Consider adding an additional type-check to rule them out.
Loading history...
49
    }
50
51
    /**
52
     * Return "schedule_date" value from the request.
53
     *
54
     * @param  string  $key
55
     * @return \Illuminate\Support\Carbon
56
     */
57
    public function getScheduleDate(string $key = 'schedule_date'): Carbon
58
    {
59
        return Carbon::parse($this->input($key, 'now'));
60
    }
61
}
62