Test Failed
Pull Request — master (#21)
by Maximo
04:17
created

AppsPlans::set()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.004

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 4
nop 2
dl 0
loc 21
ccs 12
cts 13
cp 0.9231
crap 3.004
rs 9.8666
c 0
b 0
f 0
1
<?php
2
declare (strict_types = 1);
3
4
namespace Gewaer\Models;
5
6
use Gewaer\Exception\ServerErrorHttpException;
7
use Gewaer\Exception\ModelException;
8
use Phalcon\Di;
9
10
class AppsPlans extends AbstractModel
11
{
12
    /**
13
     *
14
     * @var integer
15
     */
16
    public $id;
17
18
    /**
19
     *
20
     * @var integer
21
     */
22
    public $apps_id;
23
24
    /**
25
     *
26
     * @var string
27
     */
28
    public $name;
29
30
    /**
31
     *
32
     * @var string
33
     */
34
    public $description;
35
36
    /**
37
     *
38
     * @var string
39
     */
40
    public $stripe_id;
41
42
    /**
43
     *
44
     * @var string
45
     */
46
    public $stripe_plan;
47
48
    /**
49
     *
50
     * @var double
51
     */
52
    public $pricing;
53
54
    /**
55
     *
56
     * @var integer
57
     */
58
    public $currency_id;
59
60
    /**
61
     *
62
     * @var integer
63
     */
64
    public $free_trial_dates;
65
66
    /**
67
     *
68
     * @var integer
69
     */
70
    public $is_default;
71
72
    /**
73
     *
74
     * @var string
75
     */
76
    public $created_at;
77
78
    /**
79
     *
80
     * @var string
81
     */
82
    public $updated_at;
83
84
    /**
85
     *
86
     * @var integer
87
     */
88
    public $is_deleted;
89
90
    /**
91
     * Initialize method for model.
92
     */
93 8
    public function initialize()
94
    {
95 8
        $this->setSource('apps_plans');
96
97 8
        $this->belongsTo(
98 8
            'apps_id',
99 8
            'Gewaer\Models\Apps',
100 8
            'id',
101 8
            ['alias' => 'app']
102
        );
103
104 8
        $this->hasMany(
105 8
            'apps_id',
106 8
            'Gewaer\Models\AppsPlansSettings',
107 8
            'apps_id',
108 8
            ['alias' => 'settings']
109
        );
110 8
    }
111
112
    /**
113
     * Returns table name mapped in the model.
114
     *
115
     * @return string
116
     */
117 7
    public function getSource() : string
118
    {
119 7
        return 'apps_plans';
120
    }
121
122
    /**
123
     * Just a preatty function that returns the same object for
124
     *
125
     * $app->settings()->set(key, value);
126
     * $app->settings()->get(key);
127
     * $app->settings()->get(key)->delete();
128
     *
129
     * @return AppsPlans
130
     */
131
    public function settings() : AppsPlans
132
    {
133
        return $this;
134
    }
135
136
    /**
137
     * Get the default plan for this given app
138
     *
139
     * @return AppsPlans
140
     */
141 2
    public static function getDefaultPlan() : AppsPlans
142
    {
143 2
        return AppsPlans::findFirst([
144 2
            'conditions' => 'apps_id = ?0 and is_default = 1',
145 2
            'bind' => [Di::getDefault()->getApp()->getId()]
146
        ]);
147
    }
148
149
    /**
150
     * Get the value of the settins by it key
151
     *
152
     * @param string $key
153
     * @param string $value
154
     */
155
    public function get(string $key) : string
156
    {
157
        $setting = AppsPlansSettings::findFirst([
158
            'conditions' => 'apps_plans_id = ?0 and apps_id = ?1 and key = ?2',
159
            'bind' => [$this->getId(), $this->apps_id, $key]
160
        ]);
161
162
        if (is_object($setting)) {
163
            return $setting->value;
164
        }
165
166
        throw new ServerErrorHttpException(_('No settings found with for ' . $key . ' at this app ' . $this->apps_id));
167
    }
168
169
    /**
170
     * Set a setting for the given app
171
     *
172
     * @param string $key
173
     * @param string $value
174
     */
175 2
    public function set(string $key, $value) : bool
176
    {
177 2
        $setting = AppsPlansSettings::findFirst([
178 2
            'conditions' => 'apps_plans_id = ?0 and apps_id = ?1 and key = ?2',
179 2
            'bind' => [$this->getId(), $this->apps_id, $key]
180
        ]);
181
182 2
        if (!is_object($setting)) {
183 1
            $setting = new AppsPlansSettings();
184 1
            $setting->apps_plans_id = $this->getId();
185 1
            $setting->apps_id = $this->getId();
186 1
            $setting->key = $key;
187
        }
188
189 2
        $setting->value = $value;
190
191 2
        if (!$setting->save()) {
192
            throw new ModelException((string)current($setting->getMessages()));
193
        }
194
195 2
        return true;
196
    }
197
}
198