Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserPersonaCreateFormRequest
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 authorize
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 rules
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 1
12
 prepareForValidation
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace App\Http\Requests\UserPersona;
4
5use App\Http\Models\UserPersona;
6use App\Traits\SubscriptionTrait;
7use Illuminate\Foundation\Http\FormRequest;
8
9class UserPersonaCreateFormRequest extends FormRequest
10{
11    use SubscriptionTrait;
12
13    /**
14     * Determine if the user is authorized to make this request.
15     */
16    public function authorize(): bool
17    {
18        $user = $this->user();
19        $plan = $this->getCurrentPlan($user);
20
21        if ($this->isMethod('put')) {
22            return true;
23        }
24
25        $personaCount = UserPersona::where('user_id', $user->_id)->count();
26
27        return $personaCount < $plan->user_persona_available;
28    }
29
30    /**
31     * Get the validation rules that apply to the request.
32     *
33     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
34     */
35    public function rules(): array
36    {
37        $requiredString = 'required|string';
38        $sometimesString = 'sometimes|nullable|string';
39        $requiredArray = 'required|array|min:1';
40        $sometimesArray = 'sometimes|array';
41        $sometimesStringMax = 'sometimes|nullable|string|max:1000';
42
43        $rules = [
44            'name' => $requiredString,
45            'professional_interests' => $requiredArray,
46            'professional_interests.*' => $requiredString,
47            'industry' => $requiredString,
48            'prompt_tone_id' => 'required|string|exists:prompt_tone,_id',
49            'type' => 'required|string|in:person,company',
50            'linkedin_url' => $sometimesString,
51            'achievements' => $sometimesString,
52            'preferred_keywords_and_phrases' => $sometimesArray,
53            'preferred_keywords_and_phrases.*' => $requiredString,
54            'communication_style' => $sometimesArray,
55            'communication_style.*' => $requiredString,
56            'education_background' => $sometimesArray,
57            'education_background.*' => $requiredString,
58            'field_of_expertise' => $sometimesArray,
59            'field_of_expertise.*' => $requiredString,
60            'goals' => $sometimesString,
61            'hobbies' => $sometimesArray,
62            'hobbies.*' => $requiredString,
63            'personal_interests' => $sometimesArray,
64            'personal_interests.*' => $requiredString,
65            'skills' => $sometimesArray,
66            'skills.*' => $requiredString,
67            'taboos' => $sometimesArray,
68            'taboos.*' => $requiredString,
69            'title' => $sometimesString,
70            'content' => $sometimesStringMax,
71            'networking' => $sometimesStringMax,
72            'personal_info' => $sometimesStringMax,
73            'ai_emulation' => $requiredString,
74            'is_default' => 'required|boolean',
75        ];
76
77        if ($this->input('type') === 'person') {
78            $rules['first_name'] = $requiredString;
79            $rules['last_name'] = $requiredString;
80            $rules['company_name'] = $sometimesString;
81        }
82
83        if ($this->input('type') === 'company') {
84            $rules['first_name'] = $sometimesString;
85            $rules['last_name'] = $sometimesString;
86            $rules['company_name'] = $requiredString;
87        }
88
89        return $rules;
90    }
91
92    protected function prepareForValidation()
93    {
94        $this->merge([
95            'items_per_page' => $this->input('items_per_page', 10),
96            'page' => $this->input('page', 1),
97        ]);
98    }
99}