Completed
Pull Request — master (#22)
by ARCANEDEV
07:43
created

Customer::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
cc 1
eloc 2
nc 1
nop 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php namespace Arcanedev\Stripe\Resources;
2
3
use Arcanedev\Stripe\Contracts\Resources\CustomerInterface;
4
use Arcanedev\Stripe\StripeResource;
5
6
/**
7
 * Class     Customer
8
 *
9
 * @package  Arcanedev\Stripe\Resources
10
 * @author   ARCANEDEV <[email protected]>
11
 * @link     https://stripe.com/docs/api/php#customer_object
12
 *
13
 * @property  string                                         id
14
 * @property  string                                         object           // 'customer'
15
 * @property  int                                            account_balance
16
 * @property  int                                            created          // timestamp
17
 * @property  string                                         currency
18
 * @property  string                                         default_source
19
 * @property  bool                                           delinquent
20
 * @property  string                                         description
21
 * @property  \Arcanedev\Stripe\Resources\Discount|null      discount
22
 * @property  string                                         email
23
 * @property  bool                                           livemode
24
 * @property  \Arcanedev\Stripe\AttachedObject               metadata
25
 * @property  string                                         shipping
26
 * @property  \Arcanedev\Stripe\Collection                   sources
27
 * @property  \Arcanedev\Stripe\Collection                   subscriptions
28
 * @property  \Arcanedev\Stripe\Resources\Subscription|null  subscription    // It's for updateSubscription and cancelSubscription
29
 */
30
class Customer extends StripeResource implements CustomerInterface
31
{
32
    /* ------------------------------------------------------------------------------------------------
33
     |  Constants
34
     | ------------------------------------------------------------------------------------------------
35
     */
36
    const END_POINT_SUBSCRIPTION = 'subscription';
37
38
    const END_POINT_DISCOUNT     = 'discount';
39
40
    /* ------------------------------------------------------------------------------------------------
41
     |  Properties
42
     | ------------------------------------------------------------------------------------------------
43
     */
44
    /**
45
     * Allow to check attributes while setting.
46
     *
47
     * @var bool
48
     */
49
    protected $checkUnsavedAttributes = true;
50
51
    /* ------------------------------------------------------------------------------------------------
52
     |  Getters & Setters
53
     | ------------------------------------------------------------------------------------------------
54
     */
55
    /**
56
     * Get Subscription URL.
57
     *
58
     * @throws \Arcanedev\Stripe\Exceptions\InvalidRequestException
59
     *
60
     * @return string
61
     */
62 10
    private function getSubscriptionUrl()
63
    {
64 10
        return $this->instanceUrl() . '/' . self::END_POINT_SUBSCRIPTION;
65
    }
66
67
    /**
68
     * Get Discount URL.
69
     *
70
     * @throws \Arcanedev\Stripe\Exceptions\InvalidRequestException
71
     *
72
     * @return string
73
     */
74 15
    private function getDiscountUrl()
75
    {
76 15
        return $this->instanceUrl() . '/' . self::END_POINT_DISCOUNT;
77
    }
78
79
    /* ------------------------------------------------------------------------------------------------
80
     |  CRUD Functions
81
     | ------------------------------------------------------------------------------------------------
82
     */
83
    /**
84
     * Retrieve a Customer.
85
     * @link   https://stripe.com/docs/api/php#retrieve_customer
86
     *
87
     * @param  string             $id
88
     * @param  array|string|null  $options
89
     *
90
     * @return self
91
     */
92 60
    public static function retrieve($id, $options = null)
93
    {
94 60
        return self::scopedRetrieve($id, $options);
95
    }
96
97
    /**
98
     * List all Customers.
99
     * @link   https://stripe.com/docs/api/php#list_customers
100
     *
101
     * @param  array|null         $params
102
     * @param  array|string|null  $options
103
     *
104
     * @return \Arcanedev\Stripe\Collection|array
105
     */
106 5
    public static function all($params = [], $options = null)
107
    {
108 5
        return self::scopedAll($params, $options);
109
    }
110
111
    /**
112
     * Create Customer.
113
     * @link   https://stripe.com/docs/api/php#create_customer
114
     *
115
     * @param  array|null         $params
116
     * @param  array|string|null  $options
117
     *
118
     * @return self|array
119
     */
120 205
    public static function create($params = [], $options = null)
121
    {
122 205
        return self::scopedCreate($params, $options);
123
    }
124
125
    /**
126
     * Update a Customer.
127
     * @link   https://stripe.com/docs/api/php#create_customer
128
     *         
129
     * @param  string             $id
130
     * @param  array|null         $params
131
     * @param  array|string|null  $options
132
     *
133
     * @return self
134
     */
135
    public static function update($id, $params = [], $options = null)
136
    {
137
        return self::scopedUpdate($id, $params, $options);
138
    }
139
140
    /**
141
     * Update/Save a Customer.
142
     * @link   https://stripe.com/docs/api/php#create_customer
143
     *
144
     * @param  array|string|null  $options
145
     *
146
     * @return self
147
     */
148 45
    public function save($options = null)
149
    {
150 45
        return self::scopedSave($options);
151
    }
152
153
    /**
154
     * Delete Customer.
155
     * @link   https://stripe.com/docs/api/php#delete_customer
156
     *
157
     * @param  array|null         $params
158
     * @param  array|string|null  $options
159
     *
160
     * @return self
161
     */
162 5
    public function delete($params = [], $options = null)
163
    {
164 5
        return self::scopedDelete($params, $options);
165
    }
166
167
    /* ------------------------------------------------------------------------------------------------
168
     |  Relationships Functions
169
     | ------------------------------------------------------------------------------------------------
170
     */
171
    /**
172
     * Add an invoice item.
173
     *
174
     * @param  array  $params
175
     *
176
     * @return \Arcanedev\Stripe\Resources\InvoiceItem|array
177
     */
178 10
    public function addInvoiceItem($params = [])
179
    {
180 10
        $this->appCustomerParam($params);
181
182 10
        return InvoiceItem::create($params, $this->opts);
183
    }
184
185
    /**
186
     * Get all invoices.
187
     *
188
     * @param  array  $params
189
     *
190
     * @return \Arcanedev\Stripe\Collection|array
191
     */
192 5
    public function invoices($params = [])
193
    {
194 5
        $this->appCustomerParam($params);
195
196 5
        return Invoice::all($params, $this->opts);
197
    }
198
199
    /**
200
     * Get all invoice items.
201
     *
202
     * @param  array  $params
203
     *
204
     * @return \Arcanedev\Stripe\Collection|array
205
     */
206 5
    public function invoiceItems($params = [])
207
    {
208 5
        $this->appCustomerParam($params);
209
210 5
        return InvoiceItem::all($params, $this->opts);
211
    }
212
213
    /**
214
     * Get all charges.
215
     *
216
     * @param  array  $params
217
     *
218
     * @return \Arcanedev\Stripe\Collection|array
219
     */
220 5
    public function charges($params = [])
221
    {
222 5
        $this->appCustomerParam($params);
223
224 5
        return Charge::all($params, $this->opts);
225
    }
226
227
    /**
228
     * Update a Subscription.
229
     *
230
     * @param  array|null  $params
231
     *
232
     * @return \Arcanedev\Stripe\Resources\Subscription
233
     */
234 5
    public function updateSubscription($params = [])
235
    {
236 5
        list($response, $opts) = $this->request('post', $this->getSubscriptionUrl(), $params);
237 5
        $this->refreshFrom(['subscription' => $response], $opts, true);
238
239 5
        return $this->subscription;
240
    }
241
242
    /**
243
     * Cancel Subscription.
244
     *
245
     * @param  array|null  $params
246
     *
247
     * @return \Arcanedev\Stripe\Resources\Subscription
248
     */
249 5
    public function cancelSubscription($params = [])
250
    {
251 5
        list($response, $opts) = $this->request('delete', $this->getSubscriptionUrl(), $params);
252 5
        $this->refreshFrom(['subscription' => $response], $opts, true);
253
254 5
        return $this->subscription;
255
    }
256
257
    /**
258
     * Delete Discount.
259
     *
260
     * @return \Arcanedev\Stripe\StripeObject
261
     */
262 15
    public function deleteDiscount()
263
    {
264 15
        list($response, $opts) = $this->request('delete', $this->getDiscountUrl());
265
266 15
        $this->refreshFrom(['discount' => null], $opts, true);
267 15
        unset($response);
268
269 15
        return $this;
270
    }
271
272
    /* ------------------------------------------------------------------------------------------------
273
     |  Other Functions
274
     | ------------------------------------------------------------------------------------------------
275
     */
276
    /**
277
     * Add Customer ID to parameters.
278
     *
279
     * @param  array  $params
280
     */
281 20
    private function appCustomerParam(&$params)
282
    {
283 20
        if (empty($params)) $params = [];
284
285 20
        $params['customer'] = $this->id;
286 20
    }
287
}
288