hasPublicEmailProviderDomain()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace EmailDomain\Eloquent;
4
5
use EmailDomain\Facades\EmailDomainChecker;
6
use Illuminate\Support\Str;
7
8
trait HasEmailDomainChecker
9
{
10
11
    /**
12
     * Check is email has public provider.
13
     *
14
     * @param string|null $email
15
     *
16
     * @return bool
17
     */
18 1
    public function hasPublicEmailProviderDomain(?string $email = null): bool
19
    {
20 1
        $email = $this->getEmailProviderDomain($email);
21
22 1
        return $email ? EmailDomainChecker::usePublicProviderDomainsFile()->isDomainInList($email) : false;
23
    }
24
25
    /**
26
     * Get email provider domain.
27
     *
28
     * @param string|null $email
29
     *
30
     * @return string
31
     */
32 2
    public function getEmailProviderDomain(?string $email = null): string
33
    {
34 2
        $email = $email ?? $this->{$this->emailKeyForDomainChecker()};
35 2
        if ($email = filter_var($email, FILTER_VALIDATE_EMAIL)) {
36 2
            return Str::afterLast($email, '@');
37
        }
38
39 2
        return '';
40
    }
41
42
    /**
43
     * Field key name.
44
     *
45
     * @return string
46
     */
47 3
    public function emailKeyForDomainChecker(): string
48
    {
49 3
        return property_exists($this, 'emailKeyForDomainChecker') ? $this->emailKeyForDomainChecker : 'email';
50
    }
51
}
52