Test Failed
Pull Request — master (#46)
by Rafael
06:09
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 string
81
     */
82
    public $created_at;
83
84
    /**
85
     *
86
     * @var string
87
     */
88
    public $updated_at;
89
90
    /**
91
     *
92
     * @var integer
93
     */
94
    public $is_deleted;
95
96
    /**
97
     * Initialize
98
     *
99
     * @return void
100
     */
101 1
    public function initialize()
102
    {
103 1
        $this->belongsTo('user_id', 'Gewaer\Models\Users', 'id', ['alias' => 'user']);
104
105 1
        $this->belongsTo(
106 1
            'companies_id',
107 1
            'Gewaer\Models\Companies',
108 1
            'id',
109 1
            ['alias' => 'company']
110
        );
111
112 1
        $this->belongsTo(
113 1
            'apps_id',
114 1
            'Gewaer\Models\Apps',
115 1
            'id',
116 1
            ['alias' => 'app']
117
        );
118
119 1
        $this->belongsTo(
120 1
            'apps_plans_id',
121 1
            'Gewaer\Models\AppsPlans',
122 1
            'id',
123 1
            ['alias' => 'appPlan']
124
        );
125 1
    }
126
127
    /**
128
     * Get the active subscription for this company app
129
     *
130
     * @return void
131
     */
132 1
    public static function getActiveForThisApp() : Subscription
133
    {
134 1
        $subscription = self::findFirst([
135 1
            'conditions' => 'companies_id = ?0 and apps_id = ?1 and is_deleted  = 0',
136 1
            'bind' => [Di::getDefault()->getUserData()->currentCompanyId(), Di::getDefault()->getApp()->getId()]
137
        ]);
138
139
        if (!is_object($subscription)) {
140
            throw new ServerErrorHttpException(_('No active subscription for this app ' . Di::getDefault()->getApp()->getId() . ' at the company ' . Di::getDefault()->getUserData()->currentCompanyId()));
141
        }
142
143
        return $subscription;
144
    }
145
}
146