Completed
Pull Request — master (#18)
by
unknown
03:34
created

Transaction   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 144
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A from() 0 6 1
A useSavedCustomer() 0 5 1
A to() 0 6 1
A amount() 0 6 1
A fee() 0 5 1
A transfer() 0 11 1
A create() 0 36 3
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
use Stripe\Transfer;
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, $transfers = [];
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
     * Transfer an amount from the charge to someone.
97
     *
98
     * @param $amount
99
     * @param $receiver
100
     * @param $currency
101
     * @param $receiver_params
102
     * @return $this
103
     */
104
    public function transfer($amount, $receiver, $currency = null, $receiver_params = [])
105
    {
106
        array_push($this->transfers, [
107
            'amount' => $amount,
108
            'currency' => $currency ?? $this->currency,
109
            'receiver' => $receiver,
110
            'receiver_params' => $receiver_params
111
        ]);
112
113
        return $this;
114
    }
115
116
    /**
117
     * Create the transaction: charge customer and credit vendor.
118
     * This function saves the two accounts.
119
     *
120
     * @param array $params
121
     * @return Charge
122
     */
123
    public function create($params = [])
124
    {
125
        // Prepare vendor
126
        $vendor = StripeConnect::createAccount($this->to, $this->to_params);
127
128
        // Prepare customer
129
        if ($this->saved_customer) {
130
            $customer = StripeConnect::createCustomer($this->token, $this->from, $this->from_params);
131
            $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...
132
        } else {
133
            $params["source"] = $this->token;
134
        }
135
136
        $charge =  Charge::create(array_merge([
137
            "amount" => $this->value,
138
            "currency" => $this->currency,
139
            "destination" => [
140
                "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...
141
            ],
142
            "application_fee" => $this->fee ?? null,
143
        ], $params));
144
145
        // Send the transfers to the respective receiver
146
        foreach ($this->transfers as $transfer) {
147
            $receiver = StripeConnect::createAccount($transfer['receiver'], $transfer['receiver_params']);
148
149
            Transfer::create([
150
                'amount' => $transfer['amount'],
151
                'currency' => $this->currency,
152
                'destination' => $receiver->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...
153
                'transfer_group' => $charge->transfer_group
154
            ]);
155
        }
156
157
        return $charge;
158
    }
159
}
160