Completed
Pull Request — master (#34)
by
unknown
01:26
created

FioPay::createInternational()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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::createNational($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
	public function getXmlFile(): Pay\XMLFile
100
    {
101
        return $this->xmlFile;
102
    }
103
104
105
	/**
106
	 * @return static
107
	 */
108
	public function addPayment(Pay\Payment\Property $property)
109
	{
110
		$this->xmlFile->setData($property);
111
		return $this;
112
	}
113
114
115
	/**
116
	 * @param string|Pay\Payment\Property $filename
117
	 */
118
	public function send($filename = null): IResponse
119
	{
120
		if ($filename instanceof Pay\Payment\Property) {
121
			$this->xmlFile->setData($filename);
122
		}
123
124
		if ($this->xmlFile->isReady()) {
125
			$this->setUploadExtension('xml');
126
			$filename = $this->xmlFile->getPathname($this->log !== null);
127
			if ($this->log !== null) {
128
				$this->log->setFilename($filename);
129
			}
130
		} elseif (is_string($filename) && is_file($filename)) {
131
			$this->setUploadExtension(pathinfo($filename, PATHINFO_EXTENSION));
132
		} else {
133
			throw new InvalidArgument('Is supported only filepath or Property object.');
134
		}
135
136
		$token = $this->getAccount()->getToken();
137
		$post = [
138
			'type' => $this->uploadExtension,
139
			'token' => $token,
140
			'lng' => $this->language,
141
		];
142
143
		return $this->response = $this->queue->upload(self::getUrl(), $token, $post, $filename);
144
	}
145
146
147
	/**
148
	 * Response language.
149
	 * @return static
150
	 */
151
	public function setLanguage(string $lang)
152
	{
153
		$this->language = InvalidArgument::checkIsInList(strtolower($lang), self::LANGS);
154
		return $this;
155
	}
156
157
158
	private static function getUrl(): string
159
	{
160
		return self::REST_URL . 'import/';
161
	}
162
163
164
	private function setUploadExtension(string $extension): void
165
	{
166
		$this->uploadExtension = InvalidArgument::checkIsInList(strtolower($extension), self::EXTENSIONS);
167
	}
168
169
}
170