Completed
Push — master ( d2df27...775f73 )
by Christoph
10:35
created

IspDb::queryUrl()   C

Complexity

Conditions 12
Paths 43

Size

Total Lines 37
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 12.0427

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 37
ccs 28
cts 30
cp 0.9333
rs 5.1612
cc 12
eloc 25
nc 43
nop 1
crap 12.0427

How to fix   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 IspDb {
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
	);
32
33 3
	public function __construct(Logger $logger) {
34 3
		$this->logger = $logger;
35 3
	}
36
37 3
	private function queryUrl($url) {
38
		try {
39 3
			$xml = @simplexml_load_file($url);
40 3
			if (libxml_get_last_error() !== False || !is_object($xml) || !$xml->emailProvider) {
41 3
				libxml_clear_errors();
42 3
				return [];
43
			}
44
			$provider = [
45 3
				'displayName' => (string) $xml->emailProvider->displayName,
46 3
			];
47 3
			foreach ($xml->emailProvider->children() as $tag => $server) {
48 3
				if (!in_array($tag, ['incomingServer', 'outgoingServer'])) {
49 3
					continue;
50
				}
51 3
				foreach ($server->attributes() as $name => $value) {
52 3
					if ($name == 'type') {
53 3
						$type = (string) $value;
54 3
					}
55 3
				}
56 3
				$data = [];
57 3
				foreach ($server as $name => $value) {
58 3
					foreach ($value->children() as $tag => $val) {
59 2
						$data[$name][$tag] = (string) $val;
60 3
					}
61 3
					if (!isset($data[$name])) {
62 3
						$data[$name] = (string) $value;
63 3
					}
64 3
				}
65 3
				$provider[$type][] = $data;
0 ignored issues
show
Bug introduced by
The variable $type does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
66 3
			}
67 3
		} catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class OCA\Mail\Service\AutoConfig\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...
68
			// ignore own not-found exception or xml parsing exceptions
69
			unset($e);
70
			$provider = [];
71
		}
72 3
		return $provider;
73
	}
74
75
	/**
76
	 * @param string $domain
77
	 * @return array
78
	 */
79 3
	public function query($domain, $tryMx = true) {
80 3
		$this->logger->debug("IsbDb: querying <$domain>");
81 3
		if (strpos($domain, '@') !== false) {
82
			// TODO: use horde mail address parsing instead
83
			list(, $domain) = explode('@', $domain);
84
		}
85
86 3
		$provider = [];
87 3
		foreach ($this->urls as $url) {
88 3
			$url = str_replace("{DOMAIN}", $domain, $url);
89 3
			$this->logger->debug("IsbDb: querying <$domain> via <$url>");
90
91 3
			$provider = $this->queryUrl($url);
92 3
			if (!empty($provider)) {
93 3
				return $provider;
94
			}
95 3
		}
96
97
		if ($tryMx && ($dns = dns_get_record($domain, DNS_MX))) {
98
			$domain = $dns[0]['target'];
99
			if (!($provider = $this->query($domain, false))) {
100
				list(, $domain) = explode('.', $domain, 2);
101
				$provider = $this->query($domain, false);
102
			}
103
		}
104
		return $provider;
105
	}
106
107
}
108