Completed
Push — master ( b9ac23...46f1c6 )
by ARCANEDEV
7s
created

Customer   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 243
wmc 16
lcom 2
cbo 4
ccs 43
cts 43
cp 1
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscriptionUrl() 0 4 1
A retrieve() 0 4 1
A create() 0 4 1
A save() 0 4 1
A delete() 0 4 1
A addInvoiceItem() 0 6 1
A invoices() 0 6 1
A invoiceItems() 0 6 1
A charges() 0 6 1
A updateSubscription() 0 7 1
A cancelSubscription() 0 7 1
A deleteDiscount() 0 9 1
A appCustomerParam() 0 6 2
A all() 0 4 1
A getDiscountUrl() 0 4 1
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/Save Customer.
127
     * @link   https://stripe.com/docs/api/php#create_customer
128
     *
129
     * @param  array|string|null  $options
130
     *
131
     * @return self
132
     */
133 45
    public function save($options = null)
134
    {
135 45
        return self::scopedSave($options);
136
    }
137
138
    /**
139
     * Delete Customer.
140
     * @link   https://stripe.com/docs/api/php#delete_customer
141
     *
142
     * @param  array|null         $params
143
     * @param  array|string|null  $options
144
     *
145
     * @return self
146
     */
147 5
    public function delete($params = [], $options = null)
148
    {
149 5
        return self::scopedDelete($params, $options);
150
    }
151
152
    /* ------------------------------------------------------------------------------------------------
153
     |  Relationships Functions
154
     | ------------------------------------------------------------------------------------------------
155
     */
156
    /**
157
     * Add an invoice item.
158
     *
159
     * @param  array  $params
160
     *
161
     * @return \Arcanedev\Stripe\Resources\InvoiceItem|array
162
     */
163 10
    public function addInvoiceItem($params = [])
164
    {
165 10
        $this->appCustomerParam($params);
166
167 10
        return InvoiceItem::create($params, $this->opts);
168
    }
169
170
    /**
171
     * Get all invoices.
172
     *
173
     * @param  array  $params
174
     *
175
     * @return \Arcanedev\Stripe\Collection|array
176
     */
177 5
    public function invoices($params = [])
178
    {
179 5
        $this->appCustomerParam($params);
180
181 5
        return Invoice::all($params, $this->opts);
182
    }
183
184
    /**
185
     * Get all invoice items.
186
     *
187
     * @param  array  $params
188
     *
189
     * @return \Arcanedev\Stripe\Collection|array
190
     */
191 5
    public function invoiceItems($params = [])
192
    {
193 5
        $this->appCustomerParam($params);
194
195 5
        return InvoiceItem::all($params, $this->opts);
196
    }
197
198
    /**
199
     * Get all charges.
200
     *
201
     * @param  array  $params
202
     *
203
     * @return \Arcanedev\Stripe\Collection|array
204
     */
205 5
    public function charges($params = [])
206
    {
207 5
        $this->appCustomerParam($params);
208
209 5
        return Charge::all($params, $this->opts);
210
    }
211
212
    /**
213
     * Update a Subscription.
214
     *
215
     * @param  array|null  $params
216
     *
217
     * @return \Arcanedev\Stripe\Resources\Subscription
218
     */
219 5
    public function updateSubscription($params = [])
220
    {
221 5
        list($response, $opts) = $this->request('post', $this->getSubscriptionUrl(), $params);
222 5
        $this->refreshFrom(['subscription' => $response], $opts, true);
223
224 5
        return $this->subscription;
225
    }
226
227
    /**
228
     * Cancel Subscription.
229
     *
230
     * @param  array|null  $params
231
     *
232
     * @return \Arcanedev\Stripe\Resources\Subscription
233
     */
234 5
    public function cancelSubscription($params = [])
235
    {
236 5
        list($response, $opts) = $this->request('delete', $this->getSubscriptionUrl(), $params);
237 5
        $this->refreshFrom(['subscription' => $response], $opts, true);
238
239 5
        return $this->subscription;
240
    }
241
242
    /**
243
     * Delete Discount.
244
     *
245
     * @return \Arcanedev\Stripe\StripeObject
246
     */
247 15
    public function deleteDiscount()
248
    {
249 15
        list($response, $opts) = $this->request('delete', $this->getDiscountUrl());
250
251 15
        $this->refreshFrom(['discount' => null], $opts, true);
252 15
        unset($response);
253
254 15
        return $this;
255
    }
256
257
    /* ------------------------------------------------------------------------------------------------
258
     |  Other Functions
259
     | ------------------------------------------------------------------------------------------------
260
     */
261
    /**
262
     * Add Customer ID to parameters.
263
     *
264
     * @param  array  $params
265
     */
266 20
    private function appCustomerParam(&$params)
267
    {
268 20
        if (empty($params)) $params = [];
269
270 20
        $params['customer'] = $this->id;
271 20
    }
272
}
273