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

AppsPlans   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Test Coverage

Coverage 84.44%

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 199
ccs 38
cts 45
cp 0.8444
rs 10
c 0
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getSource() 0 3 1
A getDefaultPlan() 0 5 1
A set() 0 21 3
A settings() 0 3 1
A afterSave() 0 6 2
A initialize() 0 16 1
A get() 0 12 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Gewaer\Models;
5
6
use Gewaer\Exception\ModelException;
7
use Phalcon\Di;
8
9
/**
10
 * Class AppsPlans
11
 *
12
 * @package Gewaer\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 string
85
     */
86
    public $created_at;
87
88
    /**
89
     *
90
     * @var string
91
     */
92
    public $updated_at;
93
94
    /**
95
     *
96
     * @var integer
97
     */
98
    public $is_deleted;
99
100
    /**
101
     * Initialize method for model.
102
     */
103 10
    public function initialize()
104
    {
105 10
        $this->setSource('apps_plans');
106
107 10
        $this->belongsTo(
108 10
            'apps_id',
109 10
            'Gewaer\Models\Apps',
110 10
            'id',
111 10
            ['alias' => 'app']
112
        );
113
114 10
        $this->hasMany(
115 10
            'apps_id',
116 10
            'Gewaer\Models\AppsPlansSettings',
117 10
            'apps_id',
118 10
            ['alias' => 'settings']
119
        );
120 10
    }
121
122
    /**
123
     * Returns table name mapped in the model.
124
     *
125
     * @return string
126
     */
127 10
    public function getSource() : string
128
    {
129 10
        return 'apps_plans';
130
    }
131
132
    /**
133
     * Just a preatty function that returns the same object for
134
     *
135
     * $app->settings()->set(key, value);
136
     * $app->settings()->get(key);
137
     * $app->settings()->get(key)->delete();
138
     *
139
     * @return AppsPlans
140
     */
141
    public function settings() : AppsPlans
142
    {
143
        return $this;
144
    }
145
146
    /**
147
     * Get the default plan for this given app
148
     *
149
     * @return AppsPlans
150
     */
151 2
    public static function getDefaultPlan() : AppsPlans
152
    {
153 2
        return AppsPlans::findFirst([
154 2
            'conditions' => 'apps_id = ?0 and is_default = 1',
155 2
            'bind' => [Di::getDefault()->getApp()->getId()]
156
        ]);
157
    }
158
159
    /**
160
     * Get the value of the settins by it key
161
     *
162
     * @param string $key
163
     * @param string $value
164
     */
165 4
    public function get(string $key) : ?string
166
    {
167 4
        $setting = AppsPlansSettings::findFirst([
168 4
            'conditions' => 'apps_plans_id = ?0 and apps_id = ?1 and key = ?2',
169 4
            'bind' => [$this->getId(), $this->apps_id, $key]
170
        ]);
171
172 4
        if (is_object($setting)) {
173 3
            return (string) $setting->value;
174
        }
175
176 1
        return null;
177
    }
178
179
    /**
180
     * Set a setting for the given app
181
     *
182
     * @param string $key
183
     * @param string $value
184
     */
185 3
    public function set(string $key, $value) : bool
186
    {
187 3
        $setting = AppsPlansSettings::findFirst([
188 3
            'conditions' => 'apps_plans_id = ?0 and apps_id = ?1 and key = ?2',
189 3
            'bind' => [$this->getId(), $this->apps_id, $key]
190
        ]);
191
192 3
        if (!is_object($setting)) {
193 1
            $setting = new AppsPlansSettings();
194 1
            $setting->apps_plans_id = $this->getId();
195 1
            $setting->apps_id = $this->getId();
196 1
            $setting->key = $key;
197
        }
198
199 3
        $setting->value = $value;
200
201 3
        if (!$setting->save()) {
202
            throw new ModelException((string)current($setting->getMessages()));
203
        }
204
205 3
        return true;
206
    }
207
208
    /**
209
     * After save
210
     *
211
     * @return void
212
     */
213
    public function afterSave()
214
    {
215
        //if we udpate the is_default for this plan we need to remove all others and update the main app
216
        if ($this->is_default) {
217
            $this->app->default_apps_plan_id = $this->getId();
218
            $this->app->update();
219
        }
220
    }
221
}
222