UniqueBlockerItemValueEmail   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A checkEmail() 0 6 4
A passes() 0 17 5
A message() 0 3 1
1
<?php
2
3
namespace jeremykenedy\LaravelBlocker\App\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use jeremykenedy\LaravelBlocker\App\Models\BlockedType;
7
8
class UniqueBlockerItemValueEmail implements Rule
9
{
10
    private $typeId;
11
12
    /**
13
     * Create a new rule instance.
14
     *
15
     * @return void
16
     */
17
    public function __construct($typeId)
18
    {
19
        $this->typeId = $typeId;
20
    }
21
22
    /**
23
     * Determine if the validation rule passes.
24
     *
25
     * @param string $attribute
26
     * @param mixed  $value
27
     *
28
     * @return bool
29
     */
30
    public function passes($attribute, $value)
31
    {
32
        if ($this->typeId) {
33
            $type = BlockedType::find($this->typeId);
34
35
            if ($type->slug == 'email' || $type->slug == 'user') {
36
                $check = $this->checkEmail($value);
37
38
                if ($check) {
39
                    return $value;
40
                }
41
42
                return false;
43
            }
44
        }
45
46
        return true;
47
    }
48
49
    /**
50
     * Check if value is proper formed email.
51
     *
52
     * @param string $email The email
53
     *
54
     * @return bool
55
     */
56
    public function checkEmail($email)
57
    {
58
        $find1 = strpos($email, '@');
59
        $find2 = strpos($email, '.');
60
61
        return $find1 !== false && $find2 !== false && $find2 > $find1 ? true : false;
62
    }
63
64
    /**
65
     * Get the validation error message.
66
     *
67
     * @return string
68
     */
69
    public function message()
70
    {
71
        return trans('laravelblocker::laravelblocker.validation.email');
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
        return /** @scrutinizer ignore-call */ trans('laravelblocker::laravelblocker.validation.email');
Loading history...
72
    }
73
}
74