BaseService::validateTaskName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Service;
6
7
use App\Exception\TaskException;
8
use App\Exception\UserException;
9
use App\Exception\NoteException;
10
use Respect\Validation\Validator as v;
11
12
abstract class BaseService
13
{
14
    protected static function isRedisEnabled(): bool
15
    {
16
        return filter_var(getenv('REDIS_ENABLED'), FILTER_VALIDATE_BOOLEAN);
17
    }
18
19
    protected static function validateUserName(string $name): string
20
    {
21
        if (!v::alnum()->length(2, 100)->validate($name)) {
22
            throw new UserException('Invalid name.', 400);
23
        }
24
25
        return $name;
26
    }
27
28
    protected static function validateEmail(string $emailValue): string
29
    {
30
        $email = filter_var($emailValue, FILTER_SANITIZE_EMAIL);
31
        if (!v::email()->validate($email)) {
32
            throw new UserException('Invalid email', 400);
33
        }
34
35
        return $email;
36
    }
37
38
    protected static function validateTaskName(string $name): string
39
    {
40
        if (!v::length(2, 100)->validate($name)) {
41
            throw new TaskException('Invalid name.', 400);
42
        }
43
44
        return $name;
45
    }
46
47
    protected static function validateTaskStatus(int $status): int
48
    {
49
        if (!v::numeric()->between(0, 1)->validate($status)) {
50
            throw new TaskException('Invalid status', 400);
51
        }
52
53
        return $status;
54
    }
55
56
    protected static function validateNoteName(string $name): string
57
    {
58
        if (!v::length(2, 50)->validate($name)) {
59
            throw new NoteException('The name of the note is invalid.', 400);
60
        }
61
62
        return $name;
63
    }
64
}
65