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

Transaction::useSavedCustomer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
4
namespace Rap2hpoutre\LaravelStripeConnect;
5
6
use Stripe\Account as StripeAccount;
7
use Stripe\Charge;
8
use Stripe\Customer;
9
use Stripe\Stripe as StripeBase;
10
11
12
/**
13
 * Class Transaction
14
 * @package Rap2hpoutre\LaravelStripeConnect
15
 */
16
class Transaction
17
{
18
    /**
19
     * @var
20
     */
21
    private $from, $to, $value, $currency, $to_params, $token, $fee, $from_params, $saved_customer;
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
22
23
    /**
24
     * Transaction constructor.
25
     * @param null $token
26
     */
27
    public function __construct($token = null)
28
    {
29
        $this->token = $token;
30
    }
31
32
    /**
33
     * Set the Customer.
34
     *
35
     * @param $user
36
     * @param array $params
37
     * @return $this
38
     */
39
    public function from($user, $params = [])
40
    {
41
        $this->from = $user;
42
        $this->from_params = $params;
43
        return $this;
44
    }
45
46
    /**
47
     * @return $this
48
     */
49
    public function useSavedCustomer()
50
    {
51
        $this->saved_customer = true;
52
        return $this;
53
    }
54
55
    /**
56
     * Set the Vendor.
57
     *
58
     * @param $user
59
     * @param array $params
60
     * @return $this
61
     */
62
    public function to($user, $params = [])
63
    {
64
        $this->to = $user;
65
        $this->to_params = $params;
66
        return $this;
67
    }
68
69
    /**
70
     * The amount of the transaction.
71
     *
72
     * @param $value
73
     * @param $currency
74
     * @return $this
75
     */
76
    public function amount($value, $currency)
77
    {
78
        $this->value = $value;
79
        $this->currency = $currency;
80
        return $this;
81
    }
82
83
    /**
84
     * Take your fees here.
85
     *
86
     * @param $amount
87
     * @return $this
88
     */
89
    public function fee($amount)
90
    {
91
        $this->fee = $amount;
92
        return $this;
93
    }
94
95
    /**
96
     * Create the transaction: charge customer and credit vendor.
97
     * This function saves the two accounts.
98
     *
99
     * @param array $params
100
     * @return Charge
101
     */
102
    public function create($params = [])
103
    {
104
        // Prepare vendor
105
        $vendor = StripeConnect::createAccount($this->to, $this->to_params);
106
        // Prepare customer
107
        if ($this->saved_customer) {
108
            $customer = StripeConnect::createCustomer($this->token, $this->from, $this->from_params);
109
            $params["customer"] = $customer->customer_id;
0 ignored issues
show
Documentation introduced by
The property customer_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...
110
        } else {
111
            $params["source"] = $this->token;
112
        }
113
114
        return Charge::create(array_merge([
115
            "amount" => $this->value,
116
            "currency" => $this->currency,
117
            "destination" => [
118
                "account" => $vendor->account_id,
0 ignored issues
show
Documentation introduced by
The property account_id does not exist on object<Rap2hpoutre\LaravelStripeConnect\Stripe>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
119
            ],
120
            "application_fee" => $this->fee ?? null,
121
        ]), $params);
122
    }
123
}