Completed
Push — master ( ce2be0...7bcb9a )
by Olivier
02:08
created

EventTrait   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 21

Test Coverage

Coverage 82.89%

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 21
dl 0
loc 224
ccs 63
cts 76
cp 0.8289
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
D createFromArray() 0 70 18
A getId() 0 4 1
A getApiVersion() 0 4 1
A getCreatedAt() 0 4 1
A getPendingWebhooks() 0 4 1
A getRequest() 0 4 1
A getType() 0 4 1
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, MetadataTrait;
36
37
    /**
38
     * @var string
39
     */
40
    protected $id;
41
42
    /**
43
     * @var string
44
     */
45
    protected $apiVersion;
46
47
    /**
48
     * @var \DateTimeImmutable
49
     */
50
    protected $createdAt;
51
52
    /**
53
     * @var int
54
     */
55
    protected $pendingWebhooks;
56
57
    /**
58
     * @var ?Request
59
     */
60
    protected $request;
61
62
    /**
63
     * @var string
64
     */
65
    protected $type;
66
67
    /**
68
     * @var ?Account
69
     */
70
    private $account;
71
72
    /**
73
     * @var ?Balance
74
     */
75
    private $balance;
76
77
    /**
78
     * @var ?BankAccount
79
     */
80
    private $bankAccount;
81
82
    /**
83
     * @var ?Card
84
     */
85
    private $card;
86
87
    /**
88
     * @var ?Charge
89
     */
90
    private $charge;
91
92
    /**
93
     * @var ?Coupon
94
     */
95
    private $coupon;
96
97
    /**
98
     * @var ?Customer
99
     */
100
    private $customer;
101
102
    /**
103
     * @var ?Discount
104
     */
105
    private $discount;
106
107
    /**
108
     * @var ?Invoice
109
     */
110
    private $invoice;
111
112
    /**
113
     * @var ?InvoiceItem
114
     */
115
    private $invoiceItem;
116
117
    /**
118
     * @var ?Plan
119
     */
120
    protected $plan;
121
122
    /**
123
     * @var ?Product
124
     */
125
    protected $product;
126
127
    /**
128
     * @var ?Refund
129
     */
130
    protected $refund;
131
132
    /**
133
     * @var ?Source
134
     */
135
    protected $source;
136
137
    /**
138
     * @var ?Subscription
139
     */
140
    private $subscription;
141
142
    /**
143
     * @var ?Transfer
144
     */
145
    private $transfer;
146
147
    /**
148
     * @var array
149
     */
150
    private $previousAttributes;
151
152 58
    private function __construct()
153
    {
154 58
    }
155
156 58
    public static function createFromArray(array $data): self
157
    {
158 58
        $model = new self();
159 58
        $model->id = $data['id'];
160 58
        $model->apiVersion = $data['api_version'];
161 58
        $model->createdAt = new \DateTimeImmutable('@'.$data['created']);
162 58
        $model->live = (bool) $data['livemode'];
163 58
        $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...
164 58
        $model->pendingWebhooks = (int) $data['pending_webhooks'];
165 58
        $model->request = isset($data['request']) ? Request::createFromArray($data['request']) : null;
166 58
        $model->type = $data['type'];
167
168 58
        $object = $data['data']['object']['object'];
169
        switch ($object) {
170 58
            case 'account':
171 1
                $model->account = Account::createFromArray($data['data']['object']);
172 1
                break;
173 57
            case 'balance':
174 1
                $model->balance = Balance::createFromArray($data['data']['object']);
175 1
                break;
176 56
            case 'bank_account':
177 4
                $model->bankAccount = BankAccount::createFromArray($data['data']['object']);
178 4
                break;
179 52
            case 'card':
180
                $model->card = Card::createFromArray($data['data']['object']);
181
                break;
182 52
            case 'charge':
183 7
                $model->charge = Charge::createFromArray($data['data']['object']);
184 7
                break;
185 45
            case 'coupon':
186 3
                $model->coupon = Coupon::createFromArray($data['data']['object']);
187 3
                break;
188 42
            case 'customer':
189 3
                $model->customer = Customer::createFromArray($data['data']['object']);
190 3
                break;
191 39
            case 'discount':
192 3
                $model->discount = Discount::createFromArray($data['data']['object']);
193 3
                break;
194 36
            case 'invoice':
195 10
                $model->invoice = Invoice::createFromArray($data['data']['object']);
196 10
                break;
197 26
            case 'invoiceitem':
198 3
                $model->invoiceItem = InvoiceItem::createFromArray($data['data']['object']);
199 3
                break;
200 23
            case 'plan':
201 3
                $model->plan = Plan::createFromArray($data['data']['object']);
202 3
                break;
203 20
            case 'product':
204 3
                $model->product = Product::createFromArray($data['data']['object']);
205 3
                break;
206 17
            case 'refund':
207 1
                $model->refund = Refund::createFromArray($data['data']['object']);
208 1
                break;
209 16
            case 'source':
210 9
                $model->source = Source::createFromArray($data['data']['object']);
211 9
                break;
212 7
            case 'subscription':
213 4
                $model->subscription = Subscription::createFromArray($data['data']['object']);
214 4
                break;
215 3
            case 'transfer':
216 3
                $model->transfer = Transfer::createFromArray($data['data']['object']);
217 3
                break;
218
            default:
219
                throw new InvalidArgumentException("Unable to process event data: Unknown object $object");
220
        }
221
222 58
        $model->previousAttributes = $data['data']['previous_attributes'] ?? [];
223
224 58
        return $model;
225
    }
226
227
    public function getId(): string
228
    {
229
        return $this->id;
230
    }
231
232
    public function getApiVersion(): string
233
    {
234
        return $this->apiVersion;
235
    }
236
237
    public function getCreatedAt(): \DateTimeImmutable
238
    {
239
        return $this->createdAt;
240
    }
241
242
    public function getPendingWebhooks(): int
243
    {
244
        return $this->pendingWebhooks;
245
    }
246
247
    public function getRequest(): ?Request
248
    {
249
        return $this->request;
250
    }
251
252 58
    public function getType(): string
253
    {
254 58
        return $this->type;
255
    }
256
}
257