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

Relation::setItemsRelationValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace App\Models\Concerns\Order;
4
5
use App\Models\Branch;
6
use App\Models\Customer;
7
use App\Models\Item;
8
use App\Models\OrderStatus;
9
use App\Models\User;
10
use Illuminate\Database\Eloquent\Collection;
11
use Illuminate\Database\Eloquent\Relations\BelongsTo;
12
use Illuminate\Database\Eloquent\Relations\HasMany;
13
use Illuminate\Database\Eloquent\Relations\HasOne;
14
15
/**
16
 * @property int $customer_id Foreign key of \App\Models\Customer.
17
 * @property-read \App\Models\Customer $customer
18
 * @property int|null $user_id Foreign key of \App\Models\User.
19
 * @property-read \App\Models\User|null $user
20
 * @property int|null $branch_id Foreign key of \App\Models\Branch.
21
 * @property-read \App\Models\Branch|null $branch
22
 * @property-read \Illuminate\Database\Eloquent\Collection<\App\Models\OrderStatus> $statuses
23
 * @property-read \App\Models\OrderStatus $latestStatus
24
 * @property-read \Illuminate\Database\Eloquent\Collection<\App\Models\Item> $items
25
 *
26
 * @see \App\Models\Order
27
 */
28
trait Relation
29
{
30
    /**
31
     * Define an inverse one-to-one or many relationship with \App\Models\Customer.
32
     *
33
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
34
     */
35 3
    public function customer(): BelongsTo
36
    {
37 3
        return $this->belongsTo(Customer::class);
0 ignored issues
show
Bug introduced by
It seems like belongsTo() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

37
        return $this->/** @scrutinizer ignore-call */ belongsTo(Customer::class);
Loading history...
38
    }
39
40
    /**
41
     * Return \App\Models\Customer model relation value.
42
     *
43
     * @return \App\Models\Customer
44
     */
45
    public function getCustomerRelationValue(): Customer
46
    {
47
        return $this->getRelationValue('customer');
0 ignored issues
show
Bug introduced by
It seems like getRelationValue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

47
        return $this->/** @scrutinizer ignore-call */ getRelationValue('customer');
Loading history...
48
    }
49
50
    /**
51
     * Set \App\Models\Customer model relation value.
52
     *
53
     * @param  \App\Models\Customer  $customer
54
     * @return $this
55
     */
56 1
    public function setCustomerRelationValue(Customer $customer)
57
    {
58 1
        $this->customer()->associate($customer);
59
60 1
        return $this;
61
    }
62
63
    /**
64
     * Define an inverse one-to-one or many relationship with \App\Models\User.
65
     *
66
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
67
     */
68
    public function user(): BelongsTo
69
    {
70
        return $this->belongsTo(User::class);
71
    }
72
73
    /**
74
     * Return \App\Models\User model relation value.
75
     *
76
     * @return \App\Models\User|null
77
     */
78
    public function getUserRelationValue(): ?User
79
    {
80
        return $this->getRelationValue('user');
81
    }
82
83
    /**
84
     * Set \App\Models\User model relation value.
85
     *
86
     * @param  \App\Models\User  $user
87
     * @return $this
88
     */
89
    public function setUserRelationValue(User $user)
90
    {
91
        $this->user()->associate($user);
92
93
        return $this;
94
    }
95
96
    /**
97
     * Define an inverse one-to-one or many relationship with \App\Models\Branch.
98
     *
99
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
100
     */
101
    public function branch(): BelongsTo
102
    {
103
        return $this->belongsTo(Branch::class);
104
    }
105
106
    /**
107
     * Return \App\Models\Branch model relation value.
108
     *
109
     * @return \App\Models\Branch
110
     */
111
    public function getBranchRelationValue(): Branch
112
    {
113
        return $this->getRelationValue('branch');
114
    }
115
116
    /**
117
     * Set \App\Models\Branch model relation value.
118
     *
119
     * @param  \App\Models\Branch  $branch
120
     * @return $this
121
     */
122
    public function setBranchRelationValue(Branch $branch)
123
    {
124
        $this->branch()->associate($branch);
125
126
        return $this;
127
    }
128
129
    /**
130
     * Define a one-to-many relationship with App\Models\OrderStatus.
131
     *
132
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
133
     */
134 3
    public function statuses(): HasMany
135
    {
136 3
        return $this->hasMany(OrderStatus::class);
0 ignored issues
show
Bug introduced by
It seems like hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

136
        return $this->/** @scrutinizer ignore-call */ hasMany(OrderStatus::class);
Loading history...
137
    }
138
139
    /**
140
     * Return collection of \App\Models\OrderStatus model relation value.
141
     *
142
     * @return \Illuminate\Database\Eloquent\Collection<\App\Models\OrderStatus>
143
     */
144
    public function getStatusesRelationValue(): Collection
145
    {
146
        return $this->getCollectionValue('statuses', OrderStatus::class);
0 ignored issues
show
Bug introduced by
It seems like getCollectionValue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

146
        return $this->/** @scrutinizer ignore-call */ getCollectionValue('statuses', OrderStatus::class);
Loading history...
147
    }
148
149
    /**
150
     * Set collection of \App\Models\OrderStatus model relation value.
151
     *
152
     * @param  \Illuminate\Database\Eloquent\Collection<\App\Models\OrderStatus>  $statuses
153
     * @return $this
154
     */
155
    public function setStatusesRelationValue(Collection $statuses)
156
    {
157
        if ($this->isCollectionValid($statuses, OrderStatus::class)) {
0 ignored issues
show
Bug introduced by
It seems like isCollectionValid() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

157
        if ($this->/** @scrutinizer ignore-call */ isCollectionValid($statuses, OrderStatus::class)) {
Loading history...
158
            $this->setRelation('statuses', $statuses);
0 ignored issues
show
Bug introduced by
It seems like setRelation() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

158
            $this->/** @scrutinizer ignore-call */ 
159
                   setRelation('statuses', $statuses);
Loading history...
159
        }
160
161
        return $this;
162
    }
163
164
    /**
165
     * Define a one-to-one relationship with \App\Models\OrderStatus from a larger one-to-many relationship.
166
     *
167
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
168
     */
169
    public function latestStatus(): HasOne
170
    {
171
        return $this->hasOne(OrderStatus::class)->latestOfMany('created_at');
0 ignored issues
show
Bug introduced by
It seems like hasOne() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

171
        return $this->/** @scrutinizer ignore-call */ hasOne(OrderStatus::class)->latestOfMany('created_at');
Loading history...
172
    }
173
174
    /**
175
     * Return \App\Models\OrderStatus model relation value.
176
     *
177
     * @return \App\Models\OrderStatus
178
     */
179
    public function getLatestStatusRelationValue(): ?OrderStatus
180
    {
181
        return $this->getRelationValue('latestStatus');
182
    }
183
184
    /**
185
     * Define a one-to-many relationship with App\Models\Item.
186
     *
187
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
188
     */
189 3
    public function items(): HasMany
190
    {
191 3
        return $this->hasMany(Item::class);
192
    }
193
194
    /**
195
     * Return collection of \App\Models\Item model relation value.
196
     *
197
     * @return \Illuminate\Database\Eloquent\Collection<\App\Models\Item>
198
     */
199
    public function getItemsRelationValue(): Collection
200
    {
201
        return $this->getCollectionValue('items', Item::class);
202
    }
203
204
    /**
205
     * Set collection of \App\Models\Item model relation value.
206
     *
207
     * @param  \Illuminate\Database\Eloquent\Collection<\App\Models\Item>  $items
208
     * @return $this
209
     */
210
    public function setItemsRelationValue(Collection $items)
211
    {
212
        if ($this->isCollectionValid($items, Item::class)) {
213
            $this->setRelation('items', $items);
214
        }
215
216
        return $this;
217
    }
218
}
219