1 | <?php |
||||
2 | namespace andmemasin\myabstract\traits; |
||||
3 | |||||
4 | use andmemasin\myabstract\HasStatusModel; |
||||
5 | use andmemasin\myabstract\StatusModel; |
||||
6 | use yii\base\ErrorException; |
||||
7 | use yii\base\UserException; |
||||
8 | use yii\db\Query; |
||||
9 | |||||
10 | /** |
||||
11 | * Trait ModelWithHasStatusTrait |
||||
12 | * @package andmemasin\myabstract |
||||
13 | * @author Tonis Ormisson <[email protected]> |
||||
14 | * |
||||
15 | * @property HasStatusModel[] $hasStatuses |
||||
16 | * @property StatusModel $currentStatus |
||||
17 | * @property HasStatusModel $hasStatus |
||||
18 | */ |
||||
19 | trait ModelWithHasStatusTrait |
||||
20 | { |
||||
21 | |||||
22 | /** |
||||
23 | * @return boolean |
||||
24 | */ |
||||
25 | public function isActive() { |
||||
26 | /** @var StatusModel $statusModel */ |
||||
27 | $statusModel = (new static)->statusModelClass; |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
28 | return (new $statusModel)->isActive($this->currentStatus->id); |
||||
29 | } |
||||
30 | |||||
31 | public function addStatus($status) { |
||||
32 | /** @var HasStatusModel $hasStatus */ |
||||
33 | $hasStatus = new static::$hasStatusClassName; |
||||
0 ignored issues
–
show
|
|||||
34 | $hasStatus->status = $status; |
||||
35 | $hasStatus->{$hasStatus->parentIdColumn} = static::getPrimaryKey(); |
||||
36 | |||||
37 | if (!$hasStatus->save()) { |
||||
38 | throw new UserException(serialize($hasStatus->errors)); |
||||
39 | } |
||||
40 | } |
||||
41 | |||||
42 | /** {@inheritdoc} */ |
||||
43 | public function afterSave($insert, $changedAttributes) |
||||
44 | { |
||||
45 | |||||
46 | if ($insert) { |
||||
47 | // add a created status im the status history if some other status is assigned |
||||
48 | if ($this->status != $this->initialStatus) { |
||||
49 | $this->addStatus($this->initialStatus); |
||||
50 | } |
||||
51 | $this->addStatus($this->status); |
||||
52 | } else { |
||||
53 | if (isset($changedAttributes['status'])) { |
||||
54 | $this->addStatus($this->status); |
||||
55 | } |
||||
56 | } |
||||
57 | parent::afterSave($insert, $changedAttributes); |
||||
58 | } |
||||
59 | |||||
60 | |||||
61 | /** |
||||
62 | * @return \yii\db\ActiveQuery |
||||
63 | */ |
||||
64 | public function getHasStatuses() |
||||
65 | { |
||||
66 | /** @var HasStatusModel $hasStatus */ |
||||
67 | $hasStatus = new static::$hasStatusClassName; |
||||
0 ignored issues
–
show
|
|||||
68 | return $this->hasMany(static::$hasStatusClassName, [$hasStatus->parentIdColumn => $hasStatus->parentIdColumn]); |
||||
0 ignored issues
–
show
It seems like
hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
69 | } |
||||
70 | |||||
71 | /** |
||||
72 | * @return HasStatusModel |
||||
73 | */ |
||||
74 | public function getHasStatus() |
||||
75 | { |
||||
76 | /** @var HasStatusModel $hasStatusModel */ |
||||
77 | $hasStatusModel = new static::$hasStatusClassName; |
||||
0 ignored issues
–
show
|
|||||
78 | $query = $this->getHasStatuses(); |
||||
79 | $query->orderBy([$hasStatusModel->primaryKeySingle()=>SORT_DESC]); |
||||
80 | /** @var HasStatusModel $model */ |
||||
81 | $model = $query->limit(1)->one(); |
||||
82 | return $model; |
||||
83 | } |
||||
84 | |||||
85 | /** |
||||
86 | * @return StatusModel |
||||
87 | */ |
||||
88 | public function getCurrentStatus() |
||||
89 | { |
||||
90 | /** @var StatusModel $class */ |
||||
91 | $class = (new static)->statusModelClass; |
||||
0 ignored issues
–
show
|
|||||
92 | return $class::getById($this->status); |
||||
93 | } |
||||
94 | |||||
95 | |||||
96 | /** |
||||
97 | * @param string $status |
||||
98 | * @param integer[] $model_ids |
||||
99 | * @throws ErrorException |
||||
100 | */ |
||||
101 | public static function bulkSetStatus($status, $model_ids) { |
||||
102 | $query = new Query(); |
||||
103 | /** @var StatusModel $class */ |
||||
104 | $class = (new static)->statusModelClass; |
||||
0 ignored issues
–
show
|
|||||
105 | |||||
106 | if (!$class::isStatus($status)) { |
||||
107 | throw new ErrorException('Invalid Status'); |
||||
108 | } |
||||
109 | $query->createCommand() |
||||
110 | ->update(static::tableName(), ['status'=>$status], ['in', (new static)->primaryKeySingle(), $model_ids]) |
||||
0 ignored issues
–
show
It seems like
primaryKeySingle() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
111 | ->execute(); |
||||
112 | } |
||||
113 | |||||
114 | /** |
||||
115 | * Find the Latest one HasStatus model by status |
||||
116 | * @param $status |
||||
117 | * @return StatusModel |
||||
118 | */ |
||||
119 | public function findStatus($status) { |
||||
120 | /** @var HasStatusModel $hasStatusModel */ |
||||
121 | $hasStatusModel = new static::$hasStatusClassName; |
||||
0 ignored issues
–
show
|
|||||
122 | $query = $this->getHasStatuses() |
||||
123 | ->andWhere(['status' => $status]); |
||||
124 | // latest first |
||||
125 | $query->orderBy([$hasStatusModel->primaryKeySingle() => SORT_DESC]); |
||||
126 | |||||
127 | /** @var StatusModel $model */ |
||||
128 | $model = $query->limit(1)->one(); |
||||
129 | return $model; |
||||
130 | } |
||||
131 | |||||
132 | } |