Test Failed
Push — master ( 78a1a9...6b615f )
by P.R.
02:38
created

InvoiceEndpoint::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 37
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 19
c 2
b 0
f 0
nc 2
nop 13
dl 0
loc 37
rs 9.6333

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