EmailDomainChecker   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 31
c 1
b 0
f 0
dl 0
loc 129
ccs 38
cts 38
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 13 4
A getDomainsFilePath() 0 3 1
A isEmailDomainInList() 0 3 1
A __construct() 0 3 1
A getStoragePath() 0 3 1
A isDomainInList() 0 11 3
A absoluteFilePath() 0 9 2
A domainsList() 0 9 2
A setDomainsFilePath() 0 5 1
A setStoragePath() 0 5 1
1
<?php
2
3
namespace EmailDomain;
4
5
use EmailDomain\Exceptions\EmailDomainException;
6
use Illuminate\Support\Str;
7
8
/**
9
 * @method static usePublicProviderDomainsFile()
10
 */
11
class EmailDomainChecker
12
{
13
    protected string $storagePath = '';
14
15
    protected string $domainsFilePath = '';
16
17 9
    public function __construct(?string $storagePath = null)
18
    {
19 9
        $this->storagePath = $storagePath ?? config('email-domain.storage_path');
20
    }
21
22
    /**
23
     * @param string $storagePath
24
     *
25
     * @return $this
26
     */
27 2
    public function setStoragePath(string $storagePath): static
28
    {
29 2
        $this->storagePath = $storagePath;
30
31 2
        return $this;
32
    }
33
34
    /**
35
     * @return string
36
     */
37 1
    public function getStoragePath(): string
38
    {
39 1
        return $this->storagePath;
40
    }
41
42
    /**
43
     * @param string $domainsFilePath
44
     *
45
     * @return $this
46
     */
47 7
    public function setDomainsFilePath(string $domainsFilePath): static
48
    {
49 7
        $this->domainsFilePath = $domainsFilePath;
50
51 7
        return $this;
52
    }
53
54
    /**
55
     * @return string
56
     */
57 2
    public function getDomainsFilePath(): string
58
    {
59 2
        return $this->domainsFilePath;
60
    }
61
62
63
    /**
64
     * @param string|null $domainsFilePath
65
     *
66
     * @return string
67
     */
68 6
    public function absoluteFilePath(?string $domainsFilePath = null): string
69
    {
70 6
        $domainsFilePath = $domainsFilePath ?? $this->domainsFilePath;
71
72 6
        if (empty($domainsFilePath)) {
73 1
            throw new EmailDomainException('Domains list file path is empty');
74
        }
75
76 5
        return rtrim($this->storagePath, '/') . '/' . ltrim($domainsFilePath, '/');
77
    }
78
79
    /**
80
     * @param string|null $domainsFilePath
81
     *
82
     * @return string
83
     * @throws EmailDomainException
84
     */
85 5
    public function domainsList(?string $domainsFilePath = null): string
86
    {
87 5
        $domainsFilePath = $this->absoluteFilePath($domainsFilePath);
88
89 4
        if (!file_exists($domainsFilePath)) {
90 1
            throw new EmailDomainException("Domains list file path is wrong [{$domainsFilePath}]");
91
        }
92
93 3
        return file_get_contents($domainsFilePath);
94
    }
95
96
    /**
97
     * @param string $domain
98
     * @param string|null $domainsFilePath
99
     *
100
     * @return bool
101
     * @throws EmailDomainException
102
     */
103 2
    public function isDomainInList(string $domain, ?string $domainsFilePath = null): bool
104
    {
105 2
        if (config('email-domain.checker.filter_domain', true)) {
106 2
            if (!$domain = filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) {
107 1
                return false;
108
            }
109
        }
110
111 2
        $domainRegex = str_replace('<DOMAIN>', preg_quote($domain), config('email-domain.checker.regex_pattern'));
112
113 2
        return (bool) preg_match($domainRegex, $this->domainsList($domainsFilePath));
114
    }
115
116
    /**
117
     * @param string $email
118
     * @param string|null $domainsFilePath
119
     * @return bool
120
     * @throws EmailDomainException
121
     */
122 1
    public function isEmailDomainInList(string $email, ?string $domainsFilePath = null): bool
123
    {
124 1
        return $this->isDomainInList(Str::afterLast($email, '@'), $domainsFilePath);
125
    }
126
127 4
    public function __call(string $name, array $arguments)
128
    {
129 4
        if (Str::startsWith($name, 'use')
130 4
             && Str::endsWith($name, 'File')) {
131 4
            $group = Str::snake(Str::beforeLast(Str::after($name, 'use'), 'File'));
132 4
            if ($filePath = config("email-domain.domains_group_files.{$group}")) {
133 4
                $this->setDomainsFilePath($filePath);
134
135 4
                return $this;
136
            }
137
        }
138
139 1
        throw new \BadMethodCallException("Method [{$name}] not exists.");
140
    }
141
}
142