Failed Conditions
Pull Request — master (#334)
by Rafael
02:57
created

PaymentMethodsCreds::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
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" => [Di::getDefault()->getUserData()->getId(),Di::getDefault()->getUserData()->getDefaultCompany()->getId(),Di::getDefault()->getApp()->getId()],
106
            "order"=> "id DESC"
107
        ]);
108
    }
109
110
    /**
111
     * Create a new record from Stripe Token
112
     *
113
     * @param string $token
114
     * @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...
115
     */
116
    public static function createByStripeToken(string $token): self
117
    {
118
        $ccinfo = self::getCardInfoFromStripe($token);
119
120
        $paymentMethodCred = new self();
121
        $paymentMethodCred->users_id = Di::getDefault()->getUserData()->getId();
122
        $paymentMethodCred->companies_id = Di::getDefault()->getUserData()->getDefaultCompany()->getId();
123
        $paymentMethodCred->apps_id = Di::getDefault()->getApp()->getId();
124
        $paymentMethodCred->payment_methods_id = PaymentMethods::getDefault()->getId();
125
        $paymentMethodCred->payment_ending_numbers = $ccinfo->card->last4;
126
        $paymentMethodCred->expiration_date = $ccinfo->card->exp_year . '-' . $ccinfo->card->exp_month . '-' .  '01';
127
        $paymentMethodCred->zip_code = $ccinfo->card->address_zip;
128
        $paymentMethodCred->created_at = date('Y-m-d H:m:s');
129
        $paymentMethodCred->saveOrFail();
130
131
        return $paymentMethodCred;
132
    }
133
134
    /**
135
     * Get Credit Card information from Stripe
136
     *
137
     * @param string $token
138
     * @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...
139
     */
140
    private function getCardInfoFromStripe(string $token): StripeToken
141
    {
142
        return StripeToken::retrieve($token,[
143
            'api_key' => Di::getDefault()->getConfig()->stripe->secret
144
        ]);
145
    }
146
}
147