Completed
Push — master ( 24d46d...ba1824 )
by Olivier
01:53
created

EventTrait::createFromArray()   D

Complexity

Conditions 19
Paths 36

Size

Total Lines 73

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 64
CRAP Score 19.0013

Importance

Changes 0
Metric Value
dl 0
loc 73
ccs 64
cts 65
cp 0.9846
rs 4.5166
c 0
b 0
f 0
cc 19
nc 36
nop 1
crap 19.0013

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
/*
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license. See the LICENSE file for details.
8
 */
9
10
namespace Shapin\Stripe\Model\Event;
11
12
use Shapin\Stripe\Exception\InvalidArgumentException;
13
use Shapin\Stripe\Model\LivemodeTrait;
14
use Shapin\Stripe\Model\MetadataTrait;
15
use Shapin\Stripe\Model\MetadataCollection;
16
use Shapin\Stripe\Model\Account\Account;
17
use Shapin\Stripe\Model\Balance\Balance;
18
use Shapin\Stripe\Model\BankAccount\BankAccount;
19
use Shapin\Stripe\Model\Card\Card;
20
use Shapin\Stripe\Model\Charge\Charge;
21
use Shapin\Stripe\Model\Coupon\Coupon;
22
use Shapin\Stripe\Model\Customer\Customer;
23
use Shapin\Stripe\Model\Discount\Discount;
24
use Shapin\Stripe\Model\Invoice\Invoice;
25
use Shapin\Stripe\Model\Invoice\LineItem as InvoiceItem;
26
use Shapin\Stripe\Model\Plan\Plan;
27
use Shapin\Stripe\Model\Product\Product;
28
use Shapin\Stripe\Model\Refund\Refund;
29
use Shapin\Stripe\Model\Source\Source;
30
use Shapin\Stripe\Model\Subscription\Subscription;
31
use Shapin\Stripe\Model\Transfer\Transfer;
32
33
trait EventTrait
34
{
35
    use LivemodeTrait;
36
    use MetadataTrait;
37
38
    /**
39
     * @var string
40
     */
41
    protected $id;
42
43
    /**
44
     * @var string
45
     */
46
    protected $apiVersion;
47
48
    /**
49
     * @var \DateTimeImmutable
50
     */
51
    protected $createdAt;
52
53
    /**
54
     * @var int
55
     */
56
    protected $pendingWebhooks;
57
58
    /**
59
     * @var ?Request
60
     */
61
    protected $request;
62
63
    /**
64
     * @var string
65
     */
66
    protected $type;
67
68
    /**
69
     * @var ?Account
70
     */
71
    private $account;
72
73
    /**
74
     * @var ?Balance
75
     */
76
    private $balance;
77
78
    /**
79
     * @var ?BankAccount
80
     */
81
    private $bankAccount;
82
83
    /**
84
     * @var ?Card
85
     */
86
    private $card;
87
88
    /**
89
     * @var ?Charge
90
     */
91
    private $charge;
92
93
    /**
94
     * @var ?Coupon
95
     */
96
    private $coupon;
97
98
    /**
99
     * @var ?Customer
100
     */
101
    private $customer;
102
103
    /**
104
     * @var ?Discount
105
     */
106
    private $discount;
107
108
    /**
109
     * @var ?Invoice
110
     */
111
    private $invoice;
112
113
    /**
114
     * @var ?InvoiceItem
115
     */
116
    private $invoiceItem;
117
118
    /**
119
     * @var ?Plan
120
     */
121
    protected $plan;
122
123
    /**
124
     * @var ?Product
125
     */
126
    protected $product;
127
128
    /**
129
     * @var ?Refund
130
     */
131
    protected $refund;
132
133
    /**
134
     * @var ?Source
135
     */
136
    protected $source;
137
138
    /**
139
     * @var ?Subscription
140
     */
141
    private $subscription;
142
143
    /**
144
     * @var ?Transfer
145
     */
146
    private $transfer;
147
148
    /**
149
     * @var array
150
     */
151
    private $previousAttributes;
152
153 59
    private function __construct()
154
    {
155 59
    }
156
157 59
    public static function createFromArray(array $data): self
158
    {
159 59
        $model = new self();
160 59
        $model->id = $data['id'];
161 59
        $model->apiVersion = $data['api_version'];
162 59
        $model->createdAt = new \DateTimeImmutable('@'.$data['created']);
163 59
        $model->live = (bool) $data['livemode'];
164 59
        $model->metadata = MetadataCollection::createFromArray($data['metadata'] ?? []);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Shapin\Stripe\Model\Met...'metadata'] ?? array()) of type object<self> is incompatible with the declared type object<Shapin\Stripe\Model\MetadataCollection> of property $metadata.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
165 59
        $model->pendingWebhooks = (int) $data['pending_webhooks'];
166 59
        $model->request = isset($data['request']) ? Request::createFromArray($data['request']) : null;
167 59
        $model->type = $data['type'];
168 59
        $model->previousAttributes = $data['data']['previous_attributes'] ?? [];
169
170 59
        if ('invoice.upcoming' === $data['type']) {
171 1
            return $model;
172
        }
173
174 58
        $object = $data['data']['object']['object'];
175 58
        switch ($object) {
176 58
            case 'account':
177 1
                $model->account = Account::createFromArray($data['data']['object']);
178 1
                break;
179 57
            case 'balance':
180 1
                $model->balance = Balance::createFromArray($data['data']['object']);
181 1
                break;
182 56
            case 'bank_account':
183 4
                $model->bankAccount = BankAccount::createFromArray($data['data']['object']);
184 4
                break;
185 52
            case 'card':
186 1
                $model->card = Card::createFromArray($data['data']['object']);
187 1
                break;
188 51
            case 'charge':
189 7
                $model->charge = Charge::createFromArray($data['data']['object']);
190 7
                break;
191 44
            case 'coupon':
192 3
                $model->coupon = Coupon::createFromArray($data['data']['object']);
193 3
                break;
194 41
            case 'customer':
195 3
                $model->customer = Customer::createFromArray($data['data']['object']);
196 3
                break;
197 38
            case 'discount':
198 3
                $model->discount = Discount::createFromArray($data['data']['object']);
199 3
                break;
200 35
            case 'invoice':
201 9
                $model->invoice = Invoice::createFromArray($data['data']['object']);
202 9
                break;
203 26
            case 'invoiceitem':
204 3
                $model->invoiceItem = InvoiceItem::createFromArray($data['data']['object']);
205 3
                break;
206 23
            case 'plan':
207 3
                $model->plan = Plan::createFromArray($data['data']['object']);
208 3
                break;
209 20
            case 'product':
210 3
                $model->product = Product::createFromArray($data['data']['object']);
211 3
                break;
212 17
            case 'refund':
213 1
                $model->refund = Refund::createFromArray($data['data']['object']);
214 1
                break;
215 16
            case 'source':
216 9
                $model->source = Source::createFromArray($data['data']['object']);
217 9
                break;
218 7
            case 'subscription':
219 4
                $model->subscription = Subscription::createFromArray($data['data']['object']);
220 4
                break;
221 3
            case 'transfer':
222 3
                $model->transfer = Transfer::createFromArray($data['data']['object']);
223 3
                break;
224
            default:
225
                throw new InvalidArgumentException("Unable to process event data: Unknown object $object");
226
        }
227
228 58
        return $model;
229
    }
230
231
    public function getId(): string
232
    {
233
        return $this->id;
234
    }
235
236
    public function getApiVersion(): string
237
    {
238
        return $this->apiVersion;
239
    }
240
241
    public function getCreatedAt(): \DateTimeImmutable
242
    {
243
        return $this->createdAt;
244
    }
245
246
    public function getPendingWebhooks(): int
247
    {
248
        return $this->pendingWebhooks;
249
    }
250
251
    public function getRequest(): ?Request
252
    {
253
        return $this->request;
254
    }
255
256 59
    public function getType(): string
257
    {
258 59
        return $this->type;
259
    }
260
}
261