EmailChecker::getDisponsableService()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
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 sorciulus\EmailChecker\Exception\EmailCheckerException;
15
use sorciulus\EmailChecker\Exception\MxCheckerException;
16
use sorciulus\EmailChecker\Exception\MxFunctionException;
17
use sorciulus\EmailChecker\MxChecker;
18
use sorciulus\EmailChecker\SmtpChecker;
19
use sorciulus\EmailChecker\ResponseChecker;
20
use sorciulus\EmailChecker\DisponsableChecker;
21
22
/**
23
 * Check Email Class
24
 */
25
class EmailChecker
26
{	
27
28
	/**
29
	 * Email Check
30
	 * @var string
31
	 */
32
	private $email;
33
34
	/**
35
	 * Email to use from Sender
36
	 * @var string
37
	 */
38
	private $sender;
39
40
	/**
41
	 * Sets the stream timeout.
42
	 * @var integer
43
	 */
44
	private $timeout = 10;
45
46
	/**
47
	 * Disponsable Checker
48
	 * 
49
	 * @var EmailChecker\DisponsableChecker
0 ignored issues
show
Bug introduced by
The type sorciulus\EmailChecker\E...cker\DisponsableChecker was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
50
	 */
51
	private $disponsableService;
52
53
	/**
54
	 * @param string $email The email address to check
55
	 * @param string $sender The email address to set for sender
56
	 */
57
	function __construct($email = "", $sender = "")
58
	{
59
		if (!empty($email)) {
60
			$this->setEmail($email);
61
		}
62
		if (!empty($sender)) {
63
			$this->setSender($sender);
64
		}
65
66
		$this->disponsableService = new DisponsableChecker();		
0 ignored issues
show
Documentation Bug introduced by
It seems like new sorciulus\EmailChecker\DisponsableChecker() of type sorciulus\EmailChecker\DisponsableChecker is incompatible with the declared type sorciulus\EmailChecker\E...cker\DisponsableChecker of property $disponsableService.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
67
	}
68
69
	/**
70
	 *	Set Email 
71
	 * 
72
	 * @param string $email The email address to check
73
	 *
74
	 * @throws EmailCheckerException if the email is not valid
75
	 */
76
	public function setEmail($email)
77
	{		
78
		$this->clearEmail();
79
		if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
80
			throw new EmailCheckerException("Email not valid");
81
		}
82
		$this->email = $email;	
83
	}
84
85
	/**
86
	 *	Set Sender 
87
	 * 
88
	 * @param string $sender The email address to set for sender
89
	 *
90
	 * @throws EmailCheckerException if the email is not valid
91
	 */
92
	public function setSender($sender)
93
	{
94
		if (!filter_var($sender, FILTER_VALIDATE_EMAIL)) {
95
			throw new EmailCheckerException("Sender not valid");				
96
		}
97
		$this->sender = $sender;	
98
	}
99
100
	/**
101
	 *  Clear Email address
102
	 */
103
	private function clearEmail()
104
	{
105
		$this->email = null;
106
	}
107
108
	/**
109
	 * Extract domain from set Email
110
	 * 
111
	 * @return string $domain The domain extract from set Email
112
	 *
113
	 * @throws EmailCheckerException if the email setted are invalid or empty
114
	 */
115
	public function getDomain()
116
	{
117
		if (empty($this->getEmail())) {
118
			throw new EmailCheckerException("Email was not empty");				
119
		}
120
		$domain = explode("@", $this->getEmail());				
121
		return end($domain);
122
	}
123
124
	/**
125
    * Get the value of email.
126
    *
127
    * @return string
128
    */
129
	public function getEmail()
130
	{
131
		return $this->email;
132
	}
133
134
	/**
135
    * Get the value of sender.
136
    *
137
    * @return string
138
    */
139
	public function getSender()
140
	{
141
		return $this->sender ?: "[email protected]";
142
	}
143
	
144
	/**
145
    * Gets the Sets the stream timeout.
146
    *
147
    * @return integer
148
    */
149
    public function getTimeout()
150
    {
151
        return $this->timeout;
152
    } 
153
154
    /**
155
    * Sets the Sets the stream timeout.
156
    *
157
    * @param integer $timeout the timeout
158
    *
159
    * @return self
160
    */
161
    public function setTimeout($timeout)
162
    {
163
        if (is_int($timeout)) {
164
        	$this->timeout = $timeout;
165
        }        
166
		return $this;
167
    }	
168
169
    /**
170
    * Gets the Disponsable Checker.
171
    *
172
    * @return EmailChecker\DisponsableChecker
173
    */
174
    public function getDisponsableService()
175
    {
176
        return $this->disponsableService;
177
    }
178
179
	/**
180
    * This function extract the mx record from domain
181
    *
182
    * @return sorciulus\EmailChecker\MxChecker
0 ignored issues
show
Bug introduced by
The type sorciulus\EmailChecker\s...\EmailChecker\MxChecker was not found. Did you mean sorciulus\EmailChecker\MxChecker? If so, make sure to prefix the type with \.
Loading history...
183
    *
184
    * @throws MxCheckerException if domain is not avaible
185
    */
186
	private function checkDomain(){
187
188
		return new MxChecker($this->getDomain());		 
0 ignored issues
show
Bug Best Practice introduced by
The expression return new sorciulus\Ema...ker($this->getDomain()) returns the type sorciulus\EmailChecker\MxChecker which is incompatible with the documented return type sorciulus\EmailChecker\s...\EmailChecker\MxChecker.
Loading history...
189
	}
190
191
	/**
192
    * This function check email is valid from SMTP command
193
    *
194
    * @param  object MxChecker $recordMx
195
    *
196
    * @return object SmtpChecker 
197
    *
198
    * @throws SmtpCheckerException if SMTP return error
199
    */
200
	private function checkSMTP(MxInterface $recordMx)
201
	{
202
		$smtp = new SmtpChecker(
203
			$recordMx,
204
			$this->getSender(),
0 ignored issues
show
Bug introduced by
$this->getSender() of type string is incompatible with the type boolean expected by parameter $sender of sorciulus\EmailChecker\SmtpChecker::__construct(). ( Ignorable by Annotation )

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

204
			/** @scrutinizer ignore-type */ $this->getSender(),
Loading history...
205
			$this->getTimeout()
206
		);
207
		return $smtp->validate($this->getEmail());
208
	}
209
210
	/**
211
	 * This function is a wrapper for two function. 
212
	 * First extract and get MX record from domain email @MxChecker
213
	 * Second call the SMTP server to ask the email are valid @SmtpChecker
214
	 * 
215
	 * @return object ResponseChecker $response
216
	 * 
217
	 * @throws SmtpCheckerException if SMTP return error
218
	 * @throws EmailCheckerException if the email setted are invalid or empty
219
	 */
220
	public function validate()
221
	{	
222
		if (empty($this->getEmail())) {
223
			throw new EmailCheckerException("Email was not empty");	
224
		}	
225
		$recordMx  = $this->checkDomain();
226
		$checkSMTP = $this->checkSMTP($recordMx);
227
		$response  = new ResponseChecker();
228
		$response
229
			->setRecordMx($recordMx)
230
			->setIsValid($checkSMTP->isValid())
231
			->setMessage($checkSMTP->getMessage())
232
			->setCode($checkSMTP->getCode())
233
			->setDebug($checkSMTP->getDebug())
234
		;
235
		if ($this->getDisponsableService()->isEnable()) {
236
			$response->setIsDisponsable(
237
				$this->disponsableService->isDisponsable(
238
					$this->getDomain()
239
				)
240
			);
241
		}
242
		return $response; 	
243
	}
244
}