Passed
Push — master ( 682802...46670a )
by Maximo
05:00 queued 10s
created

AppsPlans   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Test Coverage

Coverage 43.75%

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 171
ccs 14
cts 32
cp 0.4375
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

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