TestController::withCoffeeCodeDataLayer()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 21
nc 2
nop 1
dl 0
loc 30
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
namespace Example\Controllers;
4
5
use Example\Models\User;
6
use GiovanniALO\DataValidation\Validation;
7
8
class TestController
9
{
10
    public function withCoffeeCodeRouter(): void
11
    {
12
        Validation::validate([
13
            'first_name' => ['required', 'string', 'min:2', 'max:20'],
14
            'last_name' => ['required', 'string', 'min:2', 'max:40'],
15
            'email' => ['required', 'string', 'email'],
16
            'password' => ['required', 'string', 'min:8', 'max:40', 'confirmation'],
17
        ]);
18
19
        echo 'Ok';
20
    }
21
22
    public function withCoffeeCodeDataLayer(array $data): void
23
    {
24
        Validation::validate([
25
            'first_name' => ['required', 'string', 'min:2', 'max:20'],
26
            'last_name' => ['required', 'string', 'min:2', 'max:40'],
27
            'email' => ['required', 'string', 'email', 'unique:'.User::class.',email'],
28
            'password' => ['required', 'string', 'min:8', 'max:40', 'confirmation'],
29
        ]);
30
31
        $user = new User();
32
        $user->first_name = $data['first_name'];
33
        $user->last_name = $data['last_name'];
34
        $user->email = $data['email'];
35
        $user->password = password_hash($data['password'], PASSWORD_DEFAULT, ['cost' => 12]);
36
        $saved = $user->save();
37
38
        if (!$saved) {
39
            http_response_code(500);
40
41
            echo json_encode([
42
                'message' => 'Erro ao cadastrar usuário.',
43
                'trace' => $user->fail()->getMessage(),
44
            ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
45
46
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
47
        }
48
49
        echo json_encode([
50
            'message' => 'Usuário cadastrado com sucesso.',
51
        ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
52
    }
53
}
54