Completed
Pull Request — master (#16)
by Josef
02:58
created

Validator::checkCustomerId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 8
loc 8
cc 2
eloc 4
nc 2
nop 1
rs 9.4285
1
<?php declare(strict_types = 1);
2
3
namespace SlevomatCsobGateway;
4
5
class Validator
6
{
7
8
	const CART_ITEM_NAME_LENGTH_MAX = 20;
9
	const CART_ITEM_DESCRIPTION_LENGTH_MAX = 40;
10
11
	const ORDER_ID_LENGTH_MAX = 10;
12
	const RETURN_URL_LENGTH_MAX = 300;
13
	const DESCRIPTION_LENGTH_MAX = 255;
14
	const MERCHANT_DATA_LENGTH_MAX = 255;
15
	const CUSTOMER_ID_LENGTH_MAX = 50;
16
	const PAY_ID_LENGTH_MAX = 15;
17
18
	const TTL_SEC_MIN = 300;
19
	const TTL_SEC_MAX = 1800;
20
21 View Code Duplication
	public static function checkCartItemName(string $name)
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...
22
	{
23
		self::checkWhitespaces($name);
24
25
		if (strlen(utf8_decode($name)) > self::CART_ITEM_NAME_LENGTH_MAX) {
26
			throw new \InvalidArgumentException(sprintf('Cart item name can have maximum of %d characters.', self::CART_ITEM_NAME_LENGTH_MAX));
27
		}
28
	}
29
30 View Code Duplication
	public static function checkCartItemDescription(string $description)
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...
31
	{
32
		self::checkWhitespaces($description);
33
34
		if (strlen(utf8_decode($description)) > self::CART_ITEM_DESCRIPTION_LENGTH_MAX) {
35
			throw new \InvalidArgumentException(sprintf('Cart item description can have maximum of %d characters.', self::CART_ITEM_DESCRIPTION_LENGTH_MAX));
36
		}
37
	}
38
39
	public static function checkCartItemQuantity(int $quantity)
40
	{
41
		if ($quantity < 1) {
42
			throw new \InvalidArgumentException(sprintf(
43
				'Quantity must be greater than 0. %d given.',
44
				$quantity
45
			));
46
		}
47
	}
48
49
	public static function checkOrderId(string $orderId)
50
	{
51
		self::checkWhitespaces($orderId);
52
53
		if (!ctype_digit($orderId)) {
54
			throw new \InvalidArgumentException(sprintf(
55
				'OrderId must be numeric value. %s given.',
56
				$orderId
57
			));
58
		}
59
60
		if (strlen($orderId) > self::ORDER_ID_LENGTH_MAX) {
61
			throw new \InvalidArgumentException(sprintf('OrderId can have maximum of %d characters.', self::ORDER_ID_LENGTH_MAX));
62
		}
63
64
	}
65
66 View Code Duplication
	public static function checkReturnUrl(string $returnUrl)
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...
67
	{
68
		self::checkWhitespaces($returnUrl);
69
70
		if (strlen(utf8_decode($returnUrl)) > self::RETURN_URL_LENGTH_MAX) {
71
			throw new \InvalidArgumentException(sprintf('ReturnUrl can have maximum of %d characters.', self::RETURN_URL_LENGTH_MAX));
72
		}
73
	}
74
75 View Code Duplication
	public static function checkDescription(string $description)
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...
76
	{
77
		self::checkWhitespaces($description);
78
79
		if (strlen(utf8_decode($description)) > self::DESCRIPTION_LENGTH_MAX) {
80
			throw new \InvalidArgumentException(sprintf('Description can have maximum of %d characters.', self::DESCRIPTION_LENGTH_MAX));
81
		}
82
	}
83
84 View Code Duplication
	public static function checkMerchantData(string $merchantData)
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...
85
	{
86
		self::checkWhitespaces($merchantData);
87
88
		if (strlen(utf8_decode(base64_encode($merchantData))) > self::MERCHANT_DATA_LENGTH_MAX) {
89
			throw new \InvalidArgumentException(sprintf('MerchantData can have maximum of %d characters in encoded state.', self::MERCHANT_DATA_LENGTH_MAX));
90
		}
91
	}
92
93 View Code Duplication
	public static function checkCustomerId(string $customerId)
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...
94
	{
95
		self::checkWhitespaces($customerId);
96
97
		if (strlen(utf8_decode($customerId)) > self::CUSTOMER_ID_LENGTH_MAX) {
98
			throw new \InvalidArgumentException(sprintf('CustomerId can have maximum of %d characters.', self::CUSTOMER_ID_LENGTH_MAX));
99
		}
100
	}
101
102 View Code Duplication
	public static function checkPayId(string $payId)
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...
103
	{
104
		self::checkWhitespaces($payId);
105
106
		if (strlen(utf8_decode($payId)) > self::PAY_ID_LENGTH_MAX) {
107
			throw new \InvalidArgumentException(sprintf('PayId can have maximum of %d characters.', self::PAY_ID_LENGTH_MAX));
108
		}
109
	}
110
111
	private static function checkWhitespaces(string $argument)
112
	{
113
		$charlist = preg_quote(" \t\n\r\0\x0B\xC2\xA0", '#');
114
		preg_replace('#^[' . $charlist . ']+|[' . $charlist . ']+\z#u', '', $argument);
115
116
		if ($argument !== preg_replace('#^[' . $charlist . ']+|[' . $charlist . ']+\z#u', '', $argument)) {
117
			throw new \InvalidArgumentException('Argument starts or ends with whitespace.');
118
		}
119
	}
120
121
	public static function checkTtlSec(int $ttlSec)
122
	{
123
		if ($ttlSec < self::TTL_SEC_MIN || $ttlSec > self::TTL_SEC_MAX) {
124
			throw new \InvalidArgumentException(sprintf('TTL sec is out of range (%d - %d). Current value is %d.', self::TTL_SEC_MIN, self::TTL_SEC_MAX, $ttlSec));
125
		}
126
	}
127
128
}
129