Completed
Push — master ( c011fc...384d9c )
by Milan
01:49
created

src/FioPay.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace h4kuna\Fio;
4
5
use h4kuna\Fio\Request\Pay;
6
7
class FioPay extends Fio
8
{
9
10
	/** @var string[] */
11
	private static $langs = ['en', 'cs', 'sk'];
12
13
	/** @var string */
14
	private $uploadExtension;
15
16
	/** @var string */
17
	private $language = 'cs';
18
19
	/** @var Pay\XMLResponse */
20
	private $response;
21
22
	/** @var Pay\XMLFile */
23
	private $xmlFile;
24
25
	public function __construct(Request\IQueue $queue, Account\FioAccount $account, Pay\XMLFile $xmlFile)
26
	{
27
		parent::__construct($queue, $account);
28
		$this->xmlFile = $xmlFile;
29
	}
30
31
	/** @return Pay\Payment\Euro */
32
	public function createEuro($amount, $accountTo, $name)
33
	{
34
		return (new Pay\Payment\Euro($this->account))
35
			->setName($name)
36
			->setAccountTo($accountTo)
37
			->setAmount($amount);
38
	}
39
40
	/** @return Pay\Payment\National */
41
	public function createNational($amount, $accountTo, $bankCode = null)
42
	{
43
		return (new Pay\Payment\National($this->account))
44
			->setAccountTo($accountTo, $bankCode)
45
			->setAmount($amount);
46
	}
47
48
	/** @return Pay\Payment\International */
49
	public function createInternational($amount, $accountTo, $bic, $name, $street, $city, $country, $info)
50
	{
51
		return (new Pay\Payment\International($this->account))
52
			->setBic($bic)
53
			->setName($name)
54
			->setCountry($country)
55
			->setAccountTo($accountTo)
56
			->setStreet($street)
57
			->setCity($city)
58
			->setRemittanceInfo1($info)
59
			->setAmount($amount);
60
	}
61
62
	/** @return Pay\IResponse */
63
	public function getUploadResponse()
64
	{
65
		return $this->response;
66
	}
67
68
	/**
69
	 * @param Pay\Payment\Property $property
70
	 * @return self
71
	 */
72
	public function addPayment(Pay\Payment\Property $property)
73
	{
74
		$this->xmlFile->setData($property);
75
		return $this;
76
	}
77
78
	/**
79
	 * @param string|Pay\Payment\Property $filename
80
	 * @return Response\Pay\IResponse
81
	 * @throws InvalidArgumentException
82
	 */
83
	public function send($filename = null)
84
	{
85
		if ($filename instanceof Pay\Payment\Property) {
86
			$this->xmlFile->setData($filename);
87
		}
88
89
		if ($this->xmlFile->isReady()) {
90
			$this->setUploadExtenstion('xml');
91
			$filename = $this->xmlFile->getPathname();
92
		} elseif (is_file($filename)) {
93
			$this->setUploadExtenstion(pathinfo($filename, PATHINFO_EXTENSION));
94
		} else {
95
			throw new InvalidArgumentException('Is supported only filepath or Property object.');
96
		}
97
98
		$token = $this->getAccount()->getToken();
99
		$post = [
100
			'type' => $this->uploadExtension,
101
			'token' => $token,
102
			'lng' => $this->language,
103
		];
104
105
		return $this->response = $this->queue->upload($this->getUrl(), $token, $post, $filename);
0 ignored issues
show
It seems like $filename defined by parameter $filename on line 83 can also be of type null or object<h4kuna\Fio\Request\Pay\Payment\Property>; however, h4kuna\Fio\Request\IQueue::upload() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Documentation Bug introduced by
It seems like $this->queue->upload($th...oken, $post, $filename) of type object<h4kuna\Fio\Response\Pay\IResponse> is incompatible with the declared type object<h4kuna\Fio\Request\Pay\XMLResponse> of property $response.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
106
	}
107
108
	/**
109
	 * Response language.
110
	 * @param string $lang
111
	 * @return self
112
	 * @throws InvalidArgumentException
113
	 */
114
	public function setLanguage($lang)
115
	{
116
		$this->language = strtolower($lang);
117
		if (!in_array($this->language, self::$langs)) {
118
			throw new InvalidArgumentException($this->language . ' avaible are ' . implode(', ', self::$langs));
119
		}
120
		return $this;
121
	}
122
123
	/** @return string */
124
	private function getUrl()
125
	{
126
		return self::REST_URL . 'import/';
127
	}
128
129
	/**
130
	 * Set upload file extension.
131
	 * @param string $extension
132
	 * @return self
133
	 * @throws InvalidArgumentException
134
	 */
135
	private function setUploadExtenstion($extension)
136
	{
137
		$extension = strtolower($extension);
138
		static $extensions = ['xml', 'abo'];
139
		if (!in_array($extension, $extensions)) {
140
			throw new InvalidArgumentException('Unsupported file upload format: ' . $extension . ' avaible are ' . implode(', ', $extensions));
141
		}
142
		$this->uploadExtension = $extension;
143
		return $this;
144
	}
145
146
}
147