Test Failed
Push — master ( c62a67...fdb875 )
by P.R.
02:23
created

InvoiceEndpoint::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 44
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 28
nc 2
nop 11
dl 0
loc 44
rs 9.472
c 1
b 0
f 0

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
declare(strict_types=1);
3
4
namespace SetBased\ClubCollect\Endpoint;
5
6
use SetBased\ClubCollect\Exception\ClubCollectApiException;
7
use SetBased\ClubCollect\Helper\Cast;
8
use SetBased\ClubCollect\Resource\BaseResource;
9
use SetBased\ClubCollect\Resource\Invoice;
10
11
/**
12
 * Endpoint for companies.
13
 */
14
class InvoiceEndpoint extends Endpoint
15
{
16
  //--------------------------------------------------------------------------------------------------------------------
17
  /**
18
   * Creates an invoice.
19
   *
20
   * @param string      $importId         ID of Import to which the Invoice should belong.
21
   * @param string      $externalInvoiceNumber
22
   * @param string|null $reference
23
   * @param string|null $directDebitIban  When supplied, will be accepted and added to the Invoice only if it is a
24
   *                                      valid IBAN.
25
   * @param string|null $federationMembershipNumber
26
   * @param string|null $clubMembershipNumber
27
   * @param string|null $locale           When supplied, the invoice's locale will be set to this value. From { de en
28
   *                                      fr it nl }
29
   * @param array       $customerName
30
   * @param array       $customerAddress
31
   * @param array       $customerEmail
32
   * @param array       $customerPhone
33
   * @param array       $invoiceLines
34
   * @param int         $amountTotalCents Must be equal to the sum of the amounts of the Invoice Lines. May be zero or
35
   *                                      negative.
36
   *
37
   * @return Invoice
38
   *
39
   * @throws ClubCollectApiException
40
   */
41
  public function create(string $importId,
42
                         string $externalInvoiceNumber,
43
                         ?string $reference,
44
                         ?string $directDebitIban,
45
                         ?string $federationMembershipNumber,
46
                         ?string $clubMembershipNumber,
47
                         ?string $locale,
48
                         array $customerName,
49
                         array $customerAddress,
50
                         array $customerEmail,
51
                         array $customerPhone,
52
                         array $invoiceLines,
53
                         int $amountTotalCents): Invoice
54
  {
55
    /** @var Invoice $resource */
56
    $resource = parent::restPost(
57
      ['invoices'],
58
      ['api_key' => $this->client->getApiKey()],
59
      ['import_id'                    => $importId,
60
       'external_invoice_number'      => $externalInvoiceNumber,
61
       'reference'                    => $reference,
62
       'direct_debit_iban'            => $directDebitIban,
63
       'federation_membership_number' => Cast::toOptString($federationMembershipNumber),
64
       'club_membership_number'       => Cast::toOptString($clubMembershipNumber),
65
       'locale'                       => $locale,
66
       'customer'                     => ['name'    => ['prefix'       => $customerName['prefix'] ?? null,
67
                                                        'first_name'   => $customerName['first_name'] ?? null,
68
                                                        'infix'        => $customerName['infix'] ?? null,
69
                                                        'last_name'    => $customerName['last_name'] ?? null,
70
                                                        'organization' => $customerName['organization'] ?? null],
71
                                          'address' => ['address1'     => $customerAddress['address1'] ?? null,
72
                                                        'address2'     => $customerAddress['address2'] ?? null,
73
                                                        'locality'     => $customerAddress['locality'] ?? null,
74
                                                        'house_number' => Cast::toOptString($customerAddress['house_number'] ?? null),
75
                                                        'state'        => $customerAddress['state'] ?? null,
76
                                                        'zipcode'      => $customerAddress['zip_code'] ?? null,
77
                                                        'city'         => $customerAddress['city'] ?? null,
78
                                                        'country_code' => $customerAddress['country_code'] ?? null],
79
                                          'email'   => ['email_address' => $customerEmail['email_address'] ?? null],
80
                                          'phone'   => ['phone_number' => $customerPhone['phone_number'] ?? null,
81
                                                        'country_code' => $customerPhone['country_code'] ?? null]],
82
       'invoice_lines'                => $this->composeInvoiceLines($invoiceLines),
83
       'amount_total_cents'           => $amountTotalCents]);
84
    if (!is_a($resource, Invoice::class))
85
    {
86
      throw new ClubCollectApiException('Expected an Invoice object, got a %s', get_class($resource));
87
    }
88
89
    return $resource;
90
  }
91
92
  //--------------------------------------------------------------------------------------------------------------------
93
  /**
94
   * Credits an invoice.
95
   *
96
   * @param string  $invoiceId Invoice ID for the Invoice to be credited.
97
   * @param string  $externalInvoiceNumber
98
   * @param array[] $invoiceLines
99
   * @param int     $amountTotalCents
100
   *
101
   * @return Invoice
102
   *
103
   * @throws ClubCollectApiException
104
   */
105
  public function credit(string $invoiceId,
106
                         string $externalInvoiceNumber,
107
                         array $invoiceLines,
108
                         int $amountTotalCents): Invoice
109
  {
110
    /** @var Invoice $resource */
111
    $resource = parent::restPost(['invoices', $invoiceId, 'credit'],
112
                                 ['api_key' => $this->client->getApiKey()],
113
                                 ['external_invoice_number' => $externalInvoiceNumber,
114
                                  'invoice_lines'           => $this->composeInvoiceLines($invoiceLines),
115
                                  'amount_total_cents'      => $amountTotalCents]);
116
117
    if (!is_a($resource, Invoice::class))
118
    {
119
      throw new ClubCollectApiException('Expected an Invoice object, got a %s', get_class($resource));
120
    }
121
122
    return $resource;
123
  }
124
125
  //--------------------------------------------------------------------------------------------------------------------
126
  /**
127
   * Credits and retracts an invoice.
128
   *
129
   * @param string      $invoiceId Invoice ID for the Invoice to be credited.
130
   *
131
   * @param string      $externalInvoiceNumber
132
   * @param string      $description
133
   * @param string|null $retractionReason
134
   * @param bool|null   $showRetractionReasonToCustomer
135
   *
136
   * @return Invoice
137
   *
138
   * @throws ClubCollectApiException
139
   */
140
  public function creditAndRetract(string $invoiceId,
141
                                   string $externalInvoiceNumber,
142
                                   string $description,
143
                                   ?string $retractionReason,
144
                                   ?bool $showRetractionReasonToCustomer): Invoice
145
  {
146
    /** @var Invoice $resource */
147
    $resource = parent::restPost(['invoices', $invoiceId, 'credit_and_retract'],
148
                                 ['api_key' => $this->client->getApiKey()],
149
                                 ['external_invoice_number'            => $externalInvoiceNumber,
150
                                  'description'                        => $description,
151
                                  'retraction_reason'                  => $retractionReason,
152
                                  'show_retraction_reason_to_customer' => $showRetractionReasonToCustomer]);
153
154
    if (!is_a($resource, Invoice::class))
155
    {
156
      throw new ClubCollectApiException('Expected an Invoice object, got a %s', get_class($resource));
157
    }
158
159
    return $resource;
160
  }
161
162
  //--------------------------------------------------------------------------------------------------------------------
163
  /**
164
   * Deletes an invoice from ClubCollect.
165
   *
166
   * @param string $invoiceId The invoice ID, supplied by ClubCollect.
167
   *
168
   * @throws ClubCollectApiException
169
   */
170
  public function delete(string $invoiceId): void
171
  {
172
    parent::restDelete(['invoices', $invoiceId],
173
                       ['api_key' => $this->client->getApiKey()]);
174
  }
175
176
  //--------------------------------------------------------------------------------------------------------------------
177
  /**
178
   * Fetches an invoice from ClubCollect.
179
   *
180
   * @param string $invoiceId The invoice ID, supplied by ClubCollect.
181
   *
182
   * @return Invoice
183
   *
184
   * @throws ClubCollectApiException
185
   */
186
  public function fetch(string $invoiceId): Invoice
187
  {
188
    /** @var Invoice $resource */
189
    $resource = parent::restRead(['invoices', $invoiceId],
190
                                 ['api_key' => $this->client->getApiKey()]);
191
    if (!is_a($resource, Invoice::class))
192
    {
193
      throw new ClubCollectApiException('Expected an Invoice object, got a %s', get_class($resource));
194
    }
195
196
    return $resource;
197
  }
198
199
  //--------------------------------------------------------------------------------------------------------------------
200
  /**
201
   * Updates an invoice.
202
   *
203
   * @param string      $invoiceId        ID of the invoice, supplied by ClubCollect.
204
   * @param string      $externalInvoiceNumber
205
   * @param string|null $reference
206
   * @param string|null $directDebitIban  When supplied, will be accepted and added to the Invoice only if it is a
207
   *                                      valid IBAN.
208
   * @param string|null $federationMembershipNumber
209
   * @param string|null $clubMembershipNumber
210
   * @param string|null $locale           When supplied, the invoice's locale will be set to this value. From { de en
211
   *                                      fr it nl }
212
   * @param array       $customerName
213
   * @param array       $customerAddress
214
   * @param array       $customerEmail
215
   * @param array       $customerPhone
216
   *
217
   * @return Invoice
218
   *
219
   * @throws ClubCollectApiException
220
   */
221
  public function update(string $invoiceId,
222
                         string $externalInvoiceNumber,
223
                         ?string $reference,
224
                         ?string $directDebitIban,
225
                         ?string $federationMembershipNumber,
226
                         ?string $clubMembershipNumber,
227
                         ?string $locale,
228
                         array $customerName,
229
                         array $customerAddress,
230
                         array $customerEmail,
231
                         array $customerPhone): Invoice
232
  {
233
    /** @var Invoice $resource */
234
    $resource = parent::restPut(
235
      ['invoices', $invoiceId],
236
      ['api_key' => $this->client->getApiKey()],
237
      ['external_invoice_number'      => $externalInvoiceNumber,
238
       'reference'                    => $reference,
239
       'direct_debit_iban'            => $directDebitIban,
240
       'federation_membership_number' => Cast::toOptString($federationMembershipNumber),
241
       'club_membership_number'       => Cast::toOptString($clubMembershipNumber),
242
       'locale'                       => $locale,
243
       'customer'                     => ['name'    => ['prefix'       => $customerName['prefix'] ?? null,
244
                                                        'first_name'   => $customerName['first_name'] ?? null,
245
                                                        'infix'        => $customerName['infix'] ?? null,
246
                                                        'last_name'    => $customerName['last_name'] ?? null,
247
                                                        'organization' => $customerName['organization'] ?? null],
248
                                          'address' => ['address1'     => $customerAddress['address1'] ?? null,
249
                                                        'address2'     => $customerAddress['address2'] ?? null,
250
                                                        'locality'     => $customerAddress['locality'] ?? null,
251
                                                        'house_number' => Cast::toOptString($customerAddress['house_number'] ?? null),
252
                                                        'state'        => $customerAddress['state'] ?? null,
253
                                                        'zipcode'      => $customerAddress['zip_code'] ?? null,
254
                                                        'city'         => $customerAddress['city'] ?? null,
255
                                                        'country_code' => $customerAddress['country_code'] ?? null],
256
                                          'email'   => ['email_address' => $customerEmail['email_address'] ?? null],
257
                                          'phone'   => ['phone_number' => $customerPhone['phone_number'] ?? null,
258
                                                        'country_code' => $customerPhone['country_code'] ?? null]]]);
259
    if (!is_a($resource, Invoice::class))
260
    {
261
      throw new ClubCollectApiException('Expected an Invoice object, got a %s', get_class($resource));
262
    }
263
264
    return $resource;
265
  }
266
267
  //--------------------------------------------------------------------------------------------------------------------
268
  /**
269
   * Returns an instance of this class.
270
   *
271
   * @param array $response The API response.
272
   *
273
   * @return Invoice
274
   *
275
   * @throws ClubCollectApiException
276
   */
277
  protected function getResourceObject(array $response): BaseResource
278
  {
279
    return new Invoice($this->client, $response);
280
  }
281
282
  //--------------------------------------------------------------------------------------------------------------------
283
  /**
284
   * Composes the invoice lines part of the request body.
285
   *
286
   * @param array[] $invoiceLines The data of the invoice lines.
287
   *
288
   * @return array[]
289
   */
290
  private function composeInvoiceLines(array $invoiceLines): array
291
  {
292
    $ret = [];
293
294
    foreach ($invoiceLines as $invoiceLine)
295
    {
296
      $ret[] = ['invoice_line_id' => $invoiceLine['invoice_line_id'] ?? null,
297
                'type'            => $invoiceLine['type'] ?? null,
298
                'amount_cents'    => $invoiceLine['amount_cents'],
299
                'description'     => $invoiceLine['description'],
300
                'date'            => $invoiceLine['date'] ?? date('c')];
301
    }
302
303
    return $ret;
304
  }
305
306
  //--------------------------------------------------------------------------------------------------------------------
307
}
308
309
//----------------------------------------------------------------------------------------------------------------------
310