Passed
Push — master ( dd780e...61b14e )
by John
06:10
created

EmailValidator::getGmailAddressWithoutPlus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EmailValidator;
6
7
use EmailValidator\Validator\BannedListValidator;
8
use EmailValidator\Validator\BasicValidator;
9
use EmailValidator\Validator\DisposableEmailValidator;
10
use EmailValidator\Validator\FreeEmailValidator;
11
use EmailValidator\Validator\MxValidator;
12
13
class EmailValidator
14
{
15
    public const NO_ERROR = 0;
16
17
    public const FAIL_BASIC = 1;
18
19
    public const FAIL_MX_RECORD = 2;
20
21
    public const FAIL_BANNED_DOMAIN = 3;
22
23
    public const FAIL_DISPOSABLE_DOMAIN = 4;
24
25
    public const FAIL_FREE_PROVIDER = 5;
26
27
    /**
28
     * @var BasicValidator
29
     */
30
    private $basicValidator;
31
32
    /**
33
     * @var MxValidator
34
     */
35
    private $mxValidator;
36
37
    /**
38
     * @var BannedListValidator
39
     */
40
    private $bannedListValidator;
41
42
    /**
43
     * @var DisposableEmailValidator
44
     */
45
    private $disposableEmailValidator;
46
47
    /**
48
     * @var FreeEmailValidator
49
     */
50
    private $freeEmailValidator;
51
52
    /**
53
     * @var int
54
     */
55
    private $reason;
56
57
    /**
58
     * @var EmailAddress
59
     * @since 1.1.0
60
     */
61
    private $emailAddress;
62
63
    public function __construct(array $config = [])
64
    {
65
        $this->reason = self::NO_ERROR;
66
67
        $policy = new Policy($config);
68
69
        $this->mxValidator = new MxValidator($policy);
70
        $this->basicValidator = new BasicValidator($policy);
71
        $this->bannedListValidator = new BannedListValidator($policy);
72
        $this->disposableEmailValidator = new DisposableEmailValidator($policy);
73
        $this->freeEmailValidator = new FreeEmailValidator($policy);
74
    }
75
76
    /**
77
     * Validate an email address by the rules set forth in the Policy
78
     *
79
     * @param string $email
80
     * @return bool
81
     */
82
    public function validate(string $email): bool
83
    {
84
        $this->resetErrorCode();
85
86
        $this->emailAddress = new EmailAddress($email);
87
88
        if (!$this->basicValidator->validate($this->emailAddress)) {
89
            $this->reason = self::FAIL_BASIC;
90
        } elseif (!$this->mxValidator->validate($this->emailAddress)) {
91
            $this->reason = self::FAIL_MX_RECORD;
92
        } elseif (!$this->bannedListValidator->validate($this->emailAddress)) {
93
            $this->reason = self::FAIL_BANNED_DOMAIN;
94
        } elseif (!$this->disposableEmailValidator->validate($this->emailAddress)) {
95
            $this->reason = self::FAIL_DISPOSABLE_DOMAIN;
96
        } elseif (!$this->freeEmailValidator->validate($this->emailAddress)) {
97
            $this->reason = self::FAIL_FREE_PROVIDER;
98
        }
99
100
        return $this->reason === self::NO_ERROR;
101
    }
102
103
    /**
104
     * Returns the error code constant value for invalid email addresses.
105
     *
106
     * For use by integrating systems to create their own error messages.
107
     *
108
     * @since 1.0.1
109
     * @return int
110
     */
111
    public function getErrorCode(): int
112
    {
113
        return $this->reason;
114
    }
115
116
    /**
117
     * Returns an error message for invalid email addresses
118
     *
119
     * @return string
120
     */
121
    public function getErrorReason(): string
122
    {
123
        switch ($this->reason) {
124
            case self::FAIL_BASIC:
125
                $msg = 'Invalid format';
126
                break;
127
            case self::FAIL_MX_RECORD:
128
                $msg = 'Domain does not accept email';
129
                break;
130
            case self::FAIL_BANNED_DOMAIN:
131
                $msg = 'Domain is banned';
132
                break;
133
            case self::FAIL_DISPOSABLE_DOMAIN:
134
                $msg = 'Domain is used by disposable email providers';
135
                break;
136
            case self::FAIL_FREE_PROVIDER:
137
                $msg = 'Domain is used by free email providers';
138
                break;
139
            case self::NO_ERROR:
140
            default:
141
                $msg = '';
142
        }
143
144
        return $msg;
145
    }
146
147
    /**
148
     * Resets the error code so each validation starts off defaulting to "valid"
149
     *
150
     * @since 1.0.2
151
     * @return void
152
     */
153
    private function resetErrorCode(): void
154
    {
155
        $this->reason = self::NO_ERROR;
156
    }
157
158
    /**
159
     * Determines if a gmail account is using the "plus trick".
160
     *
161
     * @codeCoverageIgnore
162
     * @since 1.1.0
163
     * @return bool
164
     */
165
    public function isGmailWithPlusChar(): bool
166
    {
167
        return $this->emailAddress->isGmailWithPlusChar();
168
    }
169
170
    /**
171
     * Returns a gmail address with the "plus trick" portion of the email address.
172
     *
173
     * @codeCoverageIgnore
174
     * @since 1.1.0
175
     * @return string
176
     */
177
    public function getGmailAddressWithoutPlus(): string
178
    {
179
        return $this->emailAddress->getGmailAddressWithoutPlus();
180
    }
181
}
182