Failed Conditions
Push — master ( dc6c53...ed3b11 )
by Maximo
01:10 queued 11s
created

getCurrentPaymentMethodCreds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Canvas\Models;
5
6
use Phalcon\Di;
7
use Stripe\Token as StripeToken;
8
use Canvas\Models\PaymentMethods;
9
10
class PaymentMethodsCreds extends AbstractModel
11
{
12
    /**
13
     *
14
     * @var integer
15
     */
16
    public $id;
17
18
    /**
19
     *
20
     * @var integer
21
     */
22
    public $users_id;
0 ignored issues
show
Coding Style introduced by
$users_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
23
24
    /**
25
     *
26
     * @var integer
27
     */
28
    public $companies_id;
0 ignored issues
show
Coding Style introduced by
$companies_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
29
30
    /**
31
     *
32
     * @var integer
33
     */
34
    public $apps_id;
0 ignored issues
show
Coding Style introduced by
$apps_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
35
36
    /**
37
     *
38
     * @var integer
39
     */
40
    public $payment_methods_id;
0 ignored issues
show
Coding Style introduced by
$payment_methods_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
41
42
    /**
43
     *
44
     * @var string
45
     */
46
    public $payment_ending_numbers;
0 ignored issues
show
Coding Style introduced by
$payment_ending_numbers does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
47
48
    /**
49
     *
50
     * @var string
51
     */
52
    public $expiration_date;
0 ignored issues
show
Coding Style introduced by
$expiration_date does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
53
54
    /**
55
     *
56
     * @var string
57
     */
58
    public $zip_code;
0 ignored issues
show
Coding Style introduced by
$zip_code does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
59
60
    /**
61
     *
62
     * @var string
63
     */
64
    public $created_at;
0 ignored issues
show
Coding Style introduced by
$created_at does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
65
66
    /**
67
     *
68
     * @var string
69
     */
70
    public $updated_at;
0 ignored issues
show
Coding Style introduced by
$updated_at does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
71
72
    /**
73
     *
74
     * @var integer
75
     */
76
    public $is_deleted;
0 ignored issues
show
Coding Style introduced by
$is_deleted does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
77
78
    /**
79
     * Initialize method for model.
80
     */
81
    public function initialize()
82
    {
83
        $this->setSource('payment_methods_creds');
84
    }
85
86
    /**
87
     * Returns table name mapped in the model.
88
     *
89
     * @return string
90
     */
91
    public function getSource(): string
92
    {
93
        return 'payment_methods_creds';
94
    }
95
96
    /**
97
     * Returns the current payment method credentials
98
     *
99
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be PaymentMethodsCreds?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
100
     */
101
    public function getCurrentPaymentMethodCreds(): self
102
    {
103
        return self::findFirstOrFail([
104
            "conditions"=> "users_id = ?0 and companies_id = ?1 and apps_id = ?2 and is_deleted = 0",
105
            "bind" => 
106
            [
107
                Di::getDefault()->getUserData()->getId(),
108
                Di::getDefault()->getUserData()->getDefaultCompany()->getId(),
109
                Di::getDefault()->getApp()->getId()
110
            ],
111
            "order"=> "id DESC"
112
        ]);
113
    }
114
115
    /**
116
     * Create a new record from Stripe Token
117
     *
118
     * @param string $token
119
     * @return return self
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
120
     */
121
    public static function createByStripeToken(string $token): self
122
    {
123
        $ccinfo = self::getCardInfoFromStripe($token);
124
125
        $paymentMethodCred = new self();
126
        $paymentMethodCred->users_id = Di::getDefault()->getUserData()->getId();
127
        $paymentMethodCred->companies_id = Di::getDefault()->getUserData()->getDefaultCompany()->getId();
128
        $paymentMethodCred->apps_id = Di::getDefault()->getApp()->getId();
129
        $paymentMethodCred->payment_methods_id = PaymentMethods::getDefault()->getId();
130
        $paymentMethodCred->payment_ending_numbers = $ccinfo->card->last4;
131
        $paymentMethodCred->expiration_date = $ccinfo->card->exp_year . '-' . $ccinfo->card->exp_month . '-' .  '01';
132
        $paymentMethodCred->zip_code = $ccinfo->card->address_zip;
133
        $paymentMethodCred->created_at = date('Y-m-d H:m:s');
134
        $paymentMethodCred->saveOrFail();
135
136
        return $paymentMethodCred;
137
    }
138
139
    /**
140
     * Get Credit Card information from Stripe
141
     *
142
     * @param string $token
143
     * @return return StripeToken
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use PaymentMethodsCreds.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
144
     */
145
    private function getCardInfoFromStripe(string $token): StripeToken
146
    {
147
        return StripeToken::retrieve($token,[
148
            'api_key' => Di::getDefault()->getConfig()->stripe->secret
149
        ]);
150
    }
151
}
152