Failed Conditions
Branch hotfix-documentation-kanvas (a7467b)
by Maximo
03:15
created

Subscription::cancel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Canvas\Models;
4
5
use Phalcon\Cashier\Subscription as PhalconSubscription;
6
use Canvas\Exception\ServerErrorHttpException;
7
use Phalcon\Di;
8
use Carbon\Carbon;
9
10
/**
11
 * Trait Subscription.
12
 *
13
 * @package Canvas\Models
14
 *
15
 * @property Users $user
16
 * @property AppsPlans $appPlan
17
 * @property CompanyBranches $branches
18
 * @property Companies $company
19
 * @property UserCompanyApps $app
20
 * @property \Phalcon\Di $di
21
 *
22
 */
23
class Subscription extends PhalconSubscription
24
{
25
    /**
26
     *
27
     * @var integer
28
     */
29
    public $apps_plans_id = 0;
0 ignored issues
show
Coding Style introduced by
$apps_plans_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...
30
31
    /**
32
     *
33
     * @var integer
34
     */
35
    public $user_id;
0 ignored issues
show
Coding Style introduced by
$user_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...
36
37
    /**
38
     *
39
     * @var integer
40
     */
41
    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...
42
43
    /**
44
     *
45
     * @var integer
46
     */
47
    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...
48
49
    /**
50
     *
51
     * @var string
52
     */
53
    public $name;
54
55
    /**
56
     *
57
     * @var string
58
     */
59
    public $stripe_id;
0 ignored issues
show
Coding Style introduced by
$stripe_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...
60
61
    /**
62
     *
63
     * @var string
64
     */
65
    public $stripe_plan;
0 ignored issues
show
Coding Style introduced by
$stripe_plan 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...
66
67
    /**
68
     *
69
     * @var integer
70
     */
71
    public $quantity;
72
73
    /**
74
     *
75
     * @var integer
76
     */
77
    public $payment_frequency_id;
0 ignored issues
show
Coding Style introduced by
$payment_frequency_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...
78
79
    /**
80
     *
81
     * @var string
82
     */
83
    public $trial_ends_at;
0 ignored issues
show
Coding Style introduced by
$trial_ends_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...
84
85
    /**
86
     *
87
     * @var integer
88
     */
89
    public $trial_ends_days;
0 ignored issues
show
Coding Style introduced by
$trial_ends_days 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...
90
91
    /**
92
     *
93
     * @var integer
94
     */
95
    public $is_freetrial;
0 ignored issues
show
Coding Style introduced by
$is_freetrial 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...
96
97
    /**
98
     *
99
     * @var integer
100
     */
101
    public $is_active;
0 ignored issues
show
Coding Style introduced by
$is_active 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...
102
103
    /**
104
     *
105
     * @var integer
106
     */
107
    public $paid;
108
109
    /**
110
     *
111
     * @var string
112
     */
113
    public $charge_date;
0 ignored issues
show
Coding Style introduced by
$charge_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...
114
115
    /**
116
     *
117
     * @var string
118
     */
119
    public $ends_at;
0 ignored issues
show
Coding Style introduced by
$ends_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...
120
121
    /**
122
     *
123
     * @var date
124
     */
125
    public $grace_period_ends;
0 ignored issues
show
Coding Style introduced by
$grace_period_ends 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...
126
127
    /**
128
     *
129
     * @var datetime
130
     */
131
    public $next_due_payment;
0 ignored issues
show
Coding Style introduced by
$next_due_payment 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...
132
133
    /**
134
     *
135
     * @var integer
136
     */
137
    public $is_cancelled;
0 ignored issues
show
Coding Style introduced by
$is_cancelled 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...
138
139
    /**
140
     *
141
     * @var string
142
     */
143
    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...
144
145
    /**
146
     *
147
     * @var string
148
     */
149
    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...
150
151
    /**
152
     *
153
     * @var integer
154
     */
155
    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...
156
157
    /**
158
     * Initialize.
159
     *
160
     * @return void
161
     */
162
    public function initialize()
163
    {
164
        $this->belongsTo('user_id', 'Canvas\Models\Users', 'id', ['alias' => 'user']);
165
166
        $this->belongsTo(
167
            'companies_id',
168
            'Canvas\Models\Companies',
169
            'id',
170
            ['alias' => 'company']
171
        );
172
173
        $this->belongsTo(
174
            'apps_id',
175
            'Canvas\Models\Apps',
176
            'id',
177
            ['alias' => 'app']
178
        );
179
180
        $this->belongsTo(
181
            'apps_plans_id',
182
            'Canvas\Models\AppsPlans',
183
            'id',
184
            ['alias' => 'appPlan']
185
        );
186
    }
187
188
    /**
189
     * Get the active subscription for this company app.
190
     *
191
     * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be Subscription?

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...
192
     */
193
    public static function getActiveForThisApp() : Subscription
194
    {
195
        $subscription = self::findFirst([
196
            'conditions' => 'companies_id = ?0 and apps_id = ?1 and is_deleted  = 0',
197
            'bind' => [Di::getDefault()->getUserData()->currentCompanyId(), Di::getDefault()->getApp()->getId()]
198
        ]);
199
200
        if (!is_object($subscription)) {
201
            throw new ServerErrorHttpException(_('No active subscription for this app ' . Di::getDefault()->getApp()->getId() . ' at the company ' . Di::getDefault()->getUserData()->currentCompanyId()));
202
        }
203
204
        return $subscription;
205
    }
206
207
    /**
208
     * Get subscription by user's default company;.
209
     * @param Users $user
210
     * @return Subscription
211
     */
212
    public static function getByDefaultCompany(Users $user): Subscription
213
    {
214
        $subscription = self::findFirst([
215
            'conditions' => 'user_id = ?0 and companies_id = ?1 and apps_id = ?2 and is_deleted  = 0',
216
            'bind' => [$user->getId(), $user->defaultCompany->getId(), Di::getDefault()->getApp()->getId()]
217
        ]);
218
219
        if (!is_object($subscription)) {
220
            throw new ServerErrorHttpException('No active subscription for default company');
221
        }
222
223
        return $subscription;
224
    }
225
226
    /**
227
     * Search current company's app setting with key paid to verify payment status for current company.
228
     *
229
     * @param Users $user
230
     * @return bool
231
     */
232
    public static function getPaymentStatus(Users $user): bool
0 ignored issues
show
Coding Style introduced by
function getPaymentStatus() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

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...
233
    {
234
        //if its not subscription based return true to ignore any payment status
235
        if (!Di::getDefault()->getApp()->subscriptioBased()) {
236
            return true;
237
        }
238
239
        if (!$user->defaultCompany->get('paid')) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return (bool) $user->defaultCompany->get('paid');.
Loading history...
240
            return false;
241
        }
242
243
        return true;
244
    }
245
246
    /**
247
     * Determine if the subscription is active.
248
     *
249
     * @return bool
250
     */
251
    public function active(): bool
0 ignored issues
show
Coding Style introduced by
function active() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

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...
252
    {
253
        return (bool) $this->is_active;
254
    }
255
256
    /**
257
     * Is the subscriptoin paid?
258
     *
259
     * @return boolean
260
     */
261
    public function paid(): bool
0 ignored issues
show
Coding Style introduced by
function paid() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

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...
262
    {
263
        return (bool) $this->paid;
264
    }
265
266
    /**
267
     * Given a not active subscription activate it
268
     *
269
     * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

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...
270
     */
271
    public function activate(): bool
0 ignored issues
show
Coding Style introduced by
function activate() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

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...
272
    {
273
        $this->is_active = 1;
274
        $this->paid = 1;
275
        $this->grace_period_ends = '';
0 ignored issues
show
Documentation Bug introduced by
It seems like '' of type string is incompatible with the declared type object<Canvas\Models\date> of property $grace_period_ends.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
276
        $this->ends_at = Carbon::now()->addDays(30)->toDateTimeString();
277
        $this->next_due_payment = $this->ends_at;
278
        $this->is_cancelled = 0;
279
        return $this->update();
280
    }
281
282
    /**
283
     * Determine if the subscription is within its trial period.
284
     *
285
     * @return bool
286
     */
287
    public function onTrial()
0 ignored issues
show
Coding Style introduced by
function onTrial() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

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...
288
    {
289
        return (bool)$this->is_freetrial;
290
    }
291
}
292