MoipAbstract::getComission()   B
last analyzed

Complexity

Conditions 8
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 15
rs 7.7777
cc 8
eloc 9
nc 3
nop 0
1
<?php namespace SOSTheBlack\Moip;
2
3
use App;
4
use Moip;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, SOSTheBlack\Moip\Moip.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
5
use Exception;
6
7
/**
8
 * MoipAbstract
9
 *
10
 * @package SOSTheBlack\Moip
11
 * @author Jean Cesar Garcia <[email protected]> <SOSTheBlack>
12
 * @version 1.*
13
 **/
14
abstract class MoipAbstract
15
{
16
	/**
17
	 * Api of MoIP
18
	 *
19
	 * @var \SOSTheBlack\Moip\Api
20
	 **/
21
	protected $api;
22
23
	/**
24
	 * data of configuration of the MoIP
25
	 *
26
	 * @var array table moip
27
	 **/
28
	protected $moip;
29
	
30
	/**
31
	 * undocumented class variable
32
	 *
33
	 * @var string
34
	 **/
35
	protected $data;
36
37
	/**
38
	* initialize()
39
	*
40
	* @return \SOSTheBlack\Moip\Api
41
	*/
42
	protected function initialize()
43
	{
44
		$this->moip = Moip::first();
45
		$this->api  = App::make('\SOSTheBlack\Moip\Api');
46
		$this->api->setEnvironment(! $this->moip->environment);
47
		$this->api->setCredential([
48
		    'key' 	=> $this->moip->key,
49
		    'token' => $this->moip->token
50
		]);
51
		return $this->api;
52
	}
53
54
	/**
55
	 * setData()
56
	 * 
57
	 * @param string[] $data
58
	 * @return void
59
	 */
60
	protected function setData($data)
61
	{
62
		$this->data = $data;
0 ignored issues
show
Documentation Bug introduced by
It seems like $data of type array<integer,string> is incompatible with the declared type string of property $data.

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...
63
	}	
64
65
	/**
66
	 * getValidate()
67
	 * 
68
	 * @return string
69
	 */
70
	protected function getValidate()
71
	{
72
		return $this->moip->validate === 0 ? 'Basic' : 'Identification';
73
	}
74
75
	/**
76
	 * getUniqueId()
77
	 * 
78
	 * adds unique id in the order
79
	 * 
80
	 * @return string|false
81
	 */
82
	protected function getUniqueId()
83
	{
84
		return isset($this->data['unique_id']) ? $this->data['unique_id'] : false;
85
	}
86
87
	/**
88
	 * getReason()
89
	 * 
90
	 * adds payment reason in the order
91
	 * 
92
	 * @return string
93
	 */
94
	protected function getReason()
95
	{
96
		return isset($this->data['reason']) ? $this->data['reason'] : $this->moip->reason;
97
	}
98
99
	/**
100
	 * getParams
101
	 * 
102
	 * Validation of the params sent
103
	 * 
104
	 * @param string $oneParam 
105
	 * @param string $twoParam 
106
	 * @param boolean $db 
107
	 * @return string
108
	 */
109
	protected function getParams($oneParam, $twoParam = null, $db = false)
110
	{
111
		if (! $twoParam) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $twoParam of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
112
			if ($db === true) {
113
				return isset($this->data[$oneParam]) ? $this->data[$oneParam] : $this->moip->$oneParam;
114
			} else {
115
				return isset($this->data[$oneParam]) ? $this->data[$oneParam] : '';
116
			}
117
		} else {
118
			return isset($this->data[$oneParam][$twoParam]) ? $this->data[$oneParam][$twoParam] : '';
119
		}
120
	}
121
122
	/**
123
	 * billetConf
124
	 * @return void
125
	 */
126
	protected function billetConf()
127
	{
128
		$this->api->setBilletConf(
129
			$this->getParams('billet', 'expiration', true),
130
			(boolean) $this->getParams('billet', 'workingDays', true),
131
			$this->getBilletInstructions(),
132
			$this->getParams('billet', 'uriLogo', true)
133
		);
134
	}
135
136
	/**
137
	 * getBilletInstructions
138
	 * 
139
	 * @return string[]
140
	 */
141
	private function getBilletInstructions()
142
	{
143
		$billet = [
144
	    	'instructions' => [
145
	    		'firstLine',
146
				'secondLine',
147
				'lastLine'
148
	    	]
149
		];
150
		
151
		foreach ($billet['instructions'] as $keyInstructions => $valueInstructions) {
152
			$billet['instructions'][$keyInstructions] =
153
				isset($this->data['billet']['instructions'][$keyInstructions]) ? 
154
				$this->data['billet']['instructions'][$keyInstructions] :
155
				$this->moip->$valueInstructions;
156
		}
157
		
158
		return $billet['instructions'];
159
	}
160
161
	/**
162
	 * getPaymentWay
163
	 * 
164
	 * Adds payment way in the order
165
	 * 
166
	 * @return false|null
167
	 */
168
	protected function getPaymentWay()
169
	{
170
		if (! isset($this->data['paymentWay'])) {
171
			return false;
172
		} else {
173
			$payment  = $this->data['paymentWay'];
174
			$arrayWay = [
175
				'creditCard',
176
		    	'billet'	,
177
		    	'financing'	,
178
		    	'debit'		,
179
		    	'debitCard'	
180
		    ];
181
182
			foreach ($arrayWay as $arrayWayKey => $arrayWayValue) {
183
				if (isset($payment[$arrayWayKey]) && $payment[$arrayWayKey] == $arrayWayValue) {
184
					$this->api->addPaymentWay($arrayWayValue);
185
				} else {
186
					if ($this->moip->$arrayWayValue === 1) {
187
						$this->api->addPaymentWay($arrayWayValue);
188
					}
189
				}
190
			}
191
		}
192
	}
193
194
	/**
195
	 * getMessage
196
	 * 
197
	 * @return void
198
	 */
199
	protected function getMessage()
200
	{
201
		if (isset($this->data['message'])) {
202
			foreach ($this->data['message'] as $keyMessage => $valueMessage) {
0 ignored issues
show
Bug introduced by
The expression $this->data['message'] of type string is not traversable.
Loading history...
203
				$this->api->addMessage($valueMessage);
204
			}
205
		}
206
	}
207
208
	/**
209
	 * getComission
210
	 * 
211
	 * @return void
212
	 */
213
	protected function getComission()
214
	{
215
		if (isset($this->data['comission'])) {
216
			//dd($this->data['comission']);
217
			foreach ($this->data['comission'] as $keyComission => $valueComission) {
0 ignored issues
show
Bug introduced by
The expression $this->data['comission'] of type string is not traversable.
Loading history...
218
				$this->api->addComission(
219
					isset($valueComission['reason']) 			? $valueComission['reason'] 		 : null,
220
					isset($valueComission['receiver']) 			? $valueComission['receiver'] 		 : null,
221
					isset($valueComission['value']) 			? $valueComission['value'] 			 : null,
222
					isset($valueComission['percentageValue']) 	? $valueComission['percentageValue'] : null,
223
					isset($valueComission['ratePayer']) 		? $valueComission['ratePayer'] 		 : null
224
				);
225
			}
226
		}
227
	}
228
229
	/**
230
	 * getParcel
231
	 * 
232
	 * @return void
233
	 */
234
	protected function getParcel()
235
	{
236
		if (isset($this->data['parcel'])) {
237
			foreach ($this->data['parcel'] as $keyParcel => $valueParcel) {
0 ignored issues
show
Bug introduced by
The expression $this->data['parcel'] of type string is not traversable.
Loading history...
238
				$this->api->addParcel(
239
					isset($valueParcel['min']) 		? $valueParcel['min'] 		: null,
240
					isset($valueParcel['max']) 		? $valueParcel['max'] 		: null,
241
					isset($valueParcel['rate']) 	? $valueParcel['rate'] 		: null,
242
					isset($valueParcel['transfer']) ? $valueParcel['transfer'] 	: false,
243
					isset($valueParcel['receipt']) ? $valueParcel['receipt'] 	: false
244
				);
245
			}
246
		}
247
	}
248
249
	/**
250
	 * responseValidation
251
	 * 
252
	 * @param  array $send
253
	 * @param  array|string $answer
254
	 * @return void
255
	 */
256
	protected function responseValidation($send, $answer)
257
	{
258
		if ($send->error != false) {
259
			throw new Exception($send->error);
260
		} elseif (is_string($answer)) {
261
			throw new Exception($answer);
262
		} elseif ($answer->error !== false) {
263
			throw new Exception($answer->error);
264
		}
265
	}
266
}