Completed
Pull Request — master (#1407)
by
unknown
07:22
created

MozillaIspDb::query()   C

Complexity

Conditions 15
Paths 164

Size

Total Lines 53
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 20.6422

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 53
ccs 29
cts 41
cp 0.7073
rs 5.7335
cc 15
eloc 34
nc 164
nop 2
crap 20.6422

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Christoph Wurst <[email protected]>
4
 *
5
 * ownCloud - Mail
6
 *
7
 * This code is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License, version 3,
9
 * as published by the Free Software Foundation.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License, version 3,
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
18
 *
19
 */
20
namespace OCA\Mail\Service\AutoConfig;
21
22
use OCA\Mail\Service\Logger;
23
24
class MozillaIspDb {
25
26
	private $logger;
27
	private $urls = array(
28
		'https://autoconfig.{DOMAIN}/mail/config-v1.1.xml',
29
		'https://{DOMAIN}/.well-known/autoconfig/mail/config-v1.1.xml',
30
		'https://autoconfig.thunderbird.net/v1.1/{DOMAIN}',
31
		// fallback for self-signed certificates (needed?)
32 3
		'http://autoconfig.{DOMAIN}/mail/config-v1.1.xml',
33 3
		'http://{DOMAIN}/.well-known/autoconfig/mail/config-v1.1.xml',
34 3
	);
35
36
	public function __construct(Logger $logger) {
37
		$this->logger = $logger;
38
	}
39
40 3
	private function queryUrl($url) {
41 3
		try {
42 3
			$xml = @simplexml_load_file($url);
43
			if (!is_object($xml) || !$xml->emailProvider) {
44
				return [];
45
			}
46
			$provider = [
47 3
				'displayName' => (string) $xml->emailProvider->displayName,
48
			];
49
			foreach ($xml->emailProvider->children() as $tag => $server) {
50 3
				if (!in_array($tag, ['incomingServer', 'outgoingServer'])) {
51 3
					continue;
52
				}
53
				foreach ($server->attributes() as $name => $value) {
54
					if ($name == 'type') {
55 3
						$type = (string) $value;
56 3
					}
57 3
				}
58 3
				$data = [];
59 3
				foreach ($server as $name => $value) {
60
					foreach ($value->children() as $tag => $val) {
61 3
						$data[$name][$tag] = (string) $val;
62 3
					}
63 3
					if (!isset($data[$name])) {
64 3
						$data[$name] = (string) $value;
65 3
					}
66 3
				}
67 3
				$provider[$type][] = $data;
68 3
			}
69 2
		} catch (Exception $e) {
70 3
			// ignore own not-found exception or xml parsing exceptions
71 3
			unset($e);
72 3
			$provider = [];
73 3
		}
74 3
		return $provider;
75 3
	}
76 3
77 3
	/**
78
	 * @param string $domain
79
	 * @return array
80
	 */
81
	public function query($domain, $tryMx = true) {
82
		$this->logger->debug("MozillaIsbDb: querying <$domain>");
83
		if (strpos($domain, '@') !== false) {
84
			// TODO: use horde mail address parsing instead
85
			list(, $domain) = explode('@', $domain);
86
		}
87
88
		$provider = []
89
		foreach($this->urls as $url) {
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_FOREACH
Loading history...
90
			$url = str_replace("{DOMAIN}", $domain, $url);
91 3
			$this->logger->debug("MozillaIsbDb: querying <$domain> via <$url>");
92
93
			$provider = $this->queryUrl($url);
94
			if (!empty($provider)) {
95
				return $provider;
96
			}
97
		}
98
99
		if ($tryMx && ($dns = dns_get_record($domain, DNS_MX))) {
100
			$domain = $dns[0]['target'];
101
			if (!($provider = $this->query($domain, false))) {
102
				list(, $domain) = explode('.', $domain, 2);
103
				$provider = $this->query($domain, false);
104
			}
105
		}
106
		return $provider;
107
	}
108
109
}
110