Test Failed
Pull Request — master (#80)
by Maximo
05:46
created

Subscription::getActiveForThisApp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.3149

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 12
ccs 4
cts 7
cp 0.5714
crap 2.3149
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Gewaer\Models;
4
5
use Phalcon\Cashier\Subscription as PhalconSubscription;
6
use Gewaer\Exception\ServerErrorHttpException;
7
use Phalcon\Di;
8
9
/**
10
 * Trait Subscription
11
 *
12
 * @package Gewaer\Models
13
 *
14
 * @property Users $user
15
 * @property AppsPlans $appPlan
16
 * @property CompanyBranches $branches
17
 * @property Companies $company
18
 * @property UserCompanyApps $app
19
 * @property \Phalcon\Di $di
20
 *
21
 */
22
class Subscription extends PhalconSubscription
23
{
24
    /**
25
     *
26
     * @var integer
27
     */
28
    public $apps_plans_id = 0;
29
30
    /**
31
     *
32
     * @var integer
33
     */
34
    public $user_id;
35
36
    /**
37
     *
38
     * @var integer
39
     */
40
    public $companies_id;
41
42
    /**
43
     *
44
     * @var integer
45
     */
46
    public $apps_id;
47
48
    /**
49
     *
50
     * @var string
51
     */
52
    public $name;
53
54
    /**
55
     *
56
     * @var string
57
     */
58
    public $stripe_id;
59
60
    /**
61
     *
62
     * @var string
63
     */
64
    public $stripe_plan;
65
66
    /**
67
     *
68
     * @var integer
69
     */
70
    public $quantity;
71
72
    /**
73
     *
74
     * @var integer
75
     */
76
    public $payment_frequency_id;
77
78
    /**
79
     *
80
     * @var string
81
     */
82
    public $trial_ends_at;
83
84
    /**
85
     *
86
     * @var integer
87
     */
88
    public $trial_ends_days;
89
90
    /**
91
     *
92
     * @var integer
93
     */
94
    public $is_freetrial;
95
96
    /**
97
     *
98
     * @var integer
99
     */
100
    public $is_active;
101
102
    /**
103
     *
104
     * @var integer
105
     */
106
    public $paid;
107
108
    /**
109
     *
110
     * @var string
111
     */
112
    public $charge_date;
113
114
    /**
115
     *
116
     * @var string
117
     */
118
    public $created_at;
119
120
    /**
121
     *
122
     * @var string
123
     */
124
    public $updated_at;
125
126
    /**
127
     *
128
     * @var integer
129
     */
130
    public $is_deleted;
131
132
    /**
133
     * Initialize
134
     *
135
     * @return void
136
     */
137 1
    public function initialize()
138
    {
139 1
        $this->belongsTo('user_id', 'Gewaer\Models\Users', 'id', ['alias' => 'user']);
140
141 1
        $this->belongsTo(
142 1
            'companies_id',
143 1
            'Gewaer\Models\Companies',
144 1
            'id',
145 1
            ['alias' => 'company']
146
        );
147
148 1
        $this->belongsTo(
149 1
            'apps_id',
150 1
            'Gewaer\Models\Apps',
151 1
            'id',
152 1
            ['alias' => 'app']
153
        );
154
155 1
        $this->belongsTo(
156 1
            'apps_plans_id',
157 1
            'Gewaer\Models\AppsPlans',
158 1
            'id',
159 1
            ['alias' => 'appPlan']
160
        );
161 1
    }
162
163
    /**
164
     * Get the active subscription for this company app
165
     *
166
     * @return void
167
     */
168 1
    public static function getActiveForThisApp() : Subscription
169
    {
170 1
        $subscription = self::findFirst([
171 1
            'conditions' => 'companies_id = ?0 and apps_id = ?1 and is_deleted  = 0',
172 1
            'bind' => [Di::getDefault()->getUserData()->currentCompanyId(), Di::getDefault()->getApp()->getId()]
173
        ]);
174
175
        if (!is_object($subscription)) {
176
            throw new ServerErrorHttpException(_('No active subscription for this app ' . Di::getDefault()->getApp()->getId() . ' at the company ' . Di::getDefault()->getUserData()->currentCompanyId()));
177
        }
178
179
        return $subscription;
180
    }
181
182
    /**
183
     * Get subscription by user's default company;
184
     * @param Users $user
185
     * @return Subscription
186
     */
187
    public static function getByDefaultCompany(Users $user): Subscription
188
    {
189
        $subscription = self::findFirst([
190
            'conditions' => 'user_id = ?0 and companies_id = ?1 and apps_id = ?2 and is_deleted  = 0',
191
            'bind' => [$user->getId(), $user->defaultCompany->getId(), Di::getDefault()->getApp()->getId()]
192
        ]);
193
194
        if (!is_object($subscription)) {
195
            throw new ServerErrorHttpException('No active subscription for default company');
196
        }
197
198
        return $subscription;
199
    }
200
201
    /**
202
     * Search current company's app setting with key paid to verify payment status for current company
203
     * @return bool
204
     */
205
    public static function getPaymentStatus(): bool
206
    {
207
        $subscriptionPaid = CompaniesSettings::findFirst([
208
            'conditions' => "companies_id = ?0 and name = 'paid' and is_deleted = 0",
209
            'bind' => [Di::getDefault()->getUserData()->default_company]
210
        ]);
211
212
        if (!$subscriptionPaid->value) {
213
            return false;
214
        }
215
216
        return true;
217
    }
218
}
219