Passed
Pull Request — master (#65)
by Rafael
05:01
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 string
99
     */
100
    public $created_at;
101
102
    /**
103
     *
104
     * @var string
105
     */
106
    public $updated_at;
107
108
    /**
109
     *
110
     * @var integer
111
     */
112
    public $is_deleted;
113
114
    /**
115
     * Initialize
116
     *
117
     * @return void
118
     */
119 11
    public function initialize()
120
    {
121 11
        $this->belongsTo('user_id', 'Gewaer\Models\Users', 'id', ['alias' => 'user']);
122
123 11
        $this->belongsTo(
124 11
            'companies_id',
125 11
            'Gewaer\Models\Companies',
126 11
            'id',
127 11
            ['alias' => 'company']
128
        );
129
130 11
        $this->belongsTo(
131 11
            'apps_id',
132 11
            'Gewaer\Models\Apps',
133 11
            'id',
134 11
            ['alias' => 'app']
135
        );
136
137 11
        $this->belongsTo(
138 11
            'apps_plans_id',
139 11
            'Gewaer\Models\AppsPlans',
140 11
            'id',
141 11
            ['alias' => 'appPlan']
142
        );
143 11
    }
144
145
    /**
146
     * Get the active subscription for this company app
147
     *
148
     * @return void
149
     */
150 3
    public static function getActiveForThisApp() : Subscription
151
    {
152 3
        $subscription = self::findFirst([
153 3
            'conditions' => 'companies_id = ?0 and apps_id = ?1 and is_deleted  = 0',
154 3
            'bind' => [Di::getDefault()->getUserData()->currentCompanyId(), Di::getDefault()->getApp()->getId()]
155
        ]);
156
157 3
        if (!is_object($subscription)) {
158
            throw new ServerErrorHttpException(_('No active subscription for this app ' . Di::getDefault()->getApp()->getId() . ' at the company ' . Di::getDefault()->getUserData()->currentCompanyId()));
159
        }
160
161 3
        return $subscription;
162
    }
163
164
    /**
165
     * Get subscription by user's default company;
166
     * @param Users $user
167
     * @return Subscription
168
     */
169
    public static function getByDefaultCompany(Users $user): Subscription
170
    {
171
        $subscription = self::findFirst([
172
            'conditions' => 'user_id = ?0 and companies_id = ?1 and apps_id = ?2 and is_deleted  = 0',
173
            'bind' => [$user->id, $user->default_company, Di::getDefault()->getApp()->getId()]
174
        ]);
175
176
        if (!is_object($subscription)) {
177
            throw new ServerErrorHttpException('No active subscription for default company');
178
        }
179
180
        return $subscription;
181
    }
182
}
183