Completed
Push — master ( 260563...208a75 )
by Mauro
03:00
created

UserValidation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 7
c 4
b 0
f 0
lcom 1
cbo 2
dl 0
loc 43
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateInputOnCreateUser() 0 13 3
A validateNameOnUpdateUser() 0 9 2
A validateEmailOnUpdateUser() 0 9 2
1
<?php
2
3
namespace App\Validation;
4
5
use App\Exception\UserException;
6
7
/**
8
 * User Validation.
9
 */
10
abstract class UserValidation extends BaseValidation
11
{
12
    /**
13
     * Validate and sanitize input data when create new user.
14
     *
15
     * @param array|object|null $input
16
     * @return array
17
     * @throws \Exception
18
     */
19
    public static function validateInputOnCreateUser($input)
20
    {
21
        if (!isset($input['name'])) {
22
            throw new UserException(UserException::USER_NAME_REQUIRED, 400);
23
        }
24
        $name = self::validateName($input['name']);
25
        $email = null;
26
        if (isset($input['email'])) {
27
            $email = self::validateEmail($input['email']);
28
        }
29
30
        return ['name' => $name, 'email' => $email];
31
    }
32
33
    public static function validateNameOnUpdateUser($input, $user)
34
    {
35
        $name = $user->name;
36
        if (isset($input['name'])) {
37
            $name = self::validateName($input['name']);
38
        }
39
40
        return $name;
41
    }
42
43
    public static function validateEmailOnUpdateUser($input, $user)
44
    {
45
        $email = $user->email;
46
        if (isset($input['email'])) {
47
            $email = self::validateEmail($input['email']);
48
        }
49
50
        return $email;
51
    }
52
}
53