Completed
Push — master ( 4f7cc2...686a0b )
by raphael
01:39
created

StripeConnect::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 7
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 View Code Duplication
    public static function createAccount($to, $params = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 Customer::create($params);
54
        });
55
    }
56
57
    /**
58
     * @param $token
59
     * @param $from
60
     * @param array $params
61
     * @return Stripe
62
     */
63 View Code Duplication
    public static function createCustomer($token, $from, $params = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 $user
76
     * @param $id_key
77
     * @param $callback
78
     * @return Stripe
79
     */
80
    private static function create($user, $id_key, $callback) {
81
        self::prepare();
82
        $customer = self::getStripeModel($user);
83
        if (!$user->$id_key) {
84
            $user->$id_key = call_user_func($callback)->id;
85
            $user->save();
86
        }
87
        return $customer;
88
    }
89
90
    /**
91
     * @param null $token
92
     * @return Transaction
93
     */
94
    public static function transaction($token = null)
95
    {
96
        return new Transaction($token);
97
    }
98
}