| Conditions | 3 |
| Paths | 3 |
| Total Lines | 27 |
| Code Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 15 | public function __invoke($action, $moved, $displaced) |
||
| 16 | { |
||
| 17 | $oldPosition = $moved->{$this->orderColumnName}; |
||
| 18 | $newPosition = $displaced->{$this->orderColumnName}; |
||
| 19 | |||
| 20 | if ($oldPosition === $newPosition) { |
||
| 21 | return; |
||
| 22 | } |
||
| 23 | |||
| 24 | $positionCalculator = new PositionCalculator; |
||
| 25 | $movedAfter = $action === 'moveAfter'; |
||
| 26 | $movingForward = $oldPosition < $newPosition; |
||
| 27 | $method = $movingForward ? 'decrement' : 'increment'; |
||
| 28 | |||
| 29 | |||
| 30 | $moved->buildSortQuery() |
||
| 31 | ->where($this->orderColumnName, '>', min([$oldPosition, $newPosition])) |
||
| 32 | ->where($this->orderColumnName, '<', max([$oldPosition, $newPosition])) |
||
| 33 | ->$method($this->orderColumnName); |
||
| 34 | |||
| 35 | |||
| 36 | $moved->{$this->orderColumnName} = $positionCalculator($movedAfter, $movingForward, $newPosition); |
||
| 37 | $displaced->{$this->orderColumnName} = $positionCalculator(!$movedAfter, $movingForward, $newPosition); |
||
| 38 | |||
| 39 | $moved->save(); |
||
| 40 | $displaced->save(); |
||
| 41 | } |
||
| 42 | } |
||
| 43 |