Passed
Pull Request — master (#52)
by Rafael
05:54
created

TrialTask::unsetTrialEndsAtAction()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 0
dl 0
loc 16
ccs 0
cts 13
cp 0
crap 20
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Gewaer\Cli\Tasks;
4
5
use Phalcon\Cli\Task as PhTask;
6
use Gewaer\Models\Subscription;
7
use Carbon\Carbon;
8
use \Datetime;
9
10
class TrialTask extends PhTask
11
{
12
    /**
13
     * Unset subscription trial_ends_at if trial has ended
14
     * @return void
15
     */
16
    public function unsetTrialEndsAtAction(): void
17
    {
18
        $subscriptions = Subscription::find([
19
            'conditions' => 'is_deleted = 0 and is_active = 1'
20
        ]);
21
22
        foreach ($subscriptions as $subscription) {
23
            $trialEnds = new Datetime($subscription->trial_ends_at);
24
            $trialEnds->setTime(0, 0);
25
            $formattedTrialEnds = $trialEnds->format('Y-m-d');
26
27
            if ($formattedTrialEnds == Carbon::today()->toDateString()) {
28
                $subscription->trial_ends_at = null;
29
30
                if ($subscription->update()) {
31
                    echo("Company: {$subscription->id} trial has ended, so its trial_ends_at is NULL \n");
32
                }
33
            }
34
        }
35
    }
36
}
37