Completed
Push — master ( 8ccc1b...aca536 )
by Jean C.
11s
created

Refund::bankAccountPartial()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
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
2
3
namespace Moip\Resource;
4
5
use ArrayIterator;
6
use Requests;
7
use stdClass;
8
9
/**
10
 * Class Refund.
11
 */
12
class Refund extends MoipResource
13
{
14
    /**
15
     * @const string
16
     */
17
    const PATH = 'refunds';
18
19
    /**
20
     * Refunds means.
21
     *
22
     * @const string
23
     */
24
    const METHOD_CREDIT_CARD = 'CREDIT_CARD';
25
26
    /**
27
     * Refunds means.
28
     *
29
     * @const string
30
     */
31
    const METHOD_BANK_ACCOUNT = 'BANK_ACCOUNT';
32
33
    /**
34
     * Refunds means.
35
     *
36
     * @const string
37
     */
38
    const METHOD_MONEY_ORDER = 'MONEY_ORDER';
39
40
    /**
41
     * @var \Moip\Resource\Orders
42
     */
43
    private $order;
44
45
    /**
46
     * @var \Moip\Resource\Payment
47
     */
48
    private $payment;
49
50
    /**
51
     * Initializes new instances.
52
     */
53
    public function initialize()
54
    {
55
        $this->data = new stdClass();
56
    }
57
58
    /**
59
     * Mount refund structure.
60
     *
61
     * @param \stdClass $response
62
     *
63
     * @return $this
64
     */
65
    protected function populate(stdClass $response)
66
    {
67
        $refund = clone $this;
68
69
        $refund->data->id = $this->getIfSet('id', $response);
70
71
        if (isset($response->amount)) {
72
            $refund->data->amount = new stdClass();
73
            $refund->data->amount->total = $this->getIfSet('total', $response->amount);
74
            $refund->data->amount->discounted = $this->getIfSet('discounted', $response->amount);
75
            $refund->data->amount->currency = $this->getIfSet('currency', $response->amount);
76
        }
77
78
        $refund->data->fee = $this->getIfSet('fee', $response);
79
        $refund->data->createdAt = $this->getIfSet('createdAt', $response);
80
81
        if (isset($response->refundingInstrument)) {
82
            $refund->data->refundingInstrument = new stdClass();
83
            $refund->data->refundingInstrument->method = $this->getIfSet('method', $response->refundingInstrument);
84
85
            if (isset($response->refundingInstrument->bankAccount)) {
86
                $refund->data->refundingInstrument->bankAccount = new stdClass();
87
                $refund->data->refundingInstrument->bankAccount->bankNumber = $this->getIfSet('bankNumber', $response->refundingInstrument->bankAccount);
88
                $refund->data->refundingInstrument->bankAccount->bankName = $this->getIfSet('bankName', $response->refundingInstrument->bankAccount);
89
                $refund->data->refundingInstrument->bankAccount->agencyNumber = $this->getIfSet('agencyNumber', $response->refundingInstrument->bankAccount);
90
                $refund->data->refundingInstrument->bankAccount->agencyCheckNumber = $this->getIfSet('agencyCheckNumber', $response->refundingInstrument->bankAccount);
91
                $refund->data->refundingInstrument->bankAccount->accountNumber = $this->getIfSet('accountNumber', $response->refundingInstrument->bankAccount);
92
                $refund->data->refundingInstrument->bankAccount->accountCheckNumber = $this->getIfSet('accountCheckNumber', $response->refundingInstrument->bankAccount);
93
                $refund->data->refundingInstrument->bankAccount->agencyCheckNumber = $this->getIfSet('agencyCheckNumber', $response->refundingInstrument->bankAccount);
94
                $refund->data->refundingInstrument->bankAccount->type = $this->getIfSet('type', $response->refundingInstrument->bankAccount);
95
            }
96
        }
97
98
        $refund->data->status = $this->getIfSet('status', $response);
99
        $refund->data->method = $this->getIfSet('method', $response);
100
        $refund->data->createdAt = $this->getIfSet('createdAt', $response);
101
        $refund->data->_links = $this->getIfSet('_links', $response);
102
103
        return $refund;
104
    }
105
106
    /**
107
     * Get id MoIP refund.
108
     *
109
     *
110
     * @return string
111
     */
112
    public function getId()
113
    {
114
        return $this->getIfSet('id');
115
    }
116
117
    /**
118
     * Create a new refund in api MoIP.
119
     *
120
     * @param stdClass $data
121
     *
122
     * @return $this
123
     */
124
    private function execute(stdClass $data = null)
125
    {
126
        $body = empty($data) ? new stdClass() : $data;
127
        $response = $this->httpRequest($this->getPath(), Requests::POST, $body);
128
129
        return $this->populate($response);
130
    }
131
132
    /**
133
     * Checks path that will be the request.
134
     *
135
     * @return string
136
     */
137
    private function getPath()
138
    {
139
        if ($this->order !== null) {
140
            return sprintf('/%s/%s/%s/%s', MoipResource::VERSION, Orders::PATH, $this->order->getId(), self::PATH);
141
        }
142
143
        return sprintf('/%s/%s/%s/%s', MoipResource::VERSION, Payment::PATH, $this->payment->getId(), self::PATH);
144
    }
145
146
    /**
147
     * Bank account is the bank address of a particular vendor or a customer.
148
     *
149
     * @param string                  $type               Kind of refund. possible values: FULL, PARTIAL.
150
     * @param string                  $bankNumber         Bank number. possible values: 001, 237, 341, 041.
151
     * @param int                     $agencyNumber       Branch number.
152
     * @param int                     $agencyCheckNumber  Checksum of the agency.
153
     * @param int                     $accountNumber      Account number.
154
     * @param int                     $accountCheckNumber Digit account checker.
155
     * @param \Moip\Resource\Customer $holder
156
     *
157
     * @return \stdClass
158
     */
159
    private function bankAccount($type, $bankNumber, $agencyNumber, $agencyCheckNumber, $accountNumber, $accountCheckNumber, Customer $holder)
160
    {
161
        $data = new stdClass();
162
        $data->refundingInstrument = self::METHOD_BANK_ACCOUNT;
163
        $data->bankAccount = new stdClass();
164
        $data->bankAccount->type = $type;
165
        $data->bankAccount->bankNumber = $bankNumber;
166
        $data->bankAccount->agencyNumber = $agencyNumber;
167
        $data->bankAccount->agencyCheckNumber = $agencyCheckNumber;
168
        $data->bankAccount->accountNumber = $accountNumber;
169
        $data->bankAccount->accountCheckNumber = $accountCheckNumber;
170
        $data->bankAccount->holder = new stdClass();
171
        $data->bankAccount->holder->fullname = $holder->getFullname();
172
        $data->bankAccount->holder->taxDocument = new stdClass();
173
        $data->bankAccount->holder->taxDocument->type = $holder->getTaxDocumentType();
174
        $data->bankAccount->holder->taxDocument->number = $holder->getTaxDocumentNumber();
175
176
        return $data;
177
    }
178
179
    /**
180
     * Making a full refund to the bank account.
181
     *
182
     * @param string                  $type               Kind of refund. possible values: FULL, PARTIAL.
183
     * @param string                  $bankNumber         Bank number. possible values: 001, 237, 341, 041.
184
     * @param int                     $agencyNumber       Branch number.
185
     * @param int                     $agencyCheckNumber  Checksum of the agency.
186
     * @param int                     $accountNumber      Account number.
187
     * @param int                     $accountCheckNumber Digit account checker.
188
     * @param \Moip\Resource\Customer $holder
189
     *
190
     * @return Refund
191
     */
192
    public function bankAccountFull($type, $bankNumber, $agencyNumber, $agencyCheckNumber, $accountNumber, $accountCheckNumber, Customer $holder)
193
    {
194
        $data = $this->bankAccount($type, $bankNumber, $agencyNumber, $agencyCheckNumber, $accountNumber, $accountCheckNumber, $holder);
195
196
        return $this->execute($data);
197
    }
198
199
    /**
200
     * Making a partial refund in the bank account.
201
     *
202
     * @param string                  $type               Kind of refund. possible values: FULL, PARTIAL.
203
     * @param string                  $bankNumber         Bank number. possible values: 001, 237, 341, 041.
204
     * @param int                     $agencyNumber       Branch number.
205
     * @param int                     $agencyCheckNumber  Checksum of the agency.
206
     * @param int                     $accountNumber      Account number.
207
     * @param int                     $accountCheckNumber Digit account checker.
208
     * @param \Moip\Resource\Customer $holder
209
     *
210
     * @return Refund
211
     */
212
    public function bankAccountPartial($amount, $type, $bankNumber, $agencyNumber, $agencyCheckNumber, $accountNumber, $accountCheckNumber, Customer $holder)
213
    {
214
        $data = $this->bankAccount($type, $bankNumber, $agencyNumber, $agencyCheckNumber, $accountNumber, $accountCheckNumber, $holder);
215
        $data->amount = $amount;
216
217
        return $this->execute($data);
218
    }
219
220
    /**
221
     * Making a full refund in credit card.
222
     *
223
     * @return \Moip\Resource\Refund
224
     */
225
    public function creditCardFull()
226
    {
227
        return $this->execute();
228
    }
229
230
    /**
231
     * Making a partial refund in credit card.
232
     *
233
     * @param int|float $amount value of refund.
234
     *
235
     * @return \Moip\Resource\Refund
236
     */
237
    public function creditCardPartial($amount)
238
    {
239
        $data = new stdClass();
240
        $data->amount = $amount;
241
242
        return $this->execute($data);
243
    }
244
245
    /**
246
     * Get iterator.
247
     *
248
     * @return \ArrayIterator
249
     */
250
    public function getIterator()
251
    {
252
        $refunds = [];
253
        $response = $this->httpRequest($this->getPath(), Requests::GET);
254
        foreach ($response->refunds as $refund) {
255
            $refunds[] = $this->populate($refund);
256
        }
257
258
        return new ArrayIterator($refunds);
259
    }
260
261
    /**
262
     * Set order.
263
     *
264
     * @param \Moip\Resource\Orders $order
265
     */
266
    public function setOrder(Orders $order)
267
    {
268
        $this->order = $order;
269
    }
270
271
    /**
272
     * Set payment.
273
     *
274
     * @param \Moip\Resource\Payment $payment
275
     */
276
    public function setPayment(Payment $payment)
277
    {
278
        $this->payment = $payment;
279
    }
280
}
281