Completed
Pull Request — master (#34)
by
unknown
06:19 queued 20s
created

FioPay::createNational()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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