Passed
Push — develop ( 9324e6...f3eb57 )
by Septianata
04:31
created

AbstractRequest::getAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 21
ccs 15
cts 15
cp 1
rs 9.7998
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace App\Http\Requests\Order;
4
5
use App\Infrastructure\Foundation\Http\FormRequest;
6
use App\Models\Branch;
7
use App\Models\Customer;
8
use App\Models\Denomination;
9
use App\Models\Item;
10
use App\Models\Order;
11
use App\Models\OrderStatus;
12
use App\Models\User;
13
use Illuminate\Database\Eloquent\Collection;
14
15
abstract class AbstractRequest extends FormRequest
16
{
17
    /**
18
     * {@inheritDoc}
19
     */
20 1
    public static function getAttributes()
21
    {
22 1
        $attributes = [
23 1
            'customer_id' => trans('admin-lang.customer'),
24 1
            'user_id' => trans('admin-lang.user'),
25 1
            'branch_id' => trans('admin-lang.branch'),
26 1
            'code' => trans('Code'),
27 1
            'schedule_date' => trans('Schedule Date'),
28 1
            'order_status.status' => trans('Order Status'),
29 1
            'order_status.note' => trans('Note'),
30
        ];
31
32 1
        foreach (request()->input('items', []) as $index => $item) {
33 1
            $number = $index + 1;
34
35 1
            $attributes['items.' . $index . '.denomination_id'] = trans('admin-lang.denomination') . ' ' . $number;
0 ignored issues
show
Bug introduced by
Are you sure trans('admin-lang.denomination') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

35
            $attributes['items.' . $index . '.denomination_id'] = /** @scrutinizer ignore-type */ trans('admin-lang.denomination') . ' ' . $number;
Loading history...
36 1
            $attributes['items.' . $index . '.quantity_per_bundle'] = trans('Quantity Per Bundle') . ' ' . $number;
0 ignored issues
show
Bug introduced by
Are you sure trans('Quantity Per Bundle') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

36
            $attributes['items.' . $index . '.quantity_per_bundle'] = /** @scrutinizer ignore-type */ trans('Quantity Per Bundle') . ' ' . $number;
Loading history...
37 1
            $attributes['items.' . $index . '.bundle_quantity'] = trans('Bundle Quantity') . ' ' . $number;
0 ignored issues
show
Bug introduced by
Are you sure trans('Bundle Quantity') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

37
            $attributes['items.' . $index . '.bundle_quantity'] = /** @scrutinizer ignore-type */ trans('Bundle Quantity') . ' ' . $number;
Loading history...
38
        }
39
40 1
        return $attributes;
41
    }
42
43
    /**
44
     * Return new order model instance based on the validated data from request.
45
     *
46
     * @return \App\Models\Order
47
     */
48 1
    public function getOrder(): Order
49
    {
50 1
        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...
51
    }
52
53
    /**
54
     * Return new order status model instance based on the validated data from request.
55
     *
56
     * @return \App\Models\OrderStatus
57
     */
58 1
    public function getOrderStatus(): OrderStatus
59
    {
60 1
        return OrderStatus::make([
61 1
            'status' => data_get($this->validated(), 'order_status.status'),
62 1
            'note' => data_get($this->validated(), 'order_status.note'),
63 1
        ])->setIssuerableRelationValue($this->getCustomer());
64
    }
65
66
    /**
67
     * Return collection of new item model based on the validated data from request.
68
     *
69
     * @return \Illuminate\Database\Eloquent\Collection<\App\Models\Item>
70
     */
71 1
    public function getItems(): Collection
72
    {
73 1
        return Collection::make(data_get($this->validated(), 'items.*', []))->map(
74 1
            fn (array $item) => Item::make($item)->setDenominationRelationValue(
75 1
                Denomination::find($item['denomination_id'])
76
            )
77 1
        );
78
    }
79
80
    /**
81
     * Return customer model based on the request.
82
     *
83
     * @param  string  $key
84
     * @return \App\Models\Customer
85
     */
86 1
    public function getCustomer(string $key = 'customer_id'): Customer
87
    {
88 1
        return Customer::find($this->input($key));
89
    }
90
91
    /**
92
     * Return user model based on the request.
93
     *
94
     * @param  string  $key
95
     * @return \App\Models\User|null
96
     */
97 1
    public function getUser(string $key = 'user_id'): ?User
98
    {
99 1
        return User::find($this->input($key));
100
    }
101
102
    /**
103
     * Return branch model based on the request.
104
     *
105
     * @param  string  $key
106
     * @return \App\Models\Branch|null
107
     */
108 1
    public function getBranch(string $key = 'branch_id'): ?Branch
109
    {
110 1
        return Branch::find($this->input($key));
111
    }
112
}
113