Parser   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 14
c 4
b 0
f 0
lcom 2
cbo 1
dl 0
loc 119
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 2
A setUrl() 0 4 1
A getUrl() 0 4 1
B processUrl() 0 18 5
A processTld() 0 13 3
A getTld() 0 4 1
A setTld() 0 4 1
1
<?php
2
/**
3
 * User: ms
4
 * Date: 12.10.15
5
 * Time: 22:16
6
 */
7
8
namespace Amazon;
9
10
use Amazon\Exceptions\InvalidDomainException;
11
12
abstract class Parser
13
{
14
	/**
15
	 * @var string
16
	 */
17
	private $tld = null;
18
19
	/**
20
	 * @var array
21
	 */
22
	private $unusedDomainParts = array(
23
		'amazon'
24
	, 'amzn'
25
	, 'www'
26
	);
27
28
	/**
29
	 * @var array
30
	 */
31
	private $amazonDomains = array(
32
		'amazon'
33
	, 'amzn'
34
	);
35
36
	/**
37
	 * @var string
38
	 */
39
	protected $url = null;
40
41
	public function __construct($url)
42
	{
43
44
		$this->url = $url;
45
		//Question Marks
46
		if (preg_match('/%3F/', $url)) {
47
			$this->url = substr($url, 0, strpos($url, "%3F"));
48
49
		}
50
51
		$urlParameter = parse_url($this->getUrl());
52
		$this->processUrl($urlParameter);
53
		$this->processTld($urlParameter['host']);
54
55
	}
56
57
	/**
58
	 * @param string $url
59
	 */
60
	protected function setUrl($url)
61
	{
62
		$this->url = $url;
63
	}
64
65
	/**
66
	 * @return string
67
	 */
68
	protected function getUrl()
69
	{
70
		return $this->url;
71
	}
72
73
	/**
74
	 * @param $urlParameter
75
	 * @throws InvalidDomainException
76
	 */
77
	protected function processUrl($urlParameter)
78
	{
79
		if (false === array_key_exists('path', $urlParameter)) {
80
			throw new InvalidDomainException(sprintf('Url %s has no path', $this->getUrl()));
81
		}
82
83
		//Check if we have an Amazon Domain
84
		$isAmazonDomain = false;
85
		//ShortUrl
86
		foreach ($this->amazonDomains as $domain) {
87
			if (preg_match('/' . $domain . '/', $this->getUrl())) {
88
				$isAmazonDomain = true;
89
			}
90
		}
91
		if (false === $isAmazonDomain) {
92
			throw new InvalidDomainException(sprintf('Url %s does not belong to Amazon', $this->getUrl()));
93
		}
94
	}
95
96
	/**
97
	 * @param $host
98
	 * @return string
99
	 */
100
	protected function processTld($host)
101
	{
102
		$tldStrings = [];
103
		$parts = explode('.', $host);
104
		foreach ($parts as $part) {
105
106
			if (false === in_array($part, $this->unusedDomainParts)) {
107
				$tldStrings[] = $part;
108
			}
109
		}
110
111
		$this->setTld(implode('.', $tldStrings));
112
	}
113
114
	/**
115
	 * @return string
116
	 */
117
	public function getTld()
118
	{
119
		return $this->tld;
120
	}
121
122
	/**
123
	 * @param string $tld
124
	 */
125
	protected function setTld($tld)
126
	{
127
		$this->tld = $tld;
128
	}
129
130
}