Test Failed
Branch master (4f0c15)
by Laurens
01:38
created

CreateParser::getCustomerPostData()   F

Complexity

Conditions 13
Paths 4096

Size

Total Lines 57
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 57
rs 2.45
c 0
b 0
f 0
cc 13
nc 4096
nop 1

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 $data;
91
    }
92
93
    /**
94
     * @throws UnPostableEntityException
95
     */
96
    private function getSubscriptionPostData(Subscription $subscription): array
97
    {
98
        if (!$subscription->getCustomer()) {
99
            throw new UnPostableEntityException('Missing required field `customer`. Make sure you create a new subscription using Subscription::new()');
100
        }
101
102
        if (!$subscription->getSubscriptionPlan()) {
103
            throw new UnPostableEntityException('Missing required field `subscriptionPlan`. Make sure you create a new subscription using Subscription::new()');
104
        }
105
106
        $data = [
107
            '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

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

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

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