SmtpServerDetector   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 16.36 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 9
loc 55
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B detect() 9 30 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * @author Christoph Wurst <[email protected]>
5
 *
6
 * Mail
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\Mail\Service\AutoConfig;
23
24
use OCA\Mail\Db\MailAccount;
25
26
class SmtpServerDetector {
27
28
	/** @var MxRecord */
29
	private $mxRecord;
30
31
	/** @var SmtpConnectivityTester */
32
	private $smtpConnectivityTester;
33
34
	/** @var bool */
35
	private $testSmtp;
36
37
	/**
38
	 * @param MxRecord $mxRecord
39
	 * @param SmtpConnectivityTester $smtpTester
40
	 * @param bool $testSmtp
41
	 */
42
	public function __construct(MxRecord $mxRecord,
43
		SmtpConnectivityTester $smtpTester, $testSmtp) {
44
		$this->mxRecord = $mxRecord;
45
		$this->smtpConnectivityTester = $smtpTester;
46
		$this->testSmtp = $testSmtp;
47
	}
48
49
	public function detect(MailAccount $account, $email, $password) {
50
		if ($this->testSmtp === false) {
51
			return;
52
		}
53
54
		// splitting the email address into user and host part
55
		// TODO: use horde libs for email address parsing
56
		list($user, $host) = explode("@", $email);
57
58
		/*
59
		 * Try to get the mx record for the email address
60
		 */
61
		$mxHosts = $this->mxRecord->query($host);
62 View Code Duplication
		if ($mxHosts) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
			foreach ($mxHosts as $mxHost) {
64
				$result = $this->smtpConnectivityTester->test($account, $mxHost,
65
					[$user, $email], $password);
66
				if ($result) {
67
					return;
68
				}
69
			}
70
		}
71
72
		/*
73
		 * IMAP login with full email address as user
74
		 * works for a lot of providers (e.g. Google Mail)
75
		 */
76
		$this->smtpConnectivityTester->test($account, $host, [$user, $email],
77
			$password, true);
78
	}
79
80
}
81