Validation   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 43
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A filterUserName() 0 5 3
A checkSize() 0 4 2
A checkWords() 0 6 1
1
<?php
2
3
namespace App\Helpers;
4
5
use App\Singleton;
6
use Illuminate\Support\Facades\Config;
7
8
/**
9
 * Class Validation.
10
 */
11
final class Validation extends Singleton
12
{
13
    /**
14
     * Filter an Username from the Invalid Names Base.
15
     *
16
     * @param string $username
17
     *
18
     * @return bool
19
     */
20
    public function filterUserName(string $username): bool
21
    {
22
        return $this->checkSize($username, 4, 15) && $this->checkWords($username) &&
23
            preg_match('/^[a-zA-Z0-9_\-=?!@:.$]+$/', $username);
24
    }
25
26
    /**
27
     * Check String Size.
28
     *
29
     * @param string $needle
30
     * @param int    $min
31
     * @param int    $max
32
     *
33
     * @return bool
34
     */
35
    public function checkSize(string $needle, int $min, int $max)
36
    {
37
        return strlen($needle) <= $max && strlen($needle) >= $min;
38
    }
39
40
    /**
41
     * Check for Illegal Words.
42
     *
43
     * @param string $needle
44
     *
45
     * @return bool
46
     */
47
    public function checkWords(string $needle): bool
48
    {
49
        return count(array_filter(Config::get('chocolatey.invalid'), function ($illegal) use ($needle) {
50
            return stripos($needle, $illegal) !== false;
51
        })) == 0;
52
    }
53
}
54