UserService   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 25
c 2
b 0
f 1
dl 0
loc 77
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseValidationRules() 0 9 1
A getGuestValidationRules() 0 18 1
A getAdministratorValidationRules() 0 7 1
A getViewerValidationRules() 0 7 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace App\Services;
5
6
use App;
7
use App\User;
8
9
class UserService
10
{
11
12
    /**
13
     * User creation validation rules for guest users:
14
     * only students can be created.
15
     *
16
     * @return array
17
     *
18
     * @todo extract configuration parameter reading and country list retrieval
19
     */
20
    public function getGuestValidationRules() : array
21
    {
22
        $rules = $this->getBaseValidationRules();
23
24
        $rules["role"][] = "in:" . User::ROLE_STUDENT;
25
        $rules["identification_number"] = [
26
            "required",
27
            "regex:/" . config("clqei.students.identification_number.pattern") . "/",
28
            "unique:students"
29
        ];
30
        $rules["gender"] = ["required", "in:male,female"];
31
        $rules["nationality"] = [
32
            "required",
33
            "in:" . implode(",", App::make("App\Services\CountryService")->getCountryCodes())
34
        ];
35
        $rules["email"][] = "regex:/" . config("clqei.students.email.pattern") . "/";
36
37
        return $rules;
38
    }
39
40
    /**
41
     * Base validation rules for user creation.
42
     *
43
     * @return array
44
     */
45
    public function getBaseValidationRules() : array
46
    {
47
48
        return [
49
            "first_name" => ["required", "string", "min:2", "max:255"],
50
            "last_name" => ["required", "string", "min:2", "max:255"],
51
            "email" => ["required", "string", "email", "max:255", "unique:users"],
52
            "password" => ["required", "string", "min:6", "confirmed"],
53
            "role" => ["required"],
54
        ];
55
56
    }
57
58
    /**
59
     * User creation validation rules for viewers:
60
     * only viewers can be created.
61
     *
62
     * @return array
63
     */
64
    public function getViewerValidationRules() : array
65
    {
66
        $rules = $this->getBaseValidationRules();
67
68
        $rules["role"][] = "in:" . User::ROLE_VIEWER;
69
70
        return $rules;
71
    }
72
73
    /**
74
     * User creation validation rules for administrators:
75
     * both viewers and administrators can be created.
76
     *
77
     * @return array
78
     */
79
    public function getAdministratorValidationRules() : array
80
    {
81
        $rules = $this->getBaseValidationRules();
82
83
        $rules["role"][] = "in:" . User::ROLE_VIEWER . "," . User::ROLE_ADMINISTRATOR;
84
85
        return $rules;
86
    }
87
88
}
89