ImportMandateAction::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace GoCardlessPayment\Actions\Imports;
4
5
use GoCardlessPayment\GoCardlessPayment;
6
use GoCardlessPayment\Makeable;
7
use GoCardlessPayment\Models\GoCardlessMandate;
8
use GoCardlessPro\Resources\Mandate;
9
10
class ImportMandateAction
11
{
12
    use Makeable;
13
14
    public readonly string $mandateId;
15
16 6
    public function __construct(string $mandateId)
17
    {
18 6
        $this->mandateId = $mandateId;
0 ignored issues
show
Bug introduced by
The property mandateId is declared read-only in GoCardlessPayment\Action...rts\ImportMandateAction.
Loading history...
19
    }
20
21 6
    public function execute(): GoCardlessMandate
22
    {
23
        /** @var Mandate $mandate */
24 6
        $mandate = GoCardlessPayment::api()->mandates()->get($this->mandateId);
0 ignored issues
show
Bug introduced by
The method mandates() does not exist on GoCardlessPayment\Api. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

24
        $mandate = GoCardlessPayment::api()->/** @scrutinizer ignore-call */ mandates()->get($this->mandateId);
Loading history...
25
26 6
        $customerId = data_get($mandate->links, 'customer');
27
28 6
        $localCustomer = GoCardlessPayment::localCustomerRepository()->findLocalCustomer($customerId);
29 6
        if (! $localCustomer) {
30 2
            throw new \Exception("Customer [{$customerId}] not found in local storage.");
31
        }
32
33 4
        $reflect = new \ReflectionClass($mandate);
34 4
        $props = $reflect->getProperties(\ReflectionProperty::IS_PROTECTED);
35
36 4
        $data = [];
37 4
        foreach ($props as $prop) {
38 4
            $data[$prop->getName()] = $prop->getValue($mandate);
39
        }
40
41 4
        return GoCardlessPayment::modelClass('mandate')::updateOrCreate(
42 4
            [
43 4
                'id' => $mandate->id,
44 4
            ],
45 4
            [
46 4
                'customer_id' => $customerId,
47 4
                'creditor_id' => data_get($mandate->links, 'creditor'),
48 4
                'customer_bank_account_id' => data_get($mandate->links, 'customer_bank_account'),
49 4
                'status' => $mandate->status,
50 4
                'reference' => $mandate->reference,
51 4
                'data' => $data,
52 4
            ]
53 4
        );
54
    }
55
}
56