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