Passed
Push — main ( cade56...9c07c4 )
by Michael
05:36 queued 02:40
created

attachPaymentMethodToCustomer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\StripeIntegration\Behaviors;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Laravel\Cashier\PaymentMethod as CashierPaymentMethod;
9
use MichaelRubel\StripeIntegration\DataTransferObjects\PaymentMethodAttachmentData;
10
use Stripe\Customer;
11
use Stripe\PaymentMethod;
12
13
trait PreparesPaymentMethods
14
{
15
    /**
16
     * Prepare the customer.
17
     *
18
     * @param  Model  $model
19
     *
20
     * @return Customer
21
     */
22 3
    public function makeCustomerUsing(Model $model): Customer
23
    {
24 3
        return call($model)->createOrGetStripeCustomer();
25
    }
26
27
    /**
28
     * Update the default payment method.
29
     *
30
     * @param  Model  $model
31
     * @param  PaymentMethod|string  $paymentMethod
32
     *
33
     * @return CashierPaymentMethod
34
     */
35 5
    public function setPaymentMethodFor(Model $model, PaymentMethod|string $paymentMethod): CashierPaymentMethod
36
    {
37 5
        return call($model)->updateDefaultPaymentMethod($paymentMethod);
38
    }
39
40
    /**
41
     * Attach the payment method to the customer.
42
     *
43
     * @param  PaymentMethodAttachmentData  $data
44
     *
45
     * @return PaymentMethod
46
     */
47 3
    public function attachPaymentMethodToCustomer(PaymentMethodAttachmentData $data): PaymentMethod
48
    {
49 3
        $params = collect(['customer' => $data->customer->id])
0 ignored issues
show
Bug introduced by
array('customer' => $data->customer->id) of type array<string,string> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
        $params = collect(/** @scrutinizer ignore-type */ ['customer' => $data->customer->id])
Loading history...
50 3
            ->merge($data->params)
0 ignored issues
show
Bug introduced by
$data->params of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::merge(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
            ->merge(/** @scrutinizer ignore-type */ $data->params)
Loading history...
51 3
            ->toArray();
52
53 3
        return call($this->stripeClient->paymentMethods)->attach(
54 3
            $data->paymentMethod->id, $params, $data->options
55
        );
56
    }
57
}
58