CreateParser::getCustomerPostData()   F
last analyzed

Complexity

Conditions 13
Paths 4096

Size

Total Lines 60
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 60
ccs 0
cts 30
cp 0
rs 2.45
c 0
b 0
f 0
cc 13
nc 4096
nop 1
crap 182

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\eCurring\Http\Resource;
6
7
use DateTime;
8
use LauLamanApps\eCurring\Http\Resource\Exception\UnPostableEntityException;
9
use LauLamanApps\eCurring\Resource\Customer;
10
use LauLamanApps\eCurring\Resource\Subscription;
11
use LauLamanApps\eCurring\Resource\Transaction;
12
13
final class CreateParser implements CreateParserInterface
14
{
15
    /**
16
     * @throws UnPostableEntityException
17
     */
18
    public function parse(Creatable $object): array
19
    {
20
        if ($object instanceof Customer) {
21
            return $this->getCustomerPostData($object);
22
        }
23
24
        if ($object instanceof Subscription) {
25
            return $this->getSubscriptionPostData($object);
26
        }
27
28
        if ($object instanceof Transaction) {
29
            return $this->getTransactionPostData($object);
30
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 28 is false. This is incompatible with the type-hinted return array. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
31
    }
32
33
34
    private function getCustomerPostData(Customer $customer): array
35
    {
36
        $data = [
37
            'first_name' => $customer->getFirstName(),
38
            'last_name' => $customer->getLastName(),
39
            'email' => $customer->getEmail(),
40
        ];
41
42
        if ($customer->getGender()) {
43
            $data['gender'] = $customer->getGender();
44
        }
45
46
        if ($customer->getMiddleName()) {
47
            $data['middle_name'] = $customer->getMiddleName();
48
        }
49
50
        if ($customer->getCompanyName()) {
51
            $data['company_name'] = $customer->getCompanyName();
52
        }
53
54
        if ($customer->getVatNumber()) {
55
            $data['vat_number'] = $customer->getVatNumber();
56
        }
57
58
        if ($customer->getPostalcode()) {
59
            $data['postalcode'] = $customer->getPostalcode();
60
        }
61
62
        if ($customer->getHouseNumber()) {
63
            $data['house_number'] = $customer->getHouseNumber();
64
        }
65
66
        if ($customer->getHouseNumberAdd()) {
67
            $data['house_number_add'] = $customer->getHouseNumberAdd();
68
        }
69
70
        if ($customer->getStreet()) {
71
            $data['street'] = $customer->getStreet();
72
        }
73
74
        if ($customer->getCity()) {
75
            $data['city'] = $customer->getCity();
76
        }
77
78
        if ($customer->getCountryCode()) {
79
            $data['country_iso2'] = $customer->getCountryCode();
80
        }
81
82
        if ($customer->getLanguage()) {
83
            $data['language'] = $customer->getLanguage();
84
        }
85
86
        if ($customer->getTelephone()) {
87
            $data['telephone '] = $customer->getTelephone();
88
        }
89
90
        return [
91
            'data' => [
92
                'type' => 'customer',
93
                'attributes' => $data
94
            ]
95
        ];
96
    }
97
98
    /**
99
     * @throws UnPostableEntityException
100
     */
101
    private function getSubscriptionPostData(Subscription $subscription): array
102
    {
103
        if (!$subscription->getCustomer()) {
104
            throw new UnPostableEntityException('Missing required field `customer`. Make sure you create a new subscription using Subscription::new()');
105
        }
106
107
        if (!$subscription->getSubscriptionPlan()) {
108
            throw new UnPostableEntityException('Missing required field `subscriptionPlan`. Make sure you create a new subscription using Subscription::new()');
109
        }
110
111
        $data = [
112
            'customer_id' => $subscription->getCustomer()->getId(),
0 ignored issues
show
Bug introduced by
The method getId() does not exist on LauLamanApps\eCurring\Resource\CustomerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to LauLamanApps\eCurring\Resource\CustomerInterface. ( Ignorable by Annotation )

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

112
            'customer_id' => $subscription->getCustomer()->/** @scrutinizer ignore-call */ getId(),
Loading history...
113
            'subscription_plan_id' => $subscription->getSubscriptionPlan()->getId(),
0 ignored issues
show
Bug introduced by
The method getId() does not exist on LauLamanApps\eCurring\Resource\ProductInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to LauLamanApps\eCurring\Resource\ProductInterface. ( Ignorable by Annotation )

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

113
            'subscription_plan_id' => $subscription->getSubscriptionPlan()->/** @scrutinizer ignore-call */ getId(),
Loading history...
114
        ];
115
116
        if ($subscription->getMandate()) {
117
            $data['mandate_code'] = $subscription->getMandate()->getCode();
118
            $data['mandate_accepted'] = $subscription->getMandate()->isAccepted();
119
            $data['mandate_accepted_date'] = $subscription->getMandate()->getAcceptedDate()->format(DateTime::ATOM);
120
        }
121
122
        if ($subscription->getStartDate()) {
123
            $data['start_date '] = $subscription->getStartDate()->format(DateTime::ATOM);
124
        }
125
126
        if ($subscription->getStatus()) {
127
            $data['status '] = $subscription->getStatus()->getValue();
128
        }
129
130
        if ($subscription->getCancelDate()) {
131
            $data['cancel_date '] = $subscription->getCancelDate()->format(DateTime::ATOM);
132
        }
133
134
        if ($subscription->getResumeDate()) {
135
            $data['resume_date '] = $subscription->getResumeDate()->format(DateTime::ATOM);
136
        }
137
138
        if ($subscription->isConfirmationSent() !== null) {
0 ignored issues
show
introduced by
The condition $subscription->isConfirmationSent() !== null is always true.
Loading history...
139
            $data['confirmation_sent '] = $subscription->isConfirmationSent();
140
        }
141
142
        if ($subscription->getSubscriptionWebhookUrl()) {
143
            $data['subscription_webhook_url '] = $subscription->getSubscriptionWebhookUrl();
144
        }
145
146
        if ($subscription->getTransactionWebhookUrl()) {
147
            $data['transaction_webhook_url '] = $subscription->getTransactionWebhookUrl();
148
        }
149
150
        if ($subscription->getSuccessRedirectUrl()) {
151
            $data['success_redirect_url '] = $subscription->getSuccessRedirectUrl();
152
        }
153
154
        return [
155
            'data' => [
156
                'type' => 'subscription',
157
                'attributes' => $data
158
            ]
159
        ];
160
    }
161
162
    /**
163
     * @throws UnPostableEntityException
164
     */
165
    private function getTransactionPostData(Transaction $transaction): array
166
    {
167
        if (!$transaction->getSubscription()) {
168
            throw new UnPostableEntityException('No Subscription found on Transaction. Make sure you create a new transaction using Transaction::new()');
169
        }
170
171
        $data = [
172
            'subscription_id' => $transaction->getSubscription()->getId(),
173
            'amount' => ((float)$transaction->getAmount()->getAmount()/100.00),
174
        ];
175
176
        if ($transaction->getDueDate()) {
177
            $data['due_date'] = $transaction->getDueDate()->format(DateTime::ATOM);
178
        }
179
180
        return $data;
181
    }
182
}
183