AppsPlans::get()   A
last analyzed

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 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Canvas\Models;
5
6
use Canvas\Exception\ModelException;
7
use Phalcon\Di;
8
9
/**
10
 * Class AppsPlans.
11
 *
12
 * @package Canvas\Models
13
 *
14
 * @property Users $user
15
 * @property Config $config
16
 * @property Apps $app
17
 * @property Companies $defaultCompany
18
 * @property \Phalcon\Di $di
19
 */
20
class AppsPlans extends AbstractModel
21
{
22
    /**
23
     *
24
     * @var integer
25
     */
26
    public $id;
27
28
    /**
29
     *
30
     * @var integer
31
     */
32
    public $apps_id;
33
34
    /**
35
     *
36
     * @var string
37
     */
38
    public $name;
39
40
    /**
41
     *
42
     * @var string
43
     */
44
    public $description;
45
46
    /**
47
     *
48
     * @var string
49
     */
50
    public $stripe_id;
51
52
    /**
53
     *
54
     * @var string
55
     */
56
    public $stripe_plan;
57
58
    /**
59
     *
60
     * @var double
61
     */
62
    public $pricing;
63
64
    /**
65
     *
66
     * @var integer
67
     */
68
    public $currency_id;
69
70
    /**
71
     *
72
     * @var integer
73
     */
74
    public $free_trial_dates;
75
76
    /**
77
     *
78
     * @var integer
79
     */
80
    public $is_default;
81
82
    /**
83
     *
84
     * @var integer
85
     */
86
    public $payment_frequencies_id;
87
88
    /**
89
     *
90
     * @var string
91
     */
92
    public $created_at;
93
94
    /**
95
     *
96
     * @var string
97
     */
98
    public $updated_at;
99
100
    /**
101
     *
102
     * @var integer
103
     */
104
    public $is_deleted;
105
106
    /**
107
     * Initialize method for model.
108
     */
109
    public function initialize()
110
    {
111
        $this->setSource('apps_plans');
112
113
        $this->belongsTo(
114
            'apps_id',
115
            'Canvas\Models\Apps',
116
            'id',
117
            ['alias' => 'app']
118
        );
119
120
        $this->belongsTo(
121
            'payment_frequencies_id',
122
            'Canvas\Models\PaymentFrequencies',
123
            'id',
124
            ['alias' => 'paymentFrequecies']
125
        );
126
127
        $this->hasMany(
128
            'apps_id',
129
            'Canvas\Models\AppsPlansSettings',
130
            'apps_id',
131
            ['alias' => 'settings']
132
        );
133
    }
134
135
    /**
136
     * Returns table name mapped in the model.
137
     *
138
     * @return string
139
     */
140
    public function getSource() : string
141
    {
142
        return 'apps_plans';
143
    }
144
145
    /**
146
     * Just a preatty function that returns the same object for.
147
     *
148
     * $app->settings()->set(key, value);
149
     * $app->settings()->get(key);
150
     * $app->settings()->get(key)->delete();
151
     *
152
     * @return AppsPlans
153
     */
154
    public function settings() : AppsPlans
155
    {
156
        return $this;
157
    }
158
159
    /**
160
     * Get the default plan for this given app.
161
     *
162
     * @return AppsPlans
163
     */
164
    public static function getDefaultPlan() : AppsPlans
165
    {
166
        return AppsPlans::findFirst([
167
            'conditions' => 'apps_id = ?0 and is_default = 1',
168
            'bind' => [Di::getDefault()->getApp()->getId()]
169
        ]);
170
    }
171
172
    /**
173
     * Get the value of the settins by it key.
174
     *
175
     * @param string $key
176
     * @param string $value
177
     */
178
    public function get(string $key) : ?string
179
    {
180
        $setting = AppsPlansSettings::findFirst([
181
            'conditions' => 'apps_plans_id = ?0 and apps_id = ?1 and key = ?2',
182
            'bind' => [$this->getId(), $this->apps_id, $key]
183
        ]);
184
185
        if (is_object($setting)) {
186
            return (string) $setting->value;
187
        }
188
189
        return null;
190
    }
191
192
    /**
193
     * Set a setting for the given app.
194
     *
195
     * @param string $key
196
     * @param string $value
197
     */
198
    public function set(string $key, $value) : bool
199
    {
200
        $setting = AppsPlansSettings::findFirst([
201
            'conditions' => 'apps_plans_id = ?0 and apps_id = ?1 and key = ?2',
202
            'bind' => [$this->getId(), $this->apps_id, $key]
203
        ]);
204
205
        if (!is_object($setting)) {
206
            $setting = new AppsPlansSettings();
207
            $setting->apps_plans_id = $this->getId();
208
            $setting->apps_id = $this->getId();
209
            $setting->key = $key;
210
        }
211
212
        $setting->value = $value;
213
214
        $setting->saveOrFail();
215
216
        return true;
217
    }
218
219
    /**
220
     * After save.
221
     *
222
     * @return void
223
     */
224
    public function afterSave()
225
    {
226
        //if we udpate the is_default for this plan we need to remove all others and update the main app
227
        if ($this->is_default) {
228
            $this->app->default_apps_plan_id = $this->getId();
229
            $this->app->update();
230
        }
231
    }
232
}
233