Test Failed
Push — master ( 6b615f...c406fd )
by P.R.
02:45
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 and retracts an invoice.
140
   *
141
   * @param string      $invoiceId Invoice ID for the Invoice to be credited.
142
   *
143
   * @param string      $externalInvoiceNumber
144
   * @param string      $description
145
   * @param string|null $retractionReason
146
   * @param bool|null   $showRetractionReasonToCustomer
147
   *
148
   * @return Invoice
149
   *
150
   * @throws ClubCollectApiException
151
   */
152
  public function creditAndRetract(string $invoiceId,
153
                                   string $externalInvoiceNumber,
154
                                   string $description,
155
                                   ?string $retractionReason,
156
                                   ?bool $showRetractionReasonToCustomer): Invoice
157
  {
158
    /** @var Invoice $resource */
159
    $resource = parent::restPost(['invoices', $invoiceId, 'credit_and_retract'],
160
                                 ['api_key' => $this->client->getApiKey()],
161
                                 ['external_invoice_number'            => $externalInvoiceNumber,
162
                                  'description'                        => $description,
163
                                  'retraction_reason'                  => $retractionReason,
164
                                  'show_retraction_reason_to_customer' => $showRetractionReasonToCustomer]);
165
166
    if (!is_a($resource, Invoice::class))
167
    {
168
      throw new ClubCollectApiException('Expected an Invoice object, got a %s', get_class($resource));
169
    }
170
171
    return $resource;
172
  }
173
174
  //--------------------------------------------------------------------------------------------------------------------
175
  /**
176
   * Deletes an invoice from ClubCollect.
177
   *
178
   * @param string $invoiceId The invoice ID, supplied by ClubCollect.
179
   *
180
   * @throws ClubCollectApiException
181
   */
182
  public function delete(string $invoiceId): void
183
  {
184
    parent::restDelete(['invoices', $invoiceId],
185
                       ['api_key' => $this->client->getApiKey()]);
186
  }
187
188
  //--------------------------------------------------------------------------------------------------------------------
189
  /**
190
   * Fetches an invoice from ClubCollect.
191
   *
192
   * @param string $invoiceId The invoice ID, supplied by ClubCollect.
193
   *
194
   * @return Invoice
195
   *
196
   * @throws ClubCollectApiException
197
   */
198
  public function fetch(string $invoiceId): Invoice
199
  {
200
    /** @var Invoice $resource */
201
    $resource = parent::restGet(['invoices', $invoiceId],
202
                                ['api_key' => $this->client->getApiKey()]);
203
    if (!is_a($resource, Invoice::class))
204
    {
205
      throw new ClubCollectApiException('Expected an Invoice object, got a %s', get_class($resource));
206
    }
207
208
    return $resource;
209
  }
210
  //--------------------------------------------------------------------------------------------------------------------
211
  /**
212
   * Updates an invoice.
213
   *
214
   * @param string      $invoiceId        ID of the invoice, supplied by ClubCollect.
215
   * @param string      $externalInvoiceNumber
216
   * @param string|null $reference
217
   * @param string|null $directDebitIban  When supplied, will be accepted and added to the Invoice only if it is a
218
   *                                      valid IBAN.
219
   * @param string|null $federationMembershipNumber
220
   * @param string|null $clubMembershipNumber
221
   * @param string|null $locale           When supplied, the invoice's locale will be set to this value. From { de en
222
   *                                      fr it nl }
223
   * @param array       $customerName
224
   * @param array       $customerAddress
225
   * @param array       $customerEmail
226
   * @param array       $customerPhone
227
   *
228
   * @return Invoice
229
   *
230
   * @throws ClubCollectApiException
231
   */
232
  public function update(string $invoiceId,
233
                         string $externalInvoiceNumber,
234
                         ?string $reference,
235
                         ?string $directDebitIban,
236
                         ?string $federationMembershipNumber,
237
                         ?string $clubMembershipNumber,
238
                         ?string $locale,
239
                         array $customerName,
240
                         array $customerAddress,
241
                         array $customerEmail,
242
                         array $customerPhone): Invoice
243
  {
244
    /** @var Invoice $resource */
245
    $resource = parent::restPut(
246
      ['invoices', $invoiceId],
247
      ['api_key' => $this->client->getApiKey()],
248
      ['external_invoice_number'      => $externalInvoiceNumber,
249
       'reference'                    => $reference,
250
       'direct_debit_iban'            => $directDebitIban,
251
       'federation_membership_number' => Cast::toOptString($federationMembershipNumber),
252
       'club_membership_number'       => Cast::toOptString($clubMembershipNumber),
253
       'locale'                       => $locale,
254
       'customer'                     => self::composeCustomer($customerName,
255
                                                               $customerAddress,
256
                                                               $customerEmail,
257
                                                               $customerPhone)]);
258
    if (!is_a($resource, Invoice::class))
259
    {
260
      throw new ClubCollectApiException('Expected an Invoice object, got a %s', get_class($resource));
261
    }
262
263
    return $resource;
264
  }
265
266
  //--------------------------------------------------------------------------------------------------------------------
267
  /**
268
   * Returns an instance of this class.
269
   *
270
   * @param array $response The API response.
271
   *
272
   * @return Invoice
273
   *
274
   * @throws ClubCollectApiException
275
   */
276
  protected function createResourceObject(array $response): BaseResource
277
  {
278
    return new Invoice($this->client, $response);
279
  }
280
281
  //--------------------------------------------------------------------------------------------------------------------
282
}
283
284
//----------------------------------------------------------------------------------------------------------------------
285