Payment::setName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace PaySys\CardPay;
4
5
use PaySys\PaySys\IPayment;
6
7
8 1
class Payment implements IPayment
9
{
10
	/** @var string */
11
	protected $amt;
12
13
	/** @var string */
14
	protected $vs;
15
16
	/** @var string */
17
	protected $curr = '978'; // EUR
18
19
	/** @var string */
20
	protected $name;
21
22
	/** @var bool */
23
	protected $tpay = false;
24
25
	/** @var string */
26
	protected $timestamp;
27
28
29
	public function __construct(string $amt, string $vs, string $name)
30
	{
31 1
		$this->setAmount($amt);
32 1
		$this->setVariableSymbol($vs);
33 1
		$this->setName($name);
34 1
		$this->timestamp = gmdate('dmYHis');
35 1
	}
36
37
	public function setAmount($amt) : IPayment
38
	{
39 1
		if (!Validator::isAmount($amt))
40 1
			throw new \PaySys\PaySys\InvalidArgumentException(sprintf("Amount must have maximal 9 digits before dot and maximal 2 digits after. '%s' is invalid.", $amt));
41
42 1
		$this->amt = $amt;
43 1
		return $this;
44
	}
45
46
	public function getAmount() : string
47
	{
48 1
		return $this->amt;
49
	}
50
51
	public function setVariableSymbol(string $vs) : Payment
52
	{
53 1
		if (!Validator::isVariableSymbol($vs))
54 1
			throw new \PaySys\PaySys\InvalidArgumentException(sprintf("Variable symbol must have minimal 1 and maximal 10 digits. '%s' is invalid.", $vs));
55
56 1
		$this->vs = $vs;
57 1
		return $this;
58
	}
59
60
	public function getVariableSymbol() : string
61
	{
62 1
		return $this->vs;
63
	}
64
65
	public function setCurrency(string $curr) : Payment
66
	{
67 1
		$currOriginal = $curr;
68 1
		$curr = self::normalizeCurrency($curr);
69 1
		if (!Validator::isCurrency($curr))
70 1
			throw new \PaySys\PaySys\InvalidArgumentException(sprintf("Currency '%s' is invalid.", $currOriginal));
71
72 1
		$this->curr = $curr;
73 1
		return $this;
74
	}
75
76
	public function getCurrency() : string
77
	{
78 1
		return $this->curr;
79
	}
80
81
	public function setName(string $name) : Payment
82
	{
83 1
		if (!Validator::isName($name))
84 1
			throw new \PaySys\PaySys\InvalidArgumentException(sprintf("Name '%s' is invalid. Allowed characters are [0-9a-zA-Z .-_@].", $name));
85
86 1
		$this->name = $name;
87 1
		return $this;
88
	}
89
90
	public function getName() : string
91
	{
92 1
		return $this->name;
93
	}
94
95
	/**
96
	 * @internal
97
	 */
98
	public function setTimestamp(string $timestamp) : Payment
99
	{
100 1
		if (!Validator::isTimestamp($timestamp))
101
			throw new \PaySys\PaySys\InvalidArgumentException(sprintf("Timestamp '%s' is invalid.", $timestamp));
102
103 1
		$this->timestamp = $timestamp;
104 1
		return $this;
105
	}
106
107
	public function getTimestamp() : string
108
	{
109 1
		return $this->timestamp;
110
	}
111
112
	public function setTpay(bool $tpay = true) : Payment
113
	{
114
		if (!is_bool($tpay))
115
			throw new \PaySys\PaySys\InvalidArgumentException(sprintf("TPAY must be boolean."));
116
117
		$this->tpay = $tpay;
118
		return $this;
119
	}
120
121
	public function getTpay() : bool
122
	{
123 1
		return $this->tpay;
124
	}
125
126
127
	private static function normalizeCurrency(string $curr) : string
128
	{
129
		$curr2code = [
130 1
			'EUR' => '978',
131
			'CZK' => '203',
132
			'USD' => '840',
133
			'GBP' => '826',
134
			'HUF' => '348',
135
			'PLN' => '985',
136
			'CHF' => '756',
137
			'DKK' => '208',
138
		];
139
140 1
		return $curr2code[strtoupper($curr)] ?? $curr;
141
	}
142
}
143