Failed Conditions
Pull Request — master (#250)
by Rafael
03:27
created

Subscription::validateByGracePeriod()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 0
dl 0
loc 11
ccs 0
cts 0
cp 0
crap 20
rs 9.9
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
    const DEFAULT_GRACE_PERIOD_DAYS = 5;
26
    /**
27
     *
28
     * @var integer
29
     */
30
    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...
31
32
    /**
33
     *
34
     * @var integer
35
     */
36
    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...
37
38
    /**
39
     *
40
     * @var integer
41
     */
42
    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...
43
44
    /**
45
     *
46
     * @var integer
47
     */
48
    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...
49
50
    /**
51
     *
52
     * @var string
53
     */
54
    public $name;
55
56
    /**
57
     *
58
     * @var string
59
     */
60
    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...
61
62
    /**
63
     *
64
     * @var string
65
     */
66
    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...
67
68
    /**
69
     *
70
     * @var integer
71
     */
72
    public $quantity;
73
74
    /**
75
     *
76
     * @var integer
77
     */
78
    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...
79
80
    /**
81
     *
82
     * @var string
83
     */
84
    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...
85
86
    /**
87
     *
88
     * @var integer
89
     */
90
    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...
91
92
    /**
93
     *
94
     * @var integer
95
     */
96
    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...
97
98
    /**
99
     *
100
     * @var integer
101
     */
102
    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...
103
104
    /**
105
     *
106
     * @var integer
107
     */
108
    public $paid;
109
110
    /**
111
     *
112
     * @var string
113
     */
114
    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...
115
116
    /**
117
     *
118
     * @var string
119
     */
120
    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...
121
122
    /**
123
     *
124
     * @var date
125
     */
126
    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...
127
128
    /**
129
     *
130
     * @var datetime
131
     */
132
    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...
133
134
    /**
135
     *
136
     * @var integer
137
     */
138
    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...
139
140
    /**
141
     *
142
     * @var string
143
     */
144
    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...
145
146
    /**
147
     *
148
     * @var string
149
     */
150
    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...
151
152
    /**
153
     *
154
     * @var integer
155
     */
156
    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...
157
158
    /**
159
     * Initialize.
160
     *
161
     * @return void
162
     */
163
    public function initialize()
164
    {
165
        $this->belongsTo('user_id', 'Canvas\Models\Users', 'id', ['alias' => 'user']);
166
167
        $this->belongsTo(
168
            'companies_id',
169
            'Canvas\Models\Companies',
170
            'id',
171
            ['alias' => 'company']
172
        );
173
174
        $this->belongsTo(
175
            'apps_id',
176
            'Canvas\Models\Apps',
177
            'id',
178
            ['alias' => 'app']
179
        );
180
181
        $this->belongsTo(
182
            'apps_plans_id',
183
            'Canvas\Models\AppsPlans',
184
            'id',
185
            ['alias' => 'appPlan']
186
        );
187
    }
188
189
    /**
190
     * Get the active subscription for this company app.
191
     *
192
     * @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...
193
     */
194
    public static function getActiveForThisApp() : Subscription
195
    {
196
        $subscription = self::findFirst([
197
            'conditions' => 'companies_id = ?0 and apps_id = ?1 and is_deleted  = 0',
198
            'bind' => [Di::getDefault()->getUserData()->currentCompanyId(), Di::getDefault()->getApp()->getId()]
199
        ]);
200
201
        if (!is_object($subscription)) {
202
            throw new ServerErrorHttpException(_('No active subscription for this app ' . Di::getDefault()->getApp()->getId() . ' at the company ' . Di::getDefault()->getUserData()->currentCompanyId()));
0 ignored issues
show
Deprecated Code introduced by
The class Canvas\Exception\ServerErrorHttpException has been deprecated with message: version 0.1.5

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
203
        }
204
205
        return $subscription;
206
    }
207
208
    /**
209
     * Get subscription by user's default company;.
210
     * @param Users $user
211
     * @return Subscription
212
     */
213
    public static function getByDefaultCompany(Users $user): Subscription
214
    {
215
        $subscription = self::findFirst([
216
            'conditions' => 'user_id = ?0 and companies_id = ?1 and apps_id = ?2 and is_deleted  = 0',
217
            'bind' => [$user->getId(), $user->defaultCompany->getId(), Di::getDefault()->getApp()->getId()]
218
        ]);
219
220
        if (!is_object($subscription)) {
221
            throw new ServerErrorHttpException('No active subscription for default company');
0 ignored issues
show
Deprecated Code introduced by
The class Canvas\Exception\ServerErrorHttpException has been deprecated with message: version 0.1.5

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
222
        }
223
224
        return $subscription;
225
    }
226
227
    /**
228
     * Search current company's app setting with key paid to verify payment status for current company.
229
     *
230
     * @param Users $user
231
     * @return bool
232
     */
233
    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...
234
    {
235
        //if its not subscription based return true to ignore any payment status
236
        if (!Di::getDefault()->getApp()->subscriptioBased()) {
237
            return true;
238
        }
239
240
        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...
241
            return false;
242
        }
243
244
        return true;
245
    }
246
247
    /**
248
     * Determine if the subscription is active.
249
     *
250
     * @return bool
251
     */
252
    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...
253
    {
254
        return (bool) $this->is_active;
255
    }
256
257
    /**
258
     * Is the subscriptoin paid?
259
     *
260
     * @return boolean
261
     */
262
    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...
263
    {
264
        return (bool) $this->paid;
265
    }
266
267
    /**
268
     * Given a not active subscription activate it.
269
     *
270
     * @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...
271
     */
272
    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...
273
    {
274
        $this->is_active = 1;
275
        $this->paid = 1;
276
        $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...
277
        $this->ends_at = Carbon::now()->addDays(30)->toDateTimeString();
278
        $this->next_due_payment = $this->ends_at;
279
        $this->is_cancelled = 0;
280
        return $this->update();
281
    }
282
283
    /**
284
     * Determine if the subscription is within its trial period.
285
     *
286
     * @return bool
287
     */
288
    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...
289
    {
290
        return (bool)$this->is_freetrial;
291
    }
292
293
    /**
294
     * Get actual subscription.
295
     */
296
    public static function getActiveSubscription(): self
297
    {
298
        $userSubscription = PhalconSubscription::findFirstOrFail([
299
            'conditions' => 'user_id = ?0 and companies_id = ?1 and apps_id = ?2 and is_deleted  = 0',
300
            'bind' => [Di::getDefault()->getUserData()->getId(), Di::getDefault()->getUserData()->currentCompanyId(), Di::getDefault()->getApp()->getId()]
301
        ]);
302
303
        return Di::getDefault()->getUserData()->subscription($userSubscription->name);
304
    }
305
306
    /**
307
     * Validate subscription status by grace period date and update grace period date.
308
     *
309
     * @return void
310
     */
311
    public function validateByGracePeriod(): void
312
    {
313
        if (isset($this->grace_period_ends)) {
314
            if (($this->charge_date == $this->grace_period_ends) && !$this->paid) {
315
                $this->is_active = 0;
316
                $this->grace_period_ends = Carbon::now()->addDays(Subscription::DEFAULT_GRACE_PERIOD_DAYS)->toDateTimeString();
317
            }
318
        } else {
319
            $this->grace_period_ends = Carbon::now()->addDays(Subscription::DEFAULT_GRACE_PERIOD_DAYS)->toDateTimeString();
320
        }
321
    }
322
}
323