Conditions | 17 |
Paths | 21 |
Total Lines | 64 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
66 | public function notify(string $type, ModelInterface $model) |
||
67 | { |
||
68 | if (!$this->isEnabled()) { |
||
69 | return; |
||
70 | } |
||
71 | |||
72 | assert($model instanceof Model); |
||
73 | |||
74 | $positionField = $this->getField(); |
||
75 | $rawSql = $this->getRawSql(); |
||
76 | |||
77 | switch ($type) { |
||
78 | case 'beforeValidation': |
||
79 | // if position field is empty, force current max(position)+1 |
||
80 | $lastPosition = $model::findFirst(['order' => $positionField . ' DESC']); |
||
81 | if ($lastPosition && assert($lastPosition instanceof $model)) { |
||
82 | $position = (int)$lastPosition->getAttribute($positionField); |
||
|
|||
83 | $model->setAttribute($positionField, $position + 1); |
||
84 | } |
||
85 | break; |
||
86 | |||
87 | case 'afterSave': |
||
88 | if (!$this->getProgress() && $model->hasSnapshotData() && $model->hasUpdated($positionField)) { |
||
89 | $this->setProgress(true); |
||
90 | |||
91 | $snapshot = $model->getOldSnapshotData() ?: $model->getSnapshotData(); |
||
92 | $modelPosition = $model->getAttribute($positionField); |
||
93 | $modelPrimaryKeys = $model->getPrimaryKeysValues(); |
||
94 | |||
95 | if (!($modelPosition instanceof RawValue)) { |
||
96 | $uField = Text::uncamelize($positionField); // @todo use columnMap |
||
97 | $updatePositionQuery = null; |
||
98 | |||
99 | if ($snapshot[$positionField] > $modelPosition) { |
||
100 | $updatePositionQuery = $rawSql |
||
101 | ? 'UPDATE `' . $model->getSource() . '` SET `' . $uField . '` = `' . $uField . '`+1 WHERE `' . $uField . '` >= :position and `' . $uField . '` < :oldPosition and `' . $idField . '` <> :id' |
||
102 | : 'UPDATE [' . get_class($model) . '] SET [' . $positionField . '] = [' . $positionField . ']+1 WHERE [' . $positionField . '] >= ?1 and [' . $positionField . '] < ?2 and [' . $idField . '] <> ?0'; |
||
103 | } |
||
104 | elseif ($snapshot[$positionField] < $modelPosition) { |
||
105 | $updatePositionQuery = $rawSql |
||
106 | ? 'UPDATE `' . $model->getSource() . '` SET `' . $uField . '` = `' . $uField . '`-1 WHERE `' . $uField . '` > :oldPosition and `' . $uField . '` <= :position and `' . $idField . '` <> :id' |
||
107 | : 'UPDATE [' . get_class($model) . '] SET [' . $positionField . '] = [' . $positionField . ']-1 WHERE [' . $positionField . '] > ?2 and [' . $positionField . '] <= ?1 and [' . $idField . '] <> ?0'; |
||
108 | } |
||
109 | |||
110 | if (!empty($updatePositionQuery)) { |
||
111 | if ($rawSql) { |
||
112 | $model->getWriteConnection()->query($updatePositionQuery, [ |
||
113 | 'primaryKeys' => $modelPrimaryKeys, |
||
114 | 'position' => $modelPosition, |
||
115 | 'oldPosition' => $snapshot[$positionField], |
||
116 | ]); |
||
117 | } |
||
118 | else { |
||
119 | $model->getModelsManager()->executeQuery($updatePositionQuery, [$modelId, $modelPosition, $snapshot[$positionField]]); |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | |||
124 | $this->setProgress(false); |
||
125 | } |
||
126 | break; |
||
127 | } |
||
128 | |||
129 | return true; |
||
130 | } |
||
132 |