Completed
Push — master ( 589fec...539028 )
by Milan
07:58
created

FioPay::send()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8657
c 0
b 0
f 0
cc 6
nc 8
nop 1
1
<?php declare(strict_types=1);
2
3
namespace h4kuna\Fio;
4
5
use h4kuna\Fio\Account\Bank;
6
use h4kuna\Fio\Exceptions\InvalidArgument;
7
use h4kuna\Fio\Request\Log;
8
use h4kuna\Fio\Request\Pay;
9
use h4kuna\Fio\Response\Pay\IResponse;
10
11
class FioPay extends Fio
12
{
13
14
	private const LANGS = ['en', 'cs', 'sk'];
15
	private const EXTENSIONS = ['xml', 'abo'];
16
17
	/** @var string */
18
	private $uploadExtension;
19
20
	/** @var string */
21
	private $language = 'cs';
22
23
	/** @var IResponse */
24
	private $response;
25
26
	/** @var Pay\XMLFile */
27
	private $xmlFile;
28
29
	/** @var Log */
30
	private $log;
31
32
33
	public function __construct(Request\IQueue $queue, Account\FioAccount $account, Pay\XMLFile $xmlFile)
34
	{
35
		parent::__construct($queue, $account);
36
		$this->xmlFile = $xmlFile;
37
	}
38
39
40
	public function enableLog(): Log
41
	{
42
		return $this->log = new Log();
43
	}
44
45
46
	public function createEuro(float $amount, string $accountTo, string $name, string $bic = ''): Pay\Payment\Euro
47
	{
48
		$account = Bank::createInternational($accountTo);
49
		if ($bic === '') {
50
			$bic = $account->getBankCode();
51
		}
52
53
		return (new Pay\Payment\Euro($this->account))
54
			->setName($name)
55
			->setAccountTo($account->getAccount())
56
			->setAmount($amount)
57
			->setBic($bic);
58
	}
59
60
61
	public function createNational(float $amount, string $accountTo, string $bankCode = ''): Pay\Payment\National
62
	{
63
		$account = Bank::createInternational($accountTo);
64
		if ($bankCode === '') {
65
			$bankCode = $account->getBankCode();
66
		}
67
		return (new Pay\Payment\National($this->account))
68
			->setAccountTo($account->getAccount())
69
			->setBankCode($bankCode)
70
			->setAmount($amount);
71
	}
72
73
74
	public function createInternational(float $amount, string $accountTo, string $name, string $street, string $city, string $country, string $info, string $bic = ''): Pay\Payment\International
75
	{
76
		$account = Bank::createInternational($accountTo);
77
		if ($bic === '') {
78
			$bic = $account->getBankCode();
79
		}
80
81
		return (new Pay\Payment\International($this->account))
82
			->setBic($bic)
83
			->setName($name)
84
			->setCountry($country)
85
			->setAccountTo($account->getAccount())
86
			->setStreet($street)
87
			->setCity($city)
88
			->setRemittanceInfo1($info)
89
			->setAmount($amount);
90
	}
91
92
93
	public function getUploadResponse(): IResponse
94
	{
95
		return $this->response;
96
	}
97
98
99
	/**
100
	 * @return static
101
	 */
102
	public function addPayment(Pay\Payment\Property $property)
103
	{
104
		$this->xmlFile->setData($property);
105
		return $this;
106
	}
107
108
109
	/**
110
	 * @param string|Pay\Payment\Property $filename
111
	 */
112
	public function send($filename = null): IResponse
113
	{
114
		if ($filename instanceof Pay\Payment\Property) {
115
			$this->xmlFile->setData($filename);
116
		}
117
118
		if ($this->xmlFile->isReady()) {
119
			$this->setUploadExtension('xml');
120
			$filename = $this->xmlFile->getPathname($this->log !== null);
121
			if ($this->log !== null) {
122
				$this->log->setFilename($filename);
123
			}
124
		} elseif (is_string($filename) && is_file($filename)) {
125
			$this->setUploadExtension(pathinfo($filename, PATHINFO_EXTENSION));
126
		} else {
127
			throw new InvalidArgument('Is supported only filepath or Property object.');
128
		}
129
130
		$token = $this->getAccount()->getToken();
131
		$post = [
132
			'type' => $this->uploadExtension,
133
			'token' => $token,
134
			'lng' => $this->language,
135
		];
136
137
		return $this->response = $this->queue->upload(self::getUrl(), $token, $post, $filename);
138
	}
139
140
141
	/**
142
	 * Response language.
143
	 * @return static
144
	 */
145
	public function setLanguage(string $lang)
146
	{
147
		$this->language = InvalidArgument::checkIsInList(strtolower($lang), self::LANGS);
148
		return $this;
149
	}
150
151
152
	private static function getUrl(): string
153
	{
154
		return self::REST_URL . 'import/';
155
	}
156
157
158
	private function setUploadExtension(string $extension): void
159
	{
160
		$this->uploadExtension = InvalidArgument::checkIsInList(strtolower($extension), self::EXTENSIONS);
161
	}
162
163
}
164