Passed
Push — master ( f5817c...637aa7 )
by Mauro
02:29
created

ValidationService::validateStatus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace App\Service;
4
5
use App\Service\Messages;
6
use Respect\Validation\Validator as v;
7
8
/**
9
 * Validation Service.
10
 */
11
abstract class ValidationService
12
{
13
    /**
14
     * Validate and sanitize a username.
15
     *
16
     * @param string $name
17
     * @return string
18
     * @throws \Exception
19
     */
20
    protected static function validateName($name)
21
    {
22
        if (!v::alnum()->length(2, 100)->validate($name)) {
23
            throw new \Exception(Messages::USER_NAME_INVALID, 400);
24
        }
25
26
        return $name;
27
    }
28
29
    /**
30
     * Validate and sanitize a email address.
31
     *
32
     * @param string $emailValue
33
     * @return string
34
     * @throws \Exception
35
     */
36
    protected static function validateEmail($emailValue)
37
    {
38
        $email = filter_var($emailValue, FILTER_SANITIZE_EMAIL);
39
        if (!v::email()->validate($email)) {
40
            throw new \Exception(Messages::USER_EMAIL_INVALID, 400);
41
        }
42
43
        return $email;
44
    }
45
46
    /**
47
     * Validate and sanitize a task name.
48
     *
49
     * @param string $name
50
     * @return string
51
     * @throws \Exception
52
     */
53
    protected static function validateTaskName($name)
54
    {
55
        if (!v::alnum()->length(2, 100)->validate($name)) {
56
            throw new \Exception(Messages::TASK_NAME_INVALID, 400);
57
        }
58
59
        return $name;
60
    }
61
62
    /**
63
     * Validate and sanitize a task status.
64
     *
65
     * @param int $status
66
     * @return string
67
     * @throws \Exception
68
     */
69
    protected static function validateStatus($status)
70
    {
71
        if (!v::numeric()->between(0, 1)->validate($status)) {
72
            throw new \Exception(Messages::TASK_STATUS_INVALID, 400);
73
        }
74
75
        return $status;
76
    }
77
78
    /**
79
     * Validate and sanitize input data when create new user.
80
     *
81
     * @param array $input
82
     * @return string
83
     * @throws \Exception
84
     */
85 View Code Duplication
    public static function validateInputOnCreateUser($input)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
    {
87
        if (!isset($input['name'])) {
88
            throw new \Exception(Messages::USER_NAME_REQUIRED, 400);
89
        }
90
        $name = self::validateName($input['name']);
91
        $email = null;
92
        if (isset($input['email'])) {
93
            $email = self::validateEmail($input['email']);
94
        }
95
96
        return ['name' => $name, 'email' => $email];
97
    }
98
99
    /**
100
     * Validate and sanitize input data when update a user.
101
     *
102
     * @param array $input
103
     * @param object $user
104
     * @return string
105
     * @throws \Exception
106
     */
107 View Code Duplication
    public static function validateInputOnUpdateUser($input, $user)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109
        if (!isset($input['name']) && !isset($input['email'])) {
110
            throw new \Exception(Messages::USER_INFO_REQUIRED, 400);
111
        }
112
        $name = $user->name;
113
        if (isset($input['name'])) {
114
            $name = self::validateName($input['name']);
115
        }
116
        $email = $user->email;
117
        if (isset($input['email'])) {
118
            $email = self::validateEmail($input['email']);
119
        }
120
121
        return ['name' => $name, 'email' => $email];
122
    }
123
124
    /**
125
     * Validate and sanitize input data when create new task.
126
     *
127
     * @param array $input
128
     * @return string
129
     * @throws \Exception
130
     */
131 View Code Duplication
    public static function validateInputOnCreateTask($input)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133
        if (empty($input['task'])) {
134
            throw new \Exception(Messages::TASK_NAME_REQUIRED, 400);
135
        }
136
        $task = self::validateTaskName($input['task']);
137
        $status = 0;
138
        if (isset($input['status'])) {
139
            $status = self::validateStatus($input['status']);
140
        }
141
142
        return ['task' => $task, 'status' => $status];
143
    }
144
145
    /**
146
     * Validate and sanitize input data when update a task.
147
     *
148
     * @param array $input
149
     * @param object $task
150
     * @return string
151
     * @throws \Exception
152
     */
153 View Code Duplication
    public static function validateInputOnUpdateTask($input, $task)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
    {
155
        if (!isset($input['task']) && !isset($input['status'])) {
156
            throw new \Exception(Messages::TASK_INFO_REQUIRED, 400);
157
        }
158
        $name = $task->task;
159
        if (isset($input['task'])) {
160
            $name = self::validateTaskName($input['task']);
161
        }
162
        $status = $task->status;
163
        if (isset($input['status'])) {
164
            $status = self::validateStatus($input['status']);
165
        }
166
167
        return ['task' => $name, 'status' => $status];
168
    }
169
}
170