Completed
Push — master ( eab039...f878db )
by Roman
03:40
created

Configuration::setMode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace PaySys\CardPay;
4
5
use Nette;
6
use Nette\Application\LinkGenerator;
7
use Nette\Utils\Strings;
8
use PaySys\PaySys\IConfiguration;
9
10
11
/**
12
 * @method string getMid()
13
 * @method string getRurl()
14
 * @method string getKey()
15
 * @method string getIpc()
16
 * @method string getLang()
17
 * @method string getMode()
18
 * @method string getRem()
19
 */
20 1
final class Configuration implements IConfiguration
21
{
22
23
	const TEST = "test";
24
	const PRODUCTION = "production";
25
26
27
	/** @var string */
28
	private $mid;
29
30
	/** @var string */
31
	private $rurl;
32
33
	/** @var string */
34
	private $lang = 'sk';
35
36
	/** @var string */
37
	private $ipc;
38
39
	/** @var string */
40
	private $key;
41
42
	/** @var string */
43
	private $rem = '';
44
45
	/** @var LinkGenerator */
46
	private $linkGenerator;
47
48
	/** @var string */
49
	private $mode = self::PRODUCTION;
50
51
	/** @var string */
52
	private $buttonTemplate;
53
54
55
	public function __construct(string $mid, $rurl, string $key, LinkGenerator $linkGenerator = NULL)
56
	{
57 1
		$this->linkGenerator = $linkGenerator;
58 1
		$this->setMid($mid);
59 1
		$this->setRurl($rurl);
60 1
		$this->setKey($key);
61 1
		$this->setIpc();
62 1
		$this->setButtonTemplate(__DIR__ . '/template/button.latte');
63 1
	}
64
65
	public function setMid(string $mid) : Configuration
66
	{
67 1
		if (!Validator::isMid($mid))
68 1
			throw new \PaySys\PaySys\ConfigurationException(sprintf("Parameter MID must have 3 or 4 characters. '%s' is invalid.", $mid));
69
70 1
		$this->mid = $mid;
71 1
		return $this;
72
	}
73
74
	public function setRurl($originalRurl) : Configuration
75
	{
76 1
		$supportedTypes = ['string', 'array'];
77 1
		if (!in_array(gettype($originalRurl), $supportedTypes))
78 1
			throw new \PaySys\PaySys\ConfigurationException(sprintf("RURL type of '%s' is invalid. Must be %s.", gettype($originalRurl), implode(' or ', $supportedTypes)));
79
80 1
		$rurl = $originalRurl;
81 1
		if ($this->linkGenerator instanceof LinkGenerator) {
82
			try {
83 1
				if (is_string($originalRurl)) {
84 1
					$rurl = $this->linkGenerator->link($originalRurl);
85
				} elseif (is_array($originalRurl)) {
86 1
					$rurl = $this->linkGenerator->link($originalRurl['dest'], @$originalRurl['params']);
87
				}
88 1
			} catch (Nette\Application\UI\InvalidLinkException $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
89
		}
90
91 1
		if (!Validator::isRurl($rurl))
92 1
			throw new \PaySys\PaySys\ConfigurationException(sprintf("RURL '%s' is invalid. Must be valid URL by RFC 1738.", $originalRurl));
93
94 1
		$this->rurl = $rurl;
95 1
		return $this;
96
	}
97
98 View Code Duplication
	public function setKey(string $originalKey) : Configuration
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
	{
100 1
		$key = $this->getNormalizedKey($originalKey);
101 1
		if (!Validator::isKey($key))
102
			throw new \PaySys\PaySys\ConfigurationException(sprintf("Key '%s' is invalid. Must have 64 byte standard string or 128 byte in hexadecimal format.", $originalKey));
103
104 1
		$this->key = $key;
105 1
		return $this;
106
	}
107
108
	public function setIpc(string $ipc = NULL) : Configuration
109
	{
110 1
		if ($ipc === NULL) {
111 1
			$ipc = $this->getIpAddress();
112
		}
113
114 1
		if (!Validator::isIp($ipc))
115 1
			throw new \PaySys\PaySys\ConfigurationException(sprintf("IP '%s' is not valid.", $ipc));
116
117 1
		$this->ipc = $ipc;
118 1
		return $this;
119
	}
120
121 View Code Duplication
	public function setLang(string $originalLang) : Configuration
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
	{
123 1
		$lang = strtolower($originalLang);
124 1
		if (!Validator::isLang($lang))
125 1
			throw new \PaySys\PaySys\ConfigurationException(sprintf("Lang '%s' is not supported.", $originalLang));
126
127 1
		$this->lang = $lang;
128 1
		return $this;
129
	}
130
131
	public function setMode(string $mode) : Configuration
132
	{
133 1
		if (!in_array($mode, [self::TEST, self::PRODUCTION]))
134 1
			throw new \PaySys\PaySys\ConfigurationException(sprintf("Mode '%s' is not valid. Use please '%s' or '%s'.", $mode, self::TEST, self::PRODUCTION));
135
136 1
		$this->mode = $mode;
137 1
		return $this;
138
	}
139
140
	public function setRem(string $rem) : Configuration
141
	{
142 1
		if (!Nette\Utils\Validators::isEmail($rem))
143
			throw new \PaySys\PaySys\ConfigurationException(sprintf("'%s' is not valid e-mail.", $rem));
144
145 1
		$this->rem = $rem;
146 1
		return $this;
147
	}
148
149
	public function setButtonTemplate(string $path) : Configuration
150
	{
151 1
		if (!file_exists($path))
152 1
			throw new \PaySys\PaySys\ConfigurationException(sprintf("Template file '%s' not exists.", $path));
153
154 1
		$this->buttonTemplate = $path;
155 1
		return $this;
156
	}
157
158
	public function getButtonTemplate() : string
159
	{
160 1
		return $this->buttonTemplate;
161
	}
162
163
	public function __call($method, $arguments) : string
164
	{
165 1
		return $this->{strtolower(substr($method, 3))};
166
	}
167
168
	private function getNormalizedKey(string $originalKey) : string
169
	{
170 1
		if (strlen($originalKey) === 128 OR strlen($originalKey) === 191) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
171 1
			if (strlen($originalKey) === 191) {
172 1
				$key = str_replace(':', '', $originalKey);
173
			} else {
174 1
				$key = $originalKey;
175
			}
176
177 1
			if (strlen($key) === 128)
178 1
				return pack("H*", $key);
179
		}
180 1
		return $originalKey;
181
	}
182
183
	private function getIpAddress() : string
184
	{
185 1
		foreach (['HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR'] as $key) {
186 1
			if (array_key_exists($key, $_SERVER) === TRUE) {
187
				foreach (explode(',', $_SERVER[$key]) as $ip) {
188
					$ip = Strings::trim($ip);
189
190
					if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== FALSE) {
191 1
						return $ip;
192
					}
193
				}
194
			}
195
		}
196 1
		return "0.0.0.0";
197
	}
198
}
199