Util::isClassTypeObjectExist()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
c 1
b 0
f 0
cc 3
eloc 4
nc 2
nop 1
ccs 4
cts 4
cp 1
crap 3
rs 9.4285
1
<?php namespace Arcanedev\Stripe\Utilities;
2
3
use Arcanedev\Stripe\Collection;
4
use Arcanedev\Stripe\Contracts\Utilities\Util as UtilContract;
5
use Arcanedev\Stripe\Resources;
6
use Arcanedev\Stripe\StripeObject;
7
8
/**
9
 * Class     Util
10
 *
11
 * @package  Arcanedev\Stripe\Utilities
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
abstract class Util implements UtilContract
15
{
16
    /* -----------------------------------------------------------------
17
     |  Properties
18
     | -----------------------------------------------------------------
19
     */
20
21
    /**
22
     * Available Resources.
23
     *
24
     * @var array
25
     */
26
    private static $resources = [
27
        // Data structures
28
        'list'                => Collection::class,
29
30
        // Resources
31
        'account'             => Resources\Account::class,
32
        'alipay_account'      => Resources\AlipayAccount::class,
33
        'apple_pay_domain'    => Resources\ApplePayDomain::class,
34
        'balance_transaction' => Resources\BalanceTransaction::class,
35
        'bank_account'        => Resources\BankAccount::class,
36
        'bitcoin_transaction' => Resources\BitcoinTransaction::class,
37
        'card'                => Resources\Card::class,
38
        'charge'              => Resources\Charge::class,
39
        'country_spec'        => Resources\CountrySpec::class,
40
        'coupon'              => Resources\Coupon::class,
41
        'customer'            => Resources\Customer::class,
42
        'discount'            => Resources\Discount::class,
43
        'dispute'             => Resources\Dispute::class,
44
        'event'               => Resources\Event::class,
45
        'ephemeral_key'       => Resources\EphemeralKey::class,
46
        'exchange_rate'       => Resources\ExchangeRate::class,
47
        'fee_refund'          => Resources\ApplicationFeeRefund::class,
48
        'file_upload'         => Resources\FileUpload::class,
49
        'invoice'             => Resources\Invoice::class,
50
        'invoiceitem'         => Resources\InvoiceItem::class,
51
        'login_link'          => Resources\LoginLink::class,
52
        'order'               => Resources\Order::class,
53
        'order_item'          => Resources\OrderItem::class,
54
        'order_return'        => Resources\OrderReturn::class,
55
        'payout'              => Resources\Payout::class,
56
        'plan'                => Resources\Plan::class,
57
        'product'             => Resources\Product::class,
58
        'recipient'           => Resources\Recipient::class,
59
        'recipient_transfer'  => Resources\RecipientTransfer::class,
60
        'refund'              => Resources\Refund::class,
61
        'sku'                 => Resources\Sku::class,
62
        'source'              => Resources\Source::class,
63
        'source_transaction'  => Resources\SourceTransaction::class,
64
        'subscription'        => Resources\Subscription::class,
65
        'subscription_item'   => Resources\SubscriptionItem::class,
66
        'three_d_secure'      => Resources\ThreeDSecure::class,
67
        'token'               => Resources\Token::class,
68
        'transfer'            => Resources\Transfer::class,
69
        'transfer_reversal'   => Resources\TransferReversal::class,
70
    ];
71
72
    private static $isHashEqualsAvailable = null;
73
74
    /* -----------------------------------------------------------------
75
     |  Main Methods
76
     | -----------------------------------------------------------------
77
     */
78
79
    /**
80
     * Recursively converts the PHP Stripe object to an array.
81
     *
82
     * @param  array  $values
83
     *
84
     * @return array
85
     */
86 8
    public static function convertStripeObjectToArray($values)
87
    {
88 8
        $results = [];
89
90 8
        foreach ($values as $k => $v) {
91
            // TODO: Fix the encapsulation violation
92 8
            if ($k[0] == '_') continue;
93
94 8
            if ($v instanceof StripeObject) {
95 4
                $results[$k] = $v->toArray(true);
96
            }
97 8
            elseif (is_array($v)) {
98 2
                $results[$k] = self::convertStripeObjectToArray($v);
99
            }
100
            else {
101 8
                $results[$k] = $v;
102
            }
103
        }
104
105 8
        return $results;
106
    }
107
108
    /**
109
     * Converts a response from the Stripe API to the corresponding PHP object.
110
     *
111
     * @param  array  $response
112
     * @param  array  $options
113
     *
114
     * @return \Arcanedev\Stripe\StripeObject|\Arcanedev\Stripe\StripeResource|\Arcanedev\Stripe\Collection|array
115
     */
116 396
    public static function convertToStripeObject($response, $options)
117
    {
118 396
        if (self::isList($response)) {
119 296
            return array_map(function($i) use ($options) {
120 212
                return self::convertToStripeObject($i, $options);
121 296
            }, $response);
122
        }
123 396
        elseif (is_array($response)) {
124 374
            $class = self::getClassTypeObject($response);
125
126 374
            return $class::scopedConstructFrom(
127 374
                $response,
128 374
                $options
129
            );
130
        }
131
132 396
        return $response;
133
    }
134
135
    /**
136
     * Get Class Type.
137
     *
138
     * @param  array  $response
139
     *
140
     * @return string
141
     */
142 374
    private static function getClassTypeObject($response)
143
    {
144 374
        return self::isClassTypeObjectExist($response)
145 352
            ? self::$resources[ $response['object'] ]
146 374
            : StripeObject::class;
147
    }
148
149
    /**
150
     * Compares two strings for equality. The time taken is independent of the
151
     * number of characters that match.
152
     *
153
     * @param  string  $one
154
     * @param  string  $two
155
     *
156
     * @return bool
157
     */
158 12
    public static function secureCompare($one, $two)
159
    {
160 12
        if (self::$isHashEqualsAvailable === null) {
161 2
            self::$isHashEqualsAvailable = function_exists('hash_equals');
162
        }
163
164 12
        if (self::$isHashEqualsAvailable) {
165 12
            return hash_equals($one, $two);
166
        }
167
168
        if (strlen($one) != strlen($two)) {
169
            return false;
170
        }
171
172
        $result = 0;
173
174
        for ($i = 0; $i < strlen($one); $i++) {
175
            $result |= ord($one[$i]) ^ ord($two[$i]);
176
        }
177
178
        return ($result == 0);
179
    }
180
181
    /* -----------------------------------------------------------------
182
     |  Check Methods
183
     | -----------------------------------------------------------------
184
     */
185
186
    /**
187
     * Whether the provided array (or other) is a list rather than a dictionary.
188
     *
189
     * @param  mixed  $array
190
     *
191
     * @return bool
192
     */
193 398
    public static function isList($array)
194
    {
195 398
        if ( ! is_array($array)) return false;
196
197
        // TODO: generally incorrect, but it's correct given Stripe's response
198 376
        foreach (array_keys($array) as $k) {
199 376
            if ( ! is_numeric($k)) return false;
200
        }
201
202 298
        return true;
203
    }
204
205
    /**
206
     * Check if the object is a resource.
207
     *
208
     * @param  array  $response
209
     *
210
     * @return bool
211
     */
212 374
    private static function isClassTypeObjectExist($response)
213
    {
214 374
        if (isset($response['object']) && is_string($response['object'])) {
215 354
            return array_key_exists($response['object'], self::$resources);
216
        }
217
218 146
        return false;
219
    }
220
}
221