Moip::response()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 11
Bugs 2 Features 0
Metric Value
c 11
b 2
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 11
nc 2
nop 1
1
<?php namespace SOSTheBlack\Moip;
2
3
use App;
4
use Config;
5
use Session;
6
use Exception;
7
8
/**
9
 * Moip's API abstraction class
10
 *
11
 * Class to use for all abstraction of Moip's API
12
 * @package SOSTheBlack\Moip
13
 * @author Jean Cesar Garcia <[email protected]> <SOSTheBlack>
14
 * @version 1.*
15
 * @license <a href="http://www.opensource.org/licenses/bsd-license.php">BSD License</a>
16
 **/
17
class Moip extends MoipAbstract implements MoipInterface
18
{
19
	/**
20
	 * Retorno da API
21
	 *
22
	 * @var object
23
	 **/
24
	private $response;
25
26
	/**
27
	 * postOrder
28
	 * 
29
	 * @param string[] $order 
30
	 * @return string[]
31
	 */
32
	public function postOrder(array $order)
33
	{
34
		$this->setData($order);
35
		$this->initialize();
36
		$this->getParcel();
37
		$this->billetConf();
38
		$this->getMessage();
39
		$this->getComission();
40
		$this->getPaymentWay();
41
		$this->api->setReason($this->getReason());
42
		$this->api->setUniqueID($this->getUniqueId());
0 ignored issues
show
Documentation introduced by
$this->getUniqueId() is of type string|false, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
		$this->api->setPayer($this->getParams('payer'));
44
		$this->api->setValue($this->data['prices']['value']);
45
		$this->api->setAdds($this->getParams('prices', 'adds'));
46
		$this->api->setDeduct($this->getParams('prices','deduct'));
47
		$this->api->setReceiver($this->getParams('receiver', null, true));
48
		$this->api->setReturnURL($this->getParams('url_return', null, true));
49
		$this->api->setNotificationURL($this->getParams('url_notification', null, true));
50
		$this->api->validate($this->getValidate());
51
		return $this->response($this->api->send());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->response($this->api->send()); (string[]) is incompatible with the return type declared by the interface SOSTheBlack\Moip\MoipInterface::postOrder of type SOSTheBlack\Moip\Moip\response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
52
	}
53
54
	/**
55
	 * response
56
	 * 
57
	 * @param type $send 
58
	 * @return string[]
59
	 */
60
	public function response($send = null)
61
	{
62
		if ($send) {
63
			$answer = $this->api->getAnswer();
64
			$this->responseValidation($send, $answer);
65
			$this->response 			= App::make('stdClass');
66
			$this->response->getXML 	= $this->api->getXML();
67
			$this->response->replyXML 	= $send->xml;
68
			$this->response->token 		= $answer->token;
69
			$this->response->url 		= $answer->payment_url;
70
		}
71
72
		Session::put(Config::get('sostheblack::moip.array_session').'.response', (array) $this->response);
73
		return $this->response;
74
	}
75
76
	/**
77
	 * parcel
78
	 * 
79
	 * Generete parcel
80
	 * 
81
	 * @param  array  $parcel
82
	 * @return array
83
	 */
84
	public function parcel(array $parcel)
85
	{
86
		$query = $this->api->queryParcel(
87
			$parcel['login'],
88
			$parcel['maxParcel'],
89
			$parcel['rate'],
90
			$parcel['simulatedValue']
91
		);
92
93
		if ($query['response'] !== true) {
94
			throw new Exception('Error: Não foi possível gerar query');
95
		}
96
97
		return $query['installment'];
98
	}
99
}