ImapConnectivityTester::test()   D
last analyzed

Complexity

Conditions 9
Paths 8

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 4.909
c 0
b 0
f 0
cc 9
eloc 22
nc 8
nop 5
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
namespace OCA\Mail\Service\AutoConfig;
22
23
use OCA\Mail\Service\Logger;
24
25
class ImapConnectivityTester extends ConnectivityTester {
26
27
	/**
28
	 * @var ImapConnector
29
	 */
30
	private $imapConnector;
31
32
	/**
33
	 * @var string
34
	 */
35
	private $userId;
36
37
	public function __construct(ImapConnector $imapConnector, Logger $logger,
38
		$UserId) {
39
		parent::__construct($logger);
40
		$this->imapConnector = $imapConnector;
41
		$this->userId = $UserId;
42
	}
43
44
	/**
45
	 * @param $email
46
	 * @param $host
47
	 * @param $users
48
	 * @param $password
49
	 * @param $name
50
	 * @return \OCA\Mail\Db\MailAccount|null
51
	 */
52
	public function test($email, $host, $users, $password, $name) {
53
		if (!is_array($users)) {
54
			$users = [$users];
55
		}
56
57
		$ports = [143, 585, 993];
58
		$encryptionProtocols = ['ssl', 'tls', null];
59
		$hostPrefixes = ['', 'imap.'];
60
		foreach ($hostPrefixes as $hostPrefix) {
61
			$url = $hostPrefix . $host;
62
			if (gethostbyname($url) === $url) {
63
				continue;
64
			}
65
			foreach ($ports as $port) {
66
				if (!$this->canConnect($url, $port)) {
67
					continue;
68
				}
69
				foreach ($encryptionProtocols as $encryptionProtocol) {
70
					foreach ($users as $user) {
71
						try {
72
							return $this->imapConnector->connect($email, $password, $name, $host,
73
									$port, $encryptionProtocol, $user);
74
						} catch (\Horde_Imap_Client_Exception $e) {
0 ignored issues
show
Bug introduced by
The class Horde_Imap_Client_Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
75
							$error = $e->getMessage();
76
							$this->logger->info("Test-Account-Failed: $this->userId, $url, $port, $user, $encryptionProtocol -> $error");
77
						}
78
					}
79
				}
80
			}
81
		}
82
		return null;
83
	}
84
85
}
86