Passed
Push — master ( d68831...58f816 )
by Bruce Pinheiro de
04:28
created

CheckoutClient::getBoletoBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace BPCI\SumUp\Checkout;
3
4
use BPCI\SumUp\ContextInterface;
5
use BPCI\SumUp\Exception\InvalidCheckoutException;
6
use BPCI\SumUp\OAuth\AccessToken;
7
use BPCI\SumUp\SumUp;
8
use BPCI\SumUp\SumUpClientInterface;
9
use BPCI\SumUp\Traits\Client;
10
use BPCI\SumUp\Traits\ClientInterface;
11
use GuzzleHttp\Exception\BadResponseException;
12
use GuzzleHttp\Exception\ConnectException;
13
use Psr\Http\Message\ResponseInterface;
14
15
/**
16
 * Class CheckoutClient
17
 * @package BPCI\SumUp\Checkout
18
 */
19
class CheckoutClient implements CheckoutClientInterface, ClientInterface
20
{
21
    use Client {
22
        request as protected traitRequest;
23
    }
24
25
	protected $context;
26
	protected $lastResponse;
27
    protected $options;
28
    protected $currentToken;
29
30
    function __construct(ContextInterface $context, ?array $options = [])
31
	{
32
		$this->context = $context;
33
        $this->options = $options;
34
	}
35
36
	/**
37
	 * @inheritDoc
38
	 * @throws BadResponseException
39
	 * @throws ConnectException
40
	 */
41
    function create(CheckoutInterface $checkout, AccessToken $accessToken = null):? CheckoutInterface
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
42
	{
43
        return $this->request('post', $checkout) ? $checkout : null;
44
	}
45
46
	/**
47
	 * @inheritDoc
48
	 * @throws BadResponseException
49
	 * @throws ConnectException
50
	 */
51
    function complete(CheckoutInterface $checkout, AccessToken $accessToken = null):? CheckoutInterface
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
52
	{
53
        return $this->request('put', $checkout) ? $checkout : null;
54
	}
55
56
	/**
57
	 * @param string $action
58
	 * @param CheckoutInterface $checkout
59
     * @param string|null $endpoint
60
	 * @return bool|null
61
	 */
62
    function request(string $action, $checkout, string $endpoint = null):? bool
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
63
	{
64
        if (!$checkout->isValid()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $checkout->isValid() of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
65
            throw new InvalidCheckoutException($checkout);
66
		}
67
68
        return $this->traitRequest($action, $checkout);
69
	}
70
71
	/**
72
	 * Generate a body to create a new checkout
73
	 *
74
	 * @param CheckoutInterface $checkout
75
	 * @return array
76
	 */
77
    protected static function getCheckoutBody(CheckoutInterface $checkout): array
78
	{
79
		return [
80
			"checkout_reference" => $checkout->getReference(),
81
			"amount" => $checkout->getAmount(),
82
			"currency" => $checkout->getCurrency(),
83
			"fee_amount" => $checkout->getFeeAmount(),
84
			"pay_to_email" => $checkout->getPayToEmail(),
85
			"pay_from_email" => $checkout->getPayFromEmail(),
86
			"description" => $checkout->getDescription(),
87
			"return_url" => $checkout->getRedirectUrl(),
88
		];
89
	}
90
91
    /**
92
     * @param CheckoutInterface $checkout
93
     * @param string|null $type
94
     * @return array
95
     */
96
    static function getBody($checkout, string $type = null): array
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
97
	{
98
		$defaultBody = self::getCheckoutBody($checkout);
99
		switch ($checkout->getType()) {
100
			case 'card':
101
				$completeBody = self::getCardBody($checkout);
102
				break;
103
104
			case 'boleto':
105
				$completeBody = self::getBoletoBody($checkout);
106
				break;
107
108
			default:
109
				$completeBody = [];
110
		}
111
112
		return array_merge($defaultBody, $completeBody);
113
	}
114
115
	private static function getCardBody(CheckoutInterface $checkout): array
116
	{
117
		return [
118
			'payment_type' => $checkout->getType(),
119
			'token' => $checkout->getCard()->getToken(),
120
			'customer_id' => $checkout->getCustomer()->getCustomerId()
121
		];
122
	}
123
124
	private static function getBoletoBody(CheckoutInterface $checkout): array
125
	{
126
		$customer = $checkout->getCustomer();
127
		return [
128
			'payment_type' => $checkout->getType(),
129
			'description' => $checkout->getDescription(),
130
			'boleto_details' => [
131
//				'cpf_cnpj' => $customer->getCpfCnpj(),
132
				'payer_name' => $customer->getName(),
133
				'payer_address' => $customer->getAddress(),
134
				'payer_city' => $customer->getAddress()->getCity(),
135
				'payer_state_code' => $customer->getAddress()->getState(),
136
				'payer_post_code' => $customer->getAddress()->getPostalCode()
137
			],
138
		];
139
	}
140
141
	/**
142
	 * @inheritDoc
143
	 */
144
    static function getScopes(): array
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
145
	{
146
		return [
147
			'payments',
148
			'boleto'
149
		];
150
	}
151
152
	function getLastResponse(): ResponseInterface
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
153
	{
154
		return $this->lastResponse;
155
	}
156
157
	/**
158
	 * return the context used to created the client.
159
	 * @return ContextInterface
160
	 */
161
	function getContext(): ContextInterface
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
162
	{
163
		return $this->context;
164
	}
165
166
    function getOptions(): array
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
167
    {
168
        return $this->options;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->options could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
169
    }
170
171
    /**
172
     * @param ResponseInterface $response
173
     * @return SumUpClientInterface
174
     */
175
    function setLastResponse(ResponseInterface $response): SumUpClientInterface
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
176
    {
177
        $this->lastResponse = $response;
178
179
        return $this;
180
    }
181
182
    /**
183
     * @return string
184
     */
185
    function getEndPoint(): string
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
186
    {
187
        return 'checkouts';
188
    }
189
190
    /**
191
     * @param AccessToken $token
192
     * @return SumUpClientInterface
193
     */
194
    function setToken(AccessToken $token): SumUpClientInterface
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
195
    {
196
        $this->currentToken = $token;
197
198
        return $this;
199
    }
200
201
    /**
202
     * @return AccessToken
203
     */
204
    function getToken():? AccessToken
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
205
    {
206
        return $this->currentToken;
207
    }
208
209
    /**
210
     * @param CheckoutInterface $checkout
211
     * @return string
212
     */
213
    static function getCompleteUrl(CheckoutInterface $checkout): string
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
214
    {
215
        if ($checkout->getId()) {
216
            return SumUp::getEntrypoint().$checkout->getId();
217
        }
218
219
        return '';
220
    }
221
}
222