Test Failed
Pull Request — master (#52)
by Rafael
05:00
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 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 2
    public function initialize()
120
    {
121 2
        $this->belongsTo('user_id', 'Gewaer\Models\Users', 'id', ['alias' => 'user']);
122
123 2
        $this->belongsTo(
124 2
            'companies_id',
125 2
            'Gewaer\Models\Companies',
126 2
            'id',
127 2
            ['alias' => 'company']
128
        );
129
130 2
        $this->belongsTo(
131 2
            'apps_id',
132 2
            'Gewaer\Models\Apps',
133 2
            'id',
134 2
            ['alias' => 'app']
135
        );
136
137 2
        $this->belongsTo(
138 2
            'apps_plans_id',
139 2
            'Gewaer\Models\AppsPlans',
140 2
            'id',
141 2
            ['alias' => 'appPlan']
142
        );
143 2
    }
144
145
    /**
146
     * Get the active subscription for this company app
147
     *
148
     * @return void
149
     */
150 1
    public static function getActiveForThisApp() : Subscription
151
    {
152 1
        $subscription = self::findFirst([
153 1
            'conditions' => 'companies_id = ?0 and apps_id = ?1 and is_deleted  = 0',
154 1
            'bind' => [Di::getDefault()->getUserData()->currentCompanyId(), Di::getDefault()->getApp()->getId()]
155
        ]);
156
157
        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
        return $subscription;
162
    }
163
}
164