Passed
Pull Request — master (#21)
by Maximo
04:23
created

Subscription::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 23
ccs 17
cts 17
cp 1
crap 1
rs 9.7333
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
class Subscription extends PhalconSubscription
10
{
11
    /**
12
     *
13
     * @var integer
14
     */
15
    public $apps_plans_id = 0;
16
17
    /**
18
     *
19
     * @var integer
20
     */
21
    public $user_id;
22
23
    /**
24
     *
25
     * @var integer
26
     */
27
    public $company_id;
28
29
    /**
30
     *
31
     * @var integer
32
     */
33
    public $apps_id;
34
35
    /**
36
     *
37
     * @var string
38
     */
39
    public $name;
40
41
    /**
42
     *
43
     * @var string
44
     */
45
    public $stripe_id;
46
47
    /**
48
     *
49
     * @var integer
50
     */
51
    public $quantity;
52
53
    /**
54
     *
55
     * @var string
56
     */
57
    public $trial_ends_at;
58
59
    /**
60
     *
61
     * @var string
62
     */
63
    public $created_at;
64
65
    /**
66
     *
67
     * @var string
68
     */
69
    public $updated_at;
70
71
    /**
72
     *
73
     * @var integer
74
     */
75
    public $is_deleted;
76
77
    /**
78
     * Initialize
79
     *
80
     * @return void
81
     */
82 10
    public function initialize()
83
    {
84 10
        $this->belongsTo('user_id', 'Gewaer\Models\Users', 'id', ['alias' => 'user']);
85
86 10
        $this->belongsTo(
87 10
            'company_id',
88 10
            'Gewaer\Models\Companies',
89 10
            'id',
90 10
            ['alias' => 'company']
91
        );
92
93 10
        $this->belongsTo(
94 10
            'apps_id',
95 10
            'Gewaer\Models\Apps',
96 10
            'id',
97 10
            ['alias' => 'app']
98
        );
99
100 10
        $this->belongsTo(
101 10
            'apps_plans_id',
102 10
            'Gewaer\Models\AppsPlans',
103 10
            'id',
104 10
            ['alias' => 'appPlan']
105
        );
106 10
    }
107
108
    /**
109
     * Get the active subscription for this company app
110
     *
111
     * @return void
112
     */
113 4
    public static function getActiveForThisApp() : Subscription
114
    {
115 4
        $subscription = self::findFirst([
116 4
            'conditions' => 'company_id = ?0 and apps_id = ?1 and is_deleted  = 0',
117 4
            'bind' => [Di::getDefault()->getUserData()->default_company, Di::getDefault()->getApp()->getId()]
118
        ]);
119
120 4
        if (!is_object($subscription)) {
121
            throw new ServerErrorHttpException(_('No active subscription for this app ' . Di::getDefault()->getApp()->getId() . ' at the company ' . Di::getDefault()->getUserData()->default_company));
122
        }
123
124 4
        return $subscription;
125
    }
126
}
127