Passed
Pull Request — master (#65)
by Rafael
05:16
created

Subscription::getByDefaultCompany()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
ccs 0
cts 7
cp 0
crap 6
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 string
75
     */
76
    public $trial_ends_at;
77
78
    /**
79
     *
80
     * @var integer
81
     */
82
    public $trial_ends_days;
83
84
    /**
85
     *
86
     * @var integer
87
     */
88
    public $is_freetrial;
89
90
    /**
91
     *
92
     * @var integer
93
     */
94
    public $is_active;
95
96
    /**
97
     *
98
     * @var integer
99
     */
100
    public $paid;
101
102
    /**
103
     *
104
     * @var string
105
     */
106
    public $charge_date;
107
108
    /**
109
     *
110
     * @var string
111
     */
112
    public $created_at;
113
114
    /**
115
     *
116
     * @var string
117
     */
118
    public $updated_at;
119
120
    /**
121
     *
122
     * @var integer
123
     */
124
    public $is_deleted;
125
126
    /**
127
     * Initialize
128
     *
129
     * @return void
130
     */
131 11
    public function initialize()
132
    {
133 11
        $this->belongsTo('user_id', 'Gewaer\Models\Users', 'id', ['alias' => 'user']);
134
135 11
        $this->belongsTo(
136 11
            'companies_id',
137 11
            'Gewaer\Models\Companies',
138 11
            'id',
139 11
            ['alias' => 'company']
140
        );
141
142 11
        $this->belongsTo(
143 11
            'apps_id',
144 11
            'Gewaer\Models\Apps',
145 11
            'id',
146 11
            ['alias' => 'app']
147
        );
148
149 11
        $this->belongsTo(
150 11
            'apps_plans_id',
151 11
            'Gewaer\Models\AppsPlans',
152 11
            'id',
153 11
            ['alias' => 'appPlan']
154
        );
155 11
    }
156
157
    /**
158
     * Get the active subscription for this company app
159
     *
160
     * @return void
161
     */
162 3
    public static function getActiveForThisApp() : Subscription
163
    {
164 3
        $subscription = self::findFirst([
165 3
            'conditions' => 'companies_id = ?0 and apps_id = ?1 and is_deleted  = 0',
166 3
            'bind' => [Di::getDefault()->getUserData()->currentCompanyId(), Di::getDefault()->getApp()->getId()]
167
        ]);
168
169 3
        if (!is_object($subscription)) {
170
            throw new ServerErrorHttpException(_('No active subscription for this app ' . Di::getDefault()->getApp()->getId() . ' at the company ' . Di::getDefault()->getUserData()->currentCompanyId()));
171
        }
172
173 3
        return $subscription;
174
    }
175
176
    /**
177
     * Get subscription by user's default company;
178
     * @param Users $user
179
     * @return Subscription
180
     */
181
    public static function getByDefaultCompany(Users $user): Subscription
182
    {
183
        $subscription = self::findFirst([
184
            'conditions' => 'user_id = ?0 and companies_id = ?1 and apps_id = ?2 and is_deleted  = 0',
185
            'bind' => [$user->id, $user->default_company, Di::getDefault()->getApp()->getId()]
186
        ]);
187
188
        if (!is_object($subscription)) {
189
            throw new ServerErrorHttpException('No active subscription for default company');
190
        }
191
192
        return $subscription;
193
    }
194
}
195