Skip to content

Commit e4c7bae

Browse files
committed
Move all requirement logics to the CollectibleController
1 parent b8f2176 commit e4c7bae

File tree

3 files changed

+49
-40
lines changed

3 files changed

+49
-40
lines changed

app/Http/Controllers/Collectible/CollectibleController.php

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,52 +46,74 @@ public function show(Collectible $collectible): \Illuminate\Contracts\View\Facto
4646

4747
$collectible->load('requirements');
4848

49-
// Evaluate the requirements for this collectible for the current user
49+
// User attributes
5050
$userAttributes = [
5151
'uploaded' => [
5252
'value' => $user->uploaded ?? 0,
53-
'label' => 'Account Upload'
53+
'label' => __('common.account').' '.__('common.upload')
5454
],
5555
'seedsize' => [
5656
'value' => $user->seedsize ?? 0,
57-
'label' => 'Seeding Size'
57+
'label' => __('torrent.seedsize')
5858
],
5959
'average_seedtime' => [
6060
'value' => $user->history_avg_seedtime ?? 0,
61-
'label' => 'Average Seedtime'
61+
'label' => 'Average '.__('torrent.seedtime')
6262
],
6363
'ratio' => [
6464
'value' => $user->ratio ?? 0,
65-
'label' => 'Account Ratio'
65+
'label' => __('common.account').' '.__('common.ratio')
6666
],
6767
'age' => [
6868
'value' => $user->created_at->diffInSeconds(now()),
69-
'label' => 'Account Age'
69+
'label' => __('common.account').' '.__('torrent.age')
7070
],
7171
];
7272

73+
$requirementsResults = [];
74+
7375
$userMeetsAllRequirements = true;
7476

7577
foreach ($collectible->requirements as $req) {
7678
$attrValue = $userAttributes[$req->operand1]['value'];
7779

78-
// Determine if the requirement is met
79-
$requirementMet = match ($req->operator) {
80-
'>' => $attrValue > $req->operand2,
81-
'<' => $attrValue < $req->operand2,
82-
'=' => $attrValue == $req->operand2,
83-
'>=' => $attrValue >= $req->operand2,
84-
'<=' => $attrValue <= $req->operand2,
85-
default => false,
86-
};
87-
88-
// Update the overall requirement status
89-
$userMeetsAllRequirements &= $requirementMet;
90-
91-
// Break early if any requirement is not met
92-
if (!$userMeetsAllRequirements) {
93-
break;
80+
// Evaluate the requirement using switch statement
81+
switch ($req->operator) {
82+
case '>':
83+
$meetsRequirement = $attrValue > $req->operand2;
84+
85+
break;
86+
case '<':
87+
$meetsRequirement = $attrValue < $req->operand2;
88+
89+
break;
90+
case '=':
91+
$meetsRequirement = $attrValue == $req->operand2;
92+
93+
break;
94+
case '>=':
95+
$meetsRequirement = $attrValue >= $req->operand2;
96+
97+
break;
98+
case '<=':
99+
$meetsRequirement = $attrValue <= $req->operand2;
100+
101+
break;
102+
default:
103+
$meetsRequirement = false;
104+
105+
break;
94106
}
107+
108+
// Collect result for the view
109+
$requirementsResults[] = [
110+
'requirement' => $req,
111+
'meets' => $meetsRequirement,
112+
'label' => $userAttributes[$req->operand1]['label']
113+
];
114+
115+
// Update overall status
116+
$userMeetsAllRequirements &= $meetsRequirement;
95117
}
96118

97119
return view('collectible.show', [
@@ -110,6 +132,7 @@ public function show(Collectible $collectible): \Illuminate\Contracts\View\Facto
110132
->first(),
111133
'availableCount' => $collectible->items()->whereNull('user_id')->count(),
112134
'userAttributes' => $userAttributes,
135+
'requirementsResults' => $requirementsResults,
113136
'userMeetsAllRequirements' => $userMeetsAllRequirements,
114137
]);
115138
}

app/Http/Controllers/User/CollectibleController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function index(Request $request, User $user): \Illuminate\Contracts\View\
3939
->with('category')
4040
->get()
4141
->groupBy('category_id'),
42-
'user' => $user,
42+
'user' => $user,
4343
]);
4444
}
4545
}

resources/views/collectible/show.blade.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -210,25 +210,11 @@ class="form__button form__button--filled"
210210
@endif
211211
</dd>
212212
</div>
213-
@foreach ($collectible->requirements as $requirement)
213+
@foreach ($requirementsResults as $result)
214214
<div class="key-value__group">
215-
<dt>{{ $userAttributes[$requirement->operand1]['label'] }}</dt>
215+
<dt>{{ $result['label'] }}</dt>
216216
<dd>
217-
@php
218-
// Retrieve the user's attribute value or default to 0
219-
$userValue = $userAttributes[$requirement->operand1]['value'] ?? 0;
220-
// Build the comparison expression
221-
$operator = $requirement->operator; // e.g., '>=', '<=', '==', etc.
222-
$operand2 = $requirement->operand2 ?? 0;
223-
$expression = '$userValue ' . $operator . ' ' . $operand2;
224-
@endphp
225-
226-
@php
227-
$meetsRequirement = false;
228-
eval('$meetsRequirement = (' . $expression . ');');
229-
@endphp
230-
231-
@if ($meetsRequirement)
217+
@if ($result['meets'])
232218
<i class="{{ config('other.font-awesome') }} fa-check text-green"></i>
233219
@else
234220
<i class="{{ config('other.font-awesome') }} fa-times text-red"></i>

0 commit comments

Comments
 (0)