1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of EmailChecker. |
5
|
|
|
* |
6
|
|
|
* (c) Corrado Ronci <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace sorciulus\EmailChecker; |
13
|
|
|
|
14
|
|
|
use GuzzleHttp\Client; |
15
|
|
|
use GuzzleHttp\Exception\TransferException; |
16
|
|
|
use GuzzleHttp\Exception\ClientException; |
17
|
|
|
use GuzzleHttp\Exception\RequestException; |
18
|
|
|
use GuzzleHttp\Exception\ServerException; |
19
|
|
|
use sorciulus\EmailChecker\Exception\DisponsableException; |
20
|
|
|
/** |
21
|
|
|
* Class to check if email is disponsable |
22
|
|
|
* through https://open.kickbox.io API |
23
|
|
|
*/ |
24
|
|
|
class DisponsableChecker |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Client HTTP |
28
|
|
|
* |
29
|
|
|
* @var GuzzleHttp\Client |
|
|
|
|
30
|
|
|
*/ |
31
|
|
|
private $client; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* This setting allows you to disable |
35
|
|
|
* disponsable email checker. If FALSE |
36
|
|
|
* the function isDisponsable return NULL |
37
|
|
|
* |
38
|
|
|
* @var boolean |
39
|
|
|
*/ |
40
|
|
|
private $enable = TRUE; |
41
|
|
|
|
42
|
|
|
public function __construct() |
43
|
|
|
{ |
44
|
|
|
$this->client = new Client([ |
|
|
|
|
45
|
|
|
"base_uri" => "https://open.kickbox.io", |
46
|
|
|
"timeout" => 2.0, |
47
|
|
|
]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Check if service is enabled |
52
|
|
|
* |
53
|
|
|
* @return boolean |
54
|
|
|
*/ |
55
|
|
|
public function isEnable() |
56
|
|
|
{ |
57
|
|
|
return $this->enable; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Call Kickbox API and return the value |
62
|
|
|
* |
63
|
|
|
* @param string $domain |
64
|
|
|
* @return boolean |
65
|
|
|
* @throws DisponsableException |
66
|
|
|
*/ |
67
|
|
|
public function isDisponsable($domain) |
68
|
|
|
{ |
69
|
|
|
if (!$this->enable) { |
70
|
|
|
throw new DisponsableException("DisponsableChecker is disable"); |
71
|
|
|
} |
72
|
|
|
try { |
73
|
|
|
$response = $this->client->get("/v1/disposable/" . $domain); |
74
|
|
|
$body = json_decode((string)$response->getBody()); |
75
|
|
|
if (property_exists($body, "disposable")) { |
76
|
|
|
return $body->disposable; |
77
|
|
|
} else { |
78
|
|
|
throw new DisponsableException("Disponsable service unknown response"); |
79
|
|
|
} |
80
|
|
|
} catch (TransferException $e) { |
81
|
|
|
throw new DisponsableException($e->getMessage()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
} |