Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.12% covered (success)
94.12%
16 / 17
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
StoreRolePlayProjectRequest
94.12% covered (success)
94.12%
16 / 17
66.67% covered (warning)
66.67%
2 / 3
6.01
0.00% covered (danger)
0.00%
0 / 1
 prepareForValidation
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 authorize
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
3.01
 rules
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Http\Requests\v2\RolePlay;
4
5use App\Http\Models\RolePlayProjects;
6use App\Http\Models\UserAddOns;
7use App\Http\Services\RolePlay\ObjectionNormalizer;
8use Illuminate\Foundation\Http\FormRequest;
9
10class StoreRolePlayProjectRequest extends FormRequest
11{
12    /**
13     * Normalize any legacy-shape `objections` payload (string options or
14     * duplicate categories) into the canonical {text, company_sizes}
15     * shape BEFORE validation rules fire. Keeps older clients working.
16     */
17    protected function prepareForValidation(): void
18    {
19        if ($this->has('objections')) {
20            $this->merge([
21                'objections' => ObjectionNormalizer::normalize($this->input('objections')),
22            ]);
23        }
24    }
25
26    /**
27     * Determine if the user is authorized to make this request.
28     */
29    public function authorize(): bool
30    {
31        $user = $this->user();
32
33        $rolePlayAddOn = UserAddOns::where('user_id', $user->id)
34            ->where('product', 'RolePlay')
35            ->where('status', 'active')
36            ->orderBy('created_at', 'desc')
37            ->first();
38
39        if (! $rolePlayAddOn) {
40            return false;
41        }
42
43        $rolePlayAddOn->load('addOn');
44
45        $project_credits = $rolePlayAddOn->addOn->features['project_credits'] ?? 1;
46
47        $usedRolePlayProjects = RolePlayProjects::where('user_id', $user->id)->count() ?? 0;
48
49        return $project_credits === -1 || $usedRolePlayProjects < $project_credits;
50    }
51
52    /**
53     * Get the validation rules that apply to the request.
54     */
55    public function rules(): array
56    {
57        return RolePlayProjects::getRules();
58    }
59}