StripeConnect   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 103
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A prepare() 0 4 1
A getStripeModel() 0 10 2
A createAccount() 0 10 1
A createCustomer() 0 10 1
A createOrUpdateCustomer() 0 12 2
A create() 0 9 2
A transaction() 0 4 1
1
<?php
2
3
4
namespace Rap2hpoutre\LaravelStripeConnect;
5
6
use Stripe\Account as StripeAccount;
7
use Stripe\Customer;
8
use Stripe\Stripe as StripeBase;
9
10
11
/**
12
 * Class StripeConnect
13
 * @package Rap2hpoutre\LaravelStripeConnect
14
 */
15
class StripeConnect
16
{
17
18
    /**
19
     *
20
     */
21
    private static function prepare()
22
    {
23
        StripeBase::setApiKey(config('services.stripe.secret'));
24
    }
25
26
    /**
27
     * @param $user
28
     * @return Stripe
29
     */
30
    private static function getStripeModel($user)
31
    {
32
        $stripe = Stripe::where('user_id', $user->id)->first();
33
        if (!$stripe) {
34
            $stripe = new Stripe();
35
            $stripe->user_id = $user->id;
0 ignored issues
show
Documentation introduced by
The property user_id does not exist on object<Rap2hpoutre\LaravelStripeConnect\Stripe>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
36
            $stripe->save();
37
        }
38
        return $stripe;
39
    }
40
41
    /**
42
     * @param $to
43
     * @param array $params
44
     * @return Stripe
45
     */
46
    public static function createAccount($to, $params = [])
47
    {
48
        $params = array_merge([
49
            "type" => "custom",
50
            "email" => $to->email,
51
        ], $params);
52
        return self::create($to, 'account_id', function () use ($params) {
53
            return StripeAccount::create($params);
54
        });
55
    }
56
57
    /**
58
     * @param $token
59
     * @param $from
60
     * @param array $params
61
     * @return Stripe
62
     */
63
    public static function createCustomer($token, $from, $params = [])
64
    {
65
        $params = array_merge([
66
            "email" => $from->email,
67
            'source' => $token,
68
        ], $params);
69
        return self::create($from, 'customer_id', function () use ($params) {
70
            return Customer::create($params);
71
        });
72
    }
73
74
    /**
75
     * @param $token
76
     * @param $from
77
     * @param array $params
78
     * @return Stripe
79
     */
80
    public function createOrUpdateCustomer($token, $from, $params = [])
81
    {
82
        self::prepare();
83
        $user = self::getStripeModel($from);
84
        if (!$user) {
85
            return self::createCustomer($token, $from, $params);
86
        }
87
        $customer = \Stripe\Customer::retrieve($token->customer_id);
88
        $customer->source = $token;
0 ignored issues
show
Bug introduced by
The property source does not seem to exist. Did you mean default_source?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
89
        $customer->save();
90
        return $user;
91
    }
92
93
    /**
94
     * @param $user
95
     * @param $id_key
96
     * @param $callback
97
     * @return Stripe
98
     */
99
    private static function create($user, $id_key, $callback) {
100
        self::prepare();
101
        $user = self::getStripeModel($user);
102
        if (!$user->$id_key) {
103
            $user->$id_key = call_user_func($callback)->id;
104
            $user->save();
105
        }
106
        return $user;
107
    }
108
109
    /**
110
     * @param null $token
111
     * @return Transaction
112
     */
113
    public static function transaction($token = null)
114
    {
115
        return new Transaction($token);
116
    }
117
}