Passed
Push — develop ( 6dcea6...5ae9a6 )
by Septianata
16:24
created

AbstractRequest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 17
c 1
b 0
f 0
dl 0
loc 72
ccs 0
cts 21
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAttributes() 0 10 1
A getOrder() 0 3 1
A getOrderStatus() 0 6 1
A getBranch() 0 3 1
A getCustomer() 0 3 1
A getUser() 0 3 1
1
<?php
2
3
namespace App\Http\Requests\Order;
4
5
use App\Enum\OrderStatus as EnumOrderStatus;
6
use App\Infrastructure\Foundation\Http\FormRequest;
7
use App\Models\Branch;
8
use App\Models\Customer;
9
use App\Models\Order;
10
use App\Models\OrderStatus as ModelsOrderStatus;
11
use App\Models\User;
12
use Illuminate\Support\Facades\Auth;
13
14
abstract class AbstractRequest extends FormRequest
15
{
16
    /**
17
     * {@inheritDoc}
18
     */
19
    public static function getAttributes()
20
    {
21
        return [
22
            'customer_id' => trans('admin-lang.customer'),
23
            'user_id' => trans('admin-lang.user'),
24
            'branch_id' => trans('admin-lang.branch'),
25
            'code' => trans('Code'),
26
            'schedule_date' => trans('Schedule Date'),
27
            'order_status.status' => trans('Order Status'),
28
            'order_status.note' => trans('Note'),
29
        ];
30
    }
31
32
    /**
33
     * Return new order model instance based on the validated data from request.
34
     *
35
     * @return \App\Models\Order
36
     */
37
    public function getOrder(): Order
38
    {
39
        return Order::make($this->only('schedule_date'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Order:...>only('schedule_date')) could return the type Illuminate\Database\Eloquent\Model which includes types incompatible with the type-hinted return App\Models\Order. Consider adding an additional type-check to rule them out.
Loading history...
40
    }
41
42
    /**
43
     * Return new order status model instance based on the validated data from request.
44
     *
45
     * @return \App\Models\OrderStatus
46
     */
47
    public function getOrderStatus(): ModelsOrderStatus
48
    {
49
        return ModelsOrderStatus::make([
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\OrderS...t\Facades\Auth::user()) could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the type-hinted return App\Models\OrderStatus. Consider adding an additional type-check to rule them out.
Loading history...
50
            'status' => data_get($this->validated(), 'order_status.status', EnumOrderStatus::on_progress()),
51
            'note' => data_get($this->validated(), 'order_status.note'),
52
        ])->setIssuerableRelationValue(Auth::user());
53
    }
54
55
    /**
56
     * Return customer model based on the request.
57
     *
58
     * @param  string  $key
59
     * @return \App\Models\Customer
60
     */
61
    public function getCustomer(string $key = 'customer_id'): Customer
62
    {
63
        return Customer::find($this->input($key));
64
    }
65
66
    /**
67
     * Return user model based on the request.
68
     *
69
     * @param  string  $key
70
     * @return \App\Models\User|null
71
     */
72
    public function getUser(string $key = 'user_id'): ?User
73
    {
74
        return User::find($this->input($key));
75
    }
76
77
    /**
78
     * Return branch model based on the request.
79
     *
80
     * @param  string  $key
81
     * @return \App\Models\Branch|null
82
     */
83
    public function getBranch(string $key = 'branch_id'): ?Branch
84
    {
85
        return Branch::find($this->input($key));
86
    }
87
}
88