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 | 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 | 9 | public function initialize() |
|
| 94 | { |
||
| 95 | 9 | $this->setSource('apps_plans'); |
|
| 96 | |||
| 97 | 9 | $this->belongsTo( |
|
| 98 | 9 | 'apps_id', |
|
| 99 | 9 | 'Gewaer\Models\Apps', |
|
| 100 | 9 | 'id', |
|
| 101 | 9 | ['alias' => 'app'] |
|
| 102 | ); |
||
| 103 | |||
| 104 | 9 | $this->hasMany( |
|
| 105 | 9 | 'apps_id', |
|
| 106 | 9 | 'Gewaer\Models\AppsPlansSettings', |
|
| 107 | 9 | 'apps_id', |
|
| 108 | 9 | ['alias' => 'settings'] |
|
| 109 | ); |
||
| 110 | 9 | } |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Returns table name mapped in the model. |
||
| 114 | * |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | 8 | public function getSource() : string |
|
| 118 | { |
||
| 119 | 8 | 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; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 164 | } |
||
| 165 | |||
| 166 | return null; |
||
| 167 | //throw new ServerErrorHttpException(_('No settings found with for ' . $key . ' at this app ' . $this->apps_id)); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Set a setting for the given app |
||
| 172 | * |
||
| 173 | * @param string $key |
||
| 174 | * @param string $value |
||
| 175 | */ |
||
| 176 | 3 | public function set(string $key, $value) : bool |
|
| 177 | { |
||
| 178 | 3 | $setting = AppsPlansSettings::findFirst([ |
|
| 179 | 3 | 'conditions' => 'apps_plans_id = ?0 and apps_id = ?1 and key = ?2', |
|
| 180 | 3 | 'bind' => [$this->getId(), $this->apps_id, $key] |
|
| 181 | ]); |
||
| 182 | |||
| 183 | 3 | if (!is_object($setting)) { |
|
| 184 | 1 | $setting = new AppsPlansSettings(); |
|
| 185 | 1 | $setting->apps_plans_id = $this->getId(); |
|
| 186 | 1 | $setting->apps_id = $this->getId(); |
|
| 187 | 1 | $setting->key = $key; |
|
| 188 | } |
||
| 189 | |||
| 190 | 3 | $setting->value = $value; |
|
| 191 | |||
| 192 | 3 | if (!$setting->save()) { |
|
| 193 | throw new ModelException((string)current($setting->getMessages())); |
||
| 194 | } |
||
| 195 | |||
| 196 | 3 | return true; |
|
| 197 | } |
||
| 198 | } |
||
| 199 |