Passed
Push — master ( 6f0dcd...27eb8a )
by Propa
04:30
created

DisposableDomains::getDomains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Propaganistas\LaravelDisposableEmail;
4
5
use ErrorException;
6
use Illuminate\Contracts\Cache\Repository as Cache;
7
use Illuminate\Support\Arr;
8
use Propaganistas\LaravelDisposableEmail\Traits\ParsesJson;
9
10
class DisposableDomains
11
{
12
    use ParsesJson;
13
14
    /**
15
     * The storage path to retrieve from and save to.
16
     *
17
     * @var string
18
     */
19
    protected $storagePath;
20
21
    /**
22
     * Array of retrieved disposable domains.
23
     *
24
     * @var array
25
     */
26
    protected $domains = [];
27
28
    /**
29
     * The cache repository.
30
     *
31
     * @var \Illuminate\Contracts\Cache\Repository|null
32
     */
33
    protected $cache;
34
35
    /**
36
     * The cache key.
37
     *
38
     * @var string
39
     */
40
    protected $cacheKey;
41
42
    /**
43
     * Disposable constructor.
44
     *
45
     * @param \Illuminate\Contracts\Cache\Repository|null $cache
46
     */
47 63
    public function __construct(Cache $cache = null)
48
    {
49 63
        $this->cache = $cache;
50 63
    }
51
52
    /**
53
     * Loads the domains from cache/storage into the class.
54
     *
55
     * @return $this
56
     */
57 63
    public function bootstrap()
58
    {
59 63
        if ($this->cache) {
60 54
            $data = $inCache = $this->getFromCache();
61
        }
62
63 63
        if (! isset($data) || ! $data) {
64 63
            $data = $this->getFromStorage();
65
        }
66
67 63
        if ($this->cache && (! isset($inCache) || ! $inCache)) {
68 54
            $this->cache->forever($this->getCacheKey(), $data);
69
        }
70
71 63
        $this->domains = $data;
72
73 63
        return $this;
74
    }
75
76
    /**
77
     * Get the domains from cache.
78
     *
79
     * @return array|null
80
     */
81 54
    protected function getFromCache()
82
    {
83 54
        return $this->cache->get($this->getCacheKey());
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

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

83
        return $this->cache->/** @scrutinizer ignore-call */ get($this->getCacheKey());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
    }
85
86
    /**
87
     * Get the domains from storage, or if non-existent, from the package.
88
     *
89
     * @return array
90
     */
91 63
    protected function getFromStorage()
92
    {
93
        try {
94 63
            if ($data = $this->parseJson(file_get_contents($this->getStoragePath()))) {
95 12
                return $data;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $data also could return the type true which is incompatible with the documented return type array.
Loading history...
96
            }
97 51
        } catch (ErrorException $e) {
98
            // File does not exist or could not be opened.
99
        }
100
101
        // Fall back to the list provided by the package.
102 60
        return $this->parseJson(file_get_contents(__DIR__.'/../domains.json'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->parseJson(... . '/../domains.json')) also could return the type boolean which is incompatible with the documented return type array.
Loading history...
103
    }
104
105
    /**
106
     * Checks whether the given email address' domain matches a disposable email service.
107
     *
108
     * @param string $email
109
     * @return bool
110
     */
111 15
    public function isDisposable($email)
112
    {
113 15
        if ($domain = Arr::get(explode('@', $email, 2), 1)) {
114 15
            return in_array($domain, $this->domains);
115
        }
116
117
        // Just ignore this validator if the value doesn't even resemble an email or domain.
118
        return false;
119
    }
120
121
    /**
122
     * Checks whether the given email address' domain doesn't match a disposable email service.
123
     *
124
     * @param string $email
125
     * @return bool
126
     */
127 15
    public function isNotDisposable($email)
128
    {
129 15
        return ! $this->isDisposable($email);
130
    }
131
132
    /**
133
     * Alias of "isNotDisposable".
134
     *
135
     * @param string $email
136
     * @return bool
137
     */
138 3
    public function isIndisposable($email)
139
    {
140 3
        return $this->isNotDisposable($email);
141
    }
142
143
    /**
144
     * Flushes the cache if applicable.
145
     */
146 63
    public function flushCache()
147
    {
148 63
        if ($this->cache) {
149 54
            $this->cache->forget($this->getCacheKey());
150
        }
151 63
    }
152
153
    /**
154
     * Flushes the source's list if applicable.
155
     */
156 63
    public function flushSource()
157
    {
158 63
        if (is_file($this->getStoragePath())) {
159 15
            @unlink($this->getStoragePath());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

159
            /** @scrutinizer ignore-unhandled */ @unlink($this->getStoragePath());

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
160
        }
161 63
    }
162
163
    /**
164
     * Get the list of disposable domains.
165
     *
166
     * @return array
167
     */
168 12
    public function getDomains()
169
    {
170 12
        return $this->domains;
171
    }
172
173
    /**
174
     * Get the storage path.
175
     *
176
     * @return string
177
     */
178 63
    public function getStoragePath()
179
    {
180 63
        return $this->storagePath;
181
    }
182
183
    /**
184
     * Set the storage path.
185
     *
186
     * @param string $path
187
     * @return $this
188
     */
189 63
    public function setStoragePath($path)
190
    {
191 63
        $this->storagePath = $path;
192
193 63
        return $this;
194
    }
195
196
    /**
197
     * Get the cache key.
198
     *
199
     * @return string
200
     */
201 57
    public function getCacheKey()
202
    {
203 57
        return $this->cacheKey;
204
    }
205
206
    /**
207
     * Set the cache key.
208
     *
209
     * @param string $key
210
     * @return $this
211
     */
212 63
    public function setCacheKey($key)
213
    {
214 63
        $this->cacheKey = $key;
215
216 63
        return $this;
217
    }
218
}