Completed
Push — master ( e8bb65...b9989c )
by Mauro
16:58
created

BaseService   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 102
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateInputOnCreateUser() 0 13 3
B validateInputOnUpdateUser() 0 16 5
A validateName() 0 8 2
A validateEmail() 0 9 2
1
<?php
2
3
namespace App\Service;
4
5
use Respect\Validation\Validator as v;
6
7
/**
8
 * Base Service Class.
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_STATUS_INVALID = 'El estado ingresado es incorrecto.';
26
27
    protected $logger;
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 a username.
81
     *
82
     * @param string $name
83
     * @return string
84
     * @throws \Exception
85
     */
86
    protected function validateName($name)
87
    {
88
        if (!v::alnum()->length(2, 100)->validate($name)) {
89
            throw new \Exception(self::USER_NAME_INVALID, 400);
90
        }
91
92
        return $name;
93
    }
94
95
    /**
96
     * Validate and sanitize a email address.
97
     *
98
     * @param string $emailValue
99
     * @return string
100
     * @throws \Exception
101
     */
102
    protected function validateEmail($emailValue)
103
    {
104
        $email = filter_var($emailValue, FILTER_SANITIZE_EMAIL);
105
        if (!v::email()->validate($email)) {
106
            throw new \Exception(self::USER_EMAIL_INVALID, 400);
107
        }
108
109
        return $email;
110
    }
111
}
112