Completed
Push — master ( 77c628...ce2be0 )
by Olivier
03:34
created

EventTrait::getCreatedAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\Customer\Customer;
22
use Shapin\Stripe\Model\Plan\Plan;
23
use Shapin\Stripe\Model\Product\Product;
24
use Shapin\Stripe\Model\Refund\Refund;
25
use Shapin\Stripe\Model\Source\Source;
26
use Shapin\Stripe\Model\Subscription\Subscription;
27
use Shapin\Stripe\Model\Transfer\Transfer;
28
29
trait EventTrait
30
{
31
    use LivemodeTrait, MetadataTrait;
32
33
    /**
34
     * @var string
35
     */
36
    protected $id;
37
38
    /**
39
     * @var string
40
     */
41
    protected $apiVersion;
42
43
    /**
44
     * @var \DateTimeImmutable
45
     */
46
    protected $createdAt;
47
48
    /**
49
     * @var integer
50
     */
51
    protected $pendingWebhooks;
52
53
    /**
54
     * @var Request
55
     */
56
    protected $request;
57
58
    /**
59
     * @var string
60
     */
61
    protected $type;
62
63
    /**
64
     * @var ?Account
65
     */
66
    private $account;
67
68
    /**
69
     * @var ?Balance
70
     */
71
    private $balance;
72
73
    /**
74
     * @var ?BankAccount
75
     */
76
    private $bankAccount;
77
78
    /**
79
     * @var ?Card
80
     */
81
    private $card;
82
83
    /**
84
     * @var ?Charge
85
     */
86
    private $charge;
87
88
    /**
89
     * @var ?Customer
90
     */
91
    private $customer;
92
93
    /**
94
     * @var ?Plan
95
     */
96
    protected $plan;
97
98
    /**
99
     * @var ?Product
100
     */
101
    protected $product;
102
103
    /**
104
     * @var ?Refund
105
     */
106
    protected $refund;
107
108
    /**
109
     * @var ?Source
110
     */
111
    protected $source;
112
113
    /**
114
     * @var ?Subscription
115
     */
116
    private $subscription;
117
118
    /**
119
     * @var ?Transfer
120
     */
121
    private $transfer;
122
123
    /**
124
     * @var array
125
     */
126
    private $previousAttributes;
127
128 39
    private function __construct()
129
    {
130 39
    }
131
132 39
    public static function createFromArray(array $data): self
133
    {
134 39
        $model = new self();
135 39
        $model->id = $data['id'];
136 39
        $model->apiVersion = $data['api_version'];
137 39
        $model->createdAt = new \DateTimeImmutable('@'.$data['created']);
138 39
        $model->live = (bool) $data['livemode'];
139 39
        $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...
140 39
        $model->pendingWebhooks = (int) $data['pending_webhooks'];
141 39
        $model->request = isset($data['request']) ? Request::createFromArray($data['request']) : null;
0 ignored issues
show
Documentation Bug introduced by
It seems like isset($data['request']) ...data['request']) : null can also be of type object<self>. However, the property $request is declared as type object<Shapin\Stripe\Model\Event\Request>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
142 39
        $model->type = $data['type'];
143
144 39
        $object = $data['data']['object']['object'];
145
        switch ($object) {
146 39
            case 'account':
147 1
                $model->account = Account::createFromArray($data['data']['object']);
148 1
                break;
149 38
            case 'balance':
150 1
                $model->balance = Balance::createFromArray($data['data']['object']);
151 1
                break;
152 37
            case 'bank_account':
153 4
                $model->bankAccount = BankAccount::createFromArray($data['data']['object']);
154 4
                break;
155 33
            case 'card':
156
                $model->card = Card::createFromArray($data['data']['object']);
157
                break;
158 33
            case 'charge':
159 7
                $model->charge = Charge::createFromArray($data['data']['object']);
160 7
                break;
161 26
            case 'customer':
162 3
                $model->customer = Customer::createFromArray($data['data']['object']);
163 3
                break;
164 23
            case 'plan':
165 3
                $model->plan = Plan::createFromArray($data['data']['object']);
166 3
                break;
167 20
            case 'product':
168 3
                $model->product = Product::createFromArray($data['data']['object']);
169 3
                break;
170 17
            case 'refund':
171 1
                $model->refund = Refund::createFromArray($data['data']['object']);
172 1
                break;
173 16
            case 'source':
174 9
                $model->source = Source::createFromArray($data['data']['object']);
175 9
                break;
176 7
            case 'subscription':
177 4
                $model->subscription = Subscription::createFromArray($data['data']['object']);
178 4
                break;
179 3
            case 'transfer':
180 3
                $model->transfer = Transfer::createFromArray($data['data']['object']);
181 3
                break;
182
            default:
183
                throw new InvalidArgumentException("Unable to process event data: Unknown object $object");
184
        }
185
186
187 39
        $model->previousAttributes = $data['data']['previous_attributes'] ?? [];
188
189 39
        return $model;
190
    }
191
192
    public function getId(): string
193
    {
194
        return $this->id;
195
    }
196
197
    public function getApiVersion(): string
198
    {
199
        return $this->apiVersion;
200
    }
201
202
    public function getCreatedAt(): \DateTimeImmutable
203
    {
204
        return $this->createdAt;
205
    }
206
207
    public function getPendingWebhooks(): int
208
    {
209
        return $this->pendingWebhooks;
210
    }
211
212
    public function getRequest(): Request
213
    {
214
        return $this->request;
215
    }
216
217 39
    public function getType(): string
218
    {
219 39
        return $this->type;
220
    }
221
}
222