Completed
Push — master ( 075684...954b15 )
by raphael
06:42
created

StripeConnect::createOrUpdateCustomer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 10
nc 2
nop 3
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
        $s = Stripe::where('user_id', $user->id)->first();
33
        if (!$s) {
34
            $s = new Stripe();
35
            $s->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
            $s->save();
37
        }
38
        return $s;
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
        } else {
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
    /**
95
     * @param $user
96
     * @param $id_key
97
     * @param $callback
98
     * @return Stripe
99
     */
100
    private static function create($user, $id_key, $callback) {
101
        self::prepare();
102
        $user = self::getStripeModel($user);
103
        if (!$user->$id_key) {
104
            $user->$id_key = call_user_func($callback)->id;
105
            $user->save();
106
        }
107
        return $user;
108
    }
109
110
    /**
111
     * @param null $token
112
     * @return Transaction
113
     */
114
    public static function transaction($token = null)
115
    {
116
        return new Transaction($token);
117
    }
118
}