Completed
Push — master ( 3b79d8...b88358 )
by Mauro
02:24
created

UserValidation   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 48
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateInputOnCreateUser() 0 13 3
B validateInputOnUpdateUser() 0 16 5
1
<?php
2
3
namespace App\Validation;
4
5
use App\Message\UserMessage;
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 $input
16
     * @return string
17
     * @throws \Exception
18
     */
19
    public static function validateInputOnCreateUser($input)
20
    {
21
        if (!isset($input['name'])) {
22
            throw new \Exception(UserMessage::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
    /**
34
     * Validate and sanitize input data when update a user.
35
     *
36
     * @param array $input
37
     * @param object $user
38
     * @return string
39
     * @throws \Exception
40
     */
41
    public static function validateInputOnUpdateUser($input, $user)
42
    {
43
        if (!isset($input['name']) && !isset($input['email'])) {
44
            throw new \Exception(UserMessage::USER_INFO_REQUIRED, 400);
45
        }
46
        $name = $user->name;
47
        if (isset($input['name'])) {
48
            $name = self::validateName($input['name']);
49
        }
50
        $email = $user->email;
51
        if (isset($input['email'])) {
52
            $email = self::validateEmail($input['email']);
53
        }
54
55
        return ['name' => $name, 'email' => $email];
56
    }
57
}
58