AddNewCommentRequest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 20
dl 0
loc 45
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B rules() 0 35 7
A authorize() 0 3 1
1
<?php
2
3
namespace WebDevEtc\BlogEtc\Requests;
4
5
use Auth;
6
use Illuminate\Foundation\Http\FormRequest;
7
use WebDevEtc\BlogEtc\Interfaces\CaptchaInterface;
8
9
class AddNewCommentRequest extends FormRequest
10
{
11
    public function authorize()
12
    {
13
        return 'built_in' === config('blogetc.comments.type_of_comments_to_show');
14
    }
15
16
    /**
17
     * Get the validation rules that apply to the request.
18
     */
19
    public function rules(): array
20
    {
21
        $return = [
22
            'comment'        => ['required', 'string', 'min:3', 'max:1000'],
23
            'author_name'    => ['string', 'min:1', 'max:50'],
24
            'author_email'   => ['string', 'nullable', 'min:1', 'max:254', 'email'],
25
            'author_website' => ['string', 'nullable', 'min:'.strlen('http://a.b'), 'max:175', 'active_url'],
26
        ];
27
28
        $return['author_name'][] = Auth::check() && config('blogetc.comments.save_user_id_if_logged_in', true)
29
            ? 'nullable'
30
            : 'required';
31
32
        if (config('blogetc.captcha.captcha_enabled')) {
33
            /** @var string $captcha_class */
34
            $captcha_class = config('blogetc.captcha.captcha_type');
35
36
            /** @var CaptchaInterface $captcha */
37
            $captcha = new $captcha_class();
38
39
            $return[$captcha->captcha_field_name()] = $captcha->rules();
40
        }
41
42
        // in case you need to implement something custom, you can use this...
43
        if (config('blogetc.comments.rules') && is_callable(config('blogetc.comments.rules'))) {
44
            /** @var callable $func */
45
            $func = config('blogetc.comments.rules');
46
            $return = $func($return);
47
        }
48
49
        if (config('blogetc.comments.require_author_email')) {
50
            $return['author_email'][] = 'required';
51
        }
52
53
        return $return;
54
    }
55
}
56