MxChecker::getRecordMx()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
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
15
use sorciulus\EmailChecker\Exception\MxCheckerException;
16
use sorciulus\EmailChecker\Exception\MxFunctionException;
17
/**
18
* Class to extract mx record from domain
19
*/
20
class MxChecker implements MxInterface
21
{
22
	/**
23
	 * List of record mx found
24
	 * @var array
25
	 */
26
	private $recordMx;
27
28
	/**
29
	 * @param string $domain
30
	 *
31
	 * @throws MxFunctionException if function not found
32
	 */
33
	function __construct($domain)
34
	{
35
		if (!function_exists("dns_get_record")) {						
36
			throw new MxFunctionException("dns_get_record() has been disabled for security reasons. Try to enable.");              
37
		}
38
		$this->recordMx = @dns_get_record($domain, DNS_MX);
39
	}
40
41
	/**
42
	 * Get the value of recordMx
43
	 * 
44
	 * @return array 
45
	 *
46
	 * @throws MxCheckerException if record not found
47
	 */
48
	public function getRecordMx()
49
	{
50
        if(empty($this->recordMx)){
51
        	throw new MxCheckerException("Record Mx not found");
52
        }
53
        $this->sortRecordByPriority();        	
54
        return $this->recordMx;
55
	}
56
57
	/**
58
	 * Sort asc mx record by priority
59
	 * 
60
	 * @return array
61
	 */
62
	private function sortRecordByPriority()
63
	{
64
		$pri = [];
65
		foreach ($this->recordMx as $value) {
66
			$pri[] = $value["pri"];
67
		}
68
		array_multisort($pri, SORT_ASC, $this->recordMx);
69
	}
70
}