Completed
Push — master ( 7468db...994a67 )
by Mauro
02:19
created

BaseService::validateTaskName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 8
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace App\Service;
4
5
use Respect\Validation\Validator as v;
6
7
/**
8
 * Base Service.
9
 */
10
abstract class BaseService
11
{
12
    const USER_NOT_FOUND = 'El usuario solicitado no existe.';
13
    const USER_NAME_NOT_FOUND = 'No se encontraron usuarios con ese nombre.';
14
    const USER_NAME_REQUIRED = 'Ingrese el nombre del usuario.';
15
    const USER_INFO_REQUIRED = 'Ingrese los datos a actualizar del usuario.';
16
    const USER_DELETED = 'El usuario fue eliminado correctamente.';
17
    const USER_NAME_INVALID = 'El nombre ingresado es incorrecto.';
18
    const USER_EMAIL_INVALID = 'El email ingresado es incorrecto.';
19
20
    const TASK_NOT_FOUND = 'La tarea solicitada no existe.';
21
    const TASK_NAME_NOT_FOUND = 'No se encontraron tareas con ese nombre.';
22
    const TASK_NAME_REQUIRED = 'Ingrese el nombre de la tarea.';
23
    const TASK_INFO_REQUIRED = 'Ingrese los datos a actualizar de la tarea.';
24
    const TASK_DELETED = 'La tarea fue eliminada correctamente.';
25
    const TASK_NAME_INVALID = 'La tarea ingresada es incorrecta.';
26
    const TASK_STATUS_INVALID = 'El estado ingresado es incorrecto.';
27
28
    protected $database;
29
    protected $request;
30
    protected $response;
31
    protected $args;
32
33
    /**
34
     * Validate and sanitize input data when create new user.
35
     *
36
     * @param array $input
37
     * @return string
38
     * @throws \Exception
39
     */
40
    protected function validateInputOnCreateUser($input)
41
    {
42
        if (!isset($input['name'])) {
43
            throw new \Exception(self::USER_NAME_REQUIRED, 400);
44
        }
45
        $name = $this->validateName($input['name']);
46
        $email = null;
47
        if (isset($input['email'])) {
48
            $email = $this->validateEmail($input['email']);
49
        }
50
51
        return ['name' => $name, 'email' => $email];
52
    }
53
54
    /**
55
     * Validate and sanitize input data when update a user.
56
     *
57
     * @param array $input
58
     * @param object $user
59
     * @return string
60
     * @throws \Exception
61
     */
62
    protected function validateInputOnUpdateUser($input, $user)
63
    {
64
        if (!isset($input['name']) && !isset($input['email'])) {
65
            throw new \Exception(self::USER_INFO_REQUIRED, 400);
66
        }
67
        $name = $user->name;
68
        if (isset($input['name'])) {
69
            $name = $this->validateName($input['name']);
70
        }
71
        $email = $user->email;
72
        if (isset($input['email'])) {
73
            $email = $this->validateEmail($input['email']);
74
        }
75
76
        return ['name' => $name, 'email' => $email];
77
    }
78
79
    /**
80
     * Validate and sanitize input data when create new user.
81
     *
82
     * @param array $input
83
     * @return string
84
     * @throws \Exception
85
     */
86
    protected function validateInputOnCreateTask($input)
87
    {
88
        if (empty($input['task'])) {
89
            throw new \Exception(self::TASK_NAME_REQUIRED, 400);
90
        }
91
        $task = $this->validateTaskName($input['task']);
92
        $status = isset($input['status']) ? $input['status'] : 0;
93
        if (!v::numeric()->between(0, 1)->validate($status)) {
94
            throw new \Exception(self::TASK_STATUS_INVALID, 400);
95
        }
96
97
        return ['task' => $task, 'status' => $status];
98
    }
99
100
    /**
101
     * Validate and sanitize a username.
102
     *
103
     * @param string $name
104
     * @return string
105
     * @throws \Exception
106
     */
107 View Code Duplication
    protected function validateName($name)
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 (!v::alnum()->length(2, 100)->validate($name)) {
110
            throw new \Exception(self::USER_NAME_INVALID, 400);
111
        }
112
113
        return $name;
114
    }
115
116
    /**
117
     * Validate and sanitize a email address.
118
     *
119
     * @param string $emailValue
120
     * @return string
121
     * @throws \Exception
122
     */
123
    protected function validateEmail($emailValue)
124
    {
125
        $email = filter_var($emailValue, FILTER_SANITIZE_EMAIL);
126
        if (!v::email()->validate($email)) {
127
            throw new \Exception(self::USER_EMAIL_INVALID, 400);
128
        }
129
130
        return $email;
131
    }
132
133 View Code Duplication
    protected function validateTaskName($name)
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...
134
    {
135
        if (!v::alnum()->length(2, 100)->validate($name)) {
136
            throw new \Exception(self::TASK_NAME_INVALID, 400);
137
        }
138
139
        return $name;
140
    }
141
}
142