cashwarden /
api
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace app\core\models; |
||||
| 4 | |||||
| 5 | use app\core\exceptions\InvalidArgumentException; |
||||
| 6 | use app\core\services\RecurrenceService; |
||||
| 7 | use app\core\services\TransactionService; |
||||
| 8 | use app\core\types\RecurrenceFrequency; |
||||
| 9 | use app\core\types\RecurrenceStatus; |
||||
| 10 | use Yii; |
||||
| 11 | use yii\base\InvalidConfigException; |
||||
| 12 | use yii\behaviors\TimestampBehavior; |
||||
| 13 | use yiier\helpers\DateHelper; |
||||
| 14 | |||||
| 15 | /** |
||||
| 16 | * This is the model class for table "{{%recurrence}}". |
||||
| 17 | * |
||||
| 18 | * @property int $id |
||||
| 19 | * @property int $user_id |
||||
| 20 | * @property string $name |
||||
| 21 | * @property int $frequency |
||||
| 22 | * @property int|null $interval |
||||
| 23 | * @property string|null $schedule |
||||
| 24 | * @property int $transaction_id |
||||
| 25 | * @property string|null $started_at |
||||
| 26 | * @property string|null $execution_date |
||||
| 27 | * @property int|null $status |
||||
| 28 | * @property string|null $created_at |
||||
| 29 | * @property string|null $updated_at |
||||
| 30 | * |
||||
| 31 | * @property-read Transaction $transaction |
||||
| 32 | */ |
||||
| 33 | class Recurrence extends \yii\db\ActiveRecord |
||||
| 34 | { |
||||
| 35 | /** |
||||
| 36 | * {@inheritdoc} |
||||
| 37 | */ |
||||
| 38 | public static function tableName() |
||||
| 39 | { |
||||
| 40 | return '{{%recurrence}}'; |
||||
| 41 | } |
||||
| 42 | |||||
| 43 | /** |
||||
| 44 | * @inheritdoc |
||||
| 45 | * @throws InvalidConfigException |
||||
| 46 | */ |
||||
| 47 | public function behaviors() |
||||
| 48 | { |
||||
| 49 | return [ |
||||
| 50 | [ |
||||
| 51 | 'class' => TimestampBehavior::class, |
||||
| 52 | 'value' => Yii::$app->formatter->asDatetime('now') |
||||
| 53 | ], |
||||
| 54 | ]; |
||||
| 55 | } |
||||
| 56 | |||||
| 57 | /** |
||||
| 58 | * @return bool |
||||
| 59 | * @throws InvalidConfigException |
||||
| 60 | */ |
||||
| 61 | public function beforeValidate() |
||||
| 62 | { |
||||
| 63 | if (parent::beforeValidate()) { |
||||
| 64 | $this->execution_date = $this->execution_date ? |
||||
| 65 | Yii::$app->formatter->asDatetime($this->execution_date, 'php:Y-m-d') : null; |
||||
| 66 | return true; |
||||
| 67 | } |
||||
| 68 | return false; |
||||
| 69 | } |
||||
| 70 | |||||
| 71 | /** |
||||
| 72 | * {@inheritdoc} |
||||
| 73 | */ |
||||
| 74 | public function rules() |
||||
| 75 | { |
||||
| 76 | return [ |
||||
| 77 | [['name', 'frequency', 'transaction_id'], 'required'], |
||||
| 78 | [['user_id', 'interval', 'transaction_id'], 'integer'], |
||||
| 79 | ['frequency', 'in', 'range' => RecurrenceFrequency::names()], |
||||
| 80 | ['status', 'in', 'range' => RecurrenceStatus::names()], |
||||
| 81 | [['started_at', 'execution_date'], 'datetime', 'format' => 'php:Y-m-d'], |
||||
| 82 | [['name'], 'string', 'max' => 255], |
||||
| 83 | [ |
||||
| 84 | 'transaction_id', |
||||
| 85 | function ($attribute, $params, $validator) { |
||||
|
0 ignored issues
–
show
The parameter
$validator is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 86 | try { |
||||
| 87 | TransactionService::findCurrentOne($this->$attribute); |
||||
| 88 | } catch (\Exception $e) { |
||||
| 89 | $this->addError( |
||||
| 90 | $attribute, |
||||
| 91 | Yii::t('app', 'The {attribute} not found.', ['attribute' => $attribute]) |
||||
| 92 | ); |
||||
| 93 | return null; |
||||
| 94 | } |
||||
| 95 | } |
||||
| 96 | ], |
||||
| 97 | [ |
||||
| 98 | 'schedule', |
||||
| 99 | 'required', |
||||
| 100 | 'when' => function (self $model) { |
||||
| 101 | return in_array( |
||||
| 102 | RecurrenceFrequency::toEnumValue($model->frequency), |
||||
| 103 | [RecurrenceFrequency::WEEK, RecurrenceFrequency::MONTH, RecurrenceFrequency::YEAR] |
||||
| 104 | ); |
||||
| 105 | } |
||||
| 106 | ], |
||||
| 107 | [ |
||||
| 108 | 'schedule', |
||||
| 109 | 'integer', |
||||
| 110 | 'min' => 1, |
||||
| 111 | 'max' => 7, |
||||
| 112 | 'when' => function (self $model) { |
||||
| 113 | return RecurrenceFrequency::toEnumValue($model->frequency) === RecurrenceFrequency::WEEK; |
||||
| 114 | } |
||||
| 115 | ], |
||||
| 116 | [ |
||||
| 117 | 'schedule', |
||||
| 118 | 'datetime', |
||||
| 119 | 'format' => 'd', |
||||
| 120 | 'when' => function (self $model) { |
||||
| 121 | return RecurrenceFrequency::toEnumValue($model->frequency) === RecurrenceFrequency::MONTH; |
||||
| 122 | } |
||||
| 123 | ], |
||||
| 124 | [ |
||||
| 125 | 'schedule', |
||||
| 126 | 'datetime', |
||||
| 127 | 'format' => 'M-d', |
||||
| 128 | 'when' => function (self $model) { |
||||
| 129 | return RecurrenceFrequency::toEnumValue($model->frequency) === RecurrenceFrequency::YEAR; |
||||
| 130 | } |
||||
| 131 | ], |
||||
| 132 | ]; |
||||
| 133 | } |
||||
| 134 | |||||
| 135 | /** |
||||
| 136 | * {@inheritdoc} |
||||
| 137 | */ |
||||
| 138 | public function attributeLabels() |
||||
| 139 | { |
||||
| 140 | return [ |
||||
| 141 | 'id' => Yii::t('app', 'ID'), |
||||
| 142 | 'user_id' => Yii::t('app', 'User ID'), |
||||
| 143 | 'name' => Yii::t('app', 'Name'), |
||||
| 144 | 'frequency' => Yii::t('app', 'Frequency'), |
||||
| 145 | 'interval' => Yii::t('app', 'Interval'), |
||||
| 146 | 'schedule' => Yii::t('app', 'Schedule'), |
||||
| 147 | 'transaction_id' => Yii::t('app', 'Transaction ID'), |
||||
| 148 | 'started_at' => Yii::t('app', 'Started At'), |
||||
| 149 | 'execution_date' => Yii::t('app', 'Execution Date'), |
||||
| 150 | 'status' => Yii::t('app', 'Status'), |
||||
| 151 | 'created_at' => Yii::t('app', 'Created At'), |
||||
| 152 | 'updated_at' => Yii::t('app', 'Updated At'), |
||||
| 153 | ]; |
||||
| 154 | } |
||||
| 155 | |||||
| 156 | /** |
||||
| 157 | * @param bool $insert |
||||
| 158 | * @return bool |
||||
| 159 | * @throws InvalidArgumentException|InvalidConfigException |
||||
| 160 | */ |
||||
| 161 | public function beforeSave($insert) |
||||
| 162 | { |
||||
| 163 | if (parent::beforeSave($insert)) { |
||||
| 164 | if ($insert) { |
||||
| 165 | $this->user_id = Yii::$app->user->id; |
||||
|
0 ignored issues
–
show
It seems like
Yii::app->user->id can also be of type string. However, the property $user_id is declared as type integer. Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||||
| 166 | } |
||||
| 167 | $this->started_at = Yii::$app->formatter->asDatetime($this->started_at ?: 'now', 'php:Y-m-d'); |
||||
| 168 | $this->frequency = RecurrenceFrequency::toEnumValue($this->frequency); |
||||
| 169 | $this->status = is_null($this->status) ? |
||||
| 170 | RecurrenceStatus::ACTIVE : RecurrenceStatus::toEnumValue($this->status); |
||||
| 171 | $this->execution_date = RecurrenceService::getExecutionDate($this); |
||||
| 172 | return true; |
||||
| 173 | } else { |
||||
| 174 | return false; |
||||
| 175 | } |
||||
| 176 | } |
||||
| 177 | |||||
| 178 | public function getTransaction() |
||||
| 179 | { |
||||
| 180 | return $this->hasOne(Transaction::class, ['id' => 'transaction_id']); |
||||
| 181 | } |
||||
| 182 | |||||
| 183 | |||||
| 184 | /** |
||||
| 185 | * @return array |
||||
| 186 | */ |
||||
| 187 | public function fields() |
||||
| 188 | { |
||||
| 189 | $fields = parent::fields(); |
||||
| 190 | unset($fields['user_id']); |
||||
| 191 | |||||
| 192 | $fields['status'] = function (self $model) { |
||||
| 193 | return RecurrenceStatus::getName($model->status); |
||||
|
0 ignored issues
–
show
It seems like
$model->status can also be of type null; however, parameter $v of app\core\types\BaseType::getName() does only seem to accept integer, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 194 | }; |
||||
| 195 | |||||
| 196 | $fields['frequency'] = function (self $model) { |
||||
| 197 | return RecurrenceFrequency::getName($model->frequency); |
||||
| 198 | }; |
||||
| 199 | |||||
| 200 | $fields['frequency_text'] = function (self $model) { |
||||
| 201 | return data_get(RecurrenceFrequency::texts(), $model->frequency); |
||||
| 202 | }; |
||||
| 203 | |||||
| 204 | $fields['started_at'] = function (self $model) { |
||||
| 205 | return DateHelper::datetimeToIso8601($model->started_at); |
||||
|
0 ignored issues
–
show
It seems like
$model->started_at can also be of type null; however, parameter $dateStr of yiier\helpers\DateHelper::datetimeToIso8601() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 206 | }; |
||||
| 207 | |||||
| 208 | $fields['execution_date'] = function (self $model) { |
||||
| 209 | return DateHelper::datetimeToIso8601($model->execution_date); |
||||
|
0 ignored issues
–
show
It seems like
$model->execution_date can also be of type null; however, parameter $dateStr of yiier\helpers\DateHelper::datetimeToIso8601() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 210 | }; |
||||
| 211 | |||||
| 212 | $fields['created_at'] = function (self $model) { |
||||
| 213 | return DateHelper::datetimeToIso8601($model->created_at); |
||||
|
0 ignored issues
–
show
It seems like
$model->created_at can also be of type null; however, parameter $dateStr of yiier\helpers\DateHelper::datetimeToIso8601() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 214 | }; |
||||
| 215 | |||||
| 216 | $fields['updated_at'] = function (self $model) { |
||||
| 217 | return DateHelper::datetimeToIso8601($model->updated_at); |
||||
|
0 ignored issues
–
show
It seems like
$model->updated_at can also be of type null; however, parameter $dateStr of yiier\helpers\DateHelper::datetimeToIso8601() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 218 | }; |
||||
| 219 | |||||
| 220 | return $fields; |
||||
| 221 | } |
||||
| 222 | } |
||||
| 223 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.