|
| 1 | +<?php |
| 2 | +namespace Pion\Support\Eloquent\Position; |
| 3 | + |
| 4 | +use Pion\Support\Eloquent\Position\Query\AbstractPositionQuery; |
| 5 | +use Pion\Support\Eloquent\Position\Query\LastPositionQuery; |
| 6 | +use Pion\Support\Eloquent\Position\Query\PositionQuery; |
| 7 | +use Pion\Support\Eloquent\Position\Traits\PositionTrait; |
| 8 | +use Illuminate\Database\Eloquent\Model; |
| 9 | +use Illuminate\Contracts\Events\Dispatcher; |
| 10 | + |
| 11 | +/** |
| 12 | + * Traits PositionObserver |
| 13 | + * |
| 14 | + * Listens for saved state and checks if the position should be update in |
| 15 | + * related entries. |
| 16 | + * |
| 17 | + * @package App\Models\Traits |
| 18 | + */ |
| 19 | +class PositionObserver |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var Dispatcher |
| 23 | + */ |
| 24 | + private $events; |
| 25 | + |
| 26 | + /** |
| 27 | + * PositionObserver constructor. |
| 28 | + * |
| 29 | + * @param Dispatcher $events |
| 30 | + */ |
| 31 | + public function __construct(Dispatcher $events) |
| 32 | + { |
| 33 | + $this->events = $events; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Updates the position before saving |
| 38 | + * |
| 39 | + * @param Model|PositionTrait $model |
| 40 | + */ |
| 41 | + public function saving($model) |
| 42 | + { |
| 43 | + if (!$model->isPositionUpdateDisabled()) { |
| 44 | + // Get the position for current and old value |
| 45 | + $position = $model->getPosition(); |
| 46 | + |
| 47 | + // Get the old position |
| 48 | + $oldPosition = $model->getOriginal($model->getPositionColumn()); |
| 49 | + |
| 50 | + // Check if the position is set |
| 51 | + if (is_null($position)) { |
| 52 | + $this->appendLast($model, $oldPosition); |
| 53 | + } else { |
| 54 | + $this->move($model, $position, $oldPosition); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Setups the position to be at the end |
| 61 | + * |
| 62 | + * @param Model|PositionTrait $model |
| 63 | + * @param int $oldPosition |
| 64 | + */ |
| 65 | + protected function appendLast($model, $oldPosition) |
| 66 | + { |
| 67 | + // Build the last position query |
| 68 | + $query = new LastPositionQuery($model, $oldPosition); |
| 69 | + |
| 70 | + // Run the query |
| 71 | + $this->runQuery($query, $oldPosition); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Moves other entries from the given position |
| 76 | + * |
| 77 | + * @param Model|PositionTrait $model |
| 78 | + * @param int $position |
| 79 | + * @param int $oldPosition |
| 80 | + */ |
| 81 | + protected function move($model, $position, $oldPosition) |
| 82 | + { |
| 83 | + // Check if the position has change and we need to recalculate |
| 84 | + if ($oldPosition != $position) { |
| 85 | + // Build the position query |
| 86 | + $query = new PositionQuery($model, $position, $oldPosition); |
| 87 | + |
| 88 | + // Run the position query |
| 89 | + $this->runQuery($query, $oldPosition); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Runs the position events and query if can. If positioning event returns |
| 95 | + * false, it will revert the new position to old position. |
| 96 | + * |
| 97 | + * @param AbstractPositionQuery $query |
| 98 | + * @param int $oldPosition |
| 99 | + */ |
| 100 | + protected function runQuery(AbstractPositionQuery $query, $oldPosition) |
| 101 | + { |
| 102 | + // Fire the validation event |
| 103 | + $eventResponse = $this->firePositioningEvent($query); |
| 104 | + |
| 105 | + // Ignore updating the position and revert the position to original value |
| 106 | + if ($eventResponse !== $eventResponse && $eventResponse === false) { |
| 107 | + // Update the new position to original position |
| 108 | + $query->model()->setPosition($oldPosition); |
| 109 | + } else { |
| 110 | + // Run the query to update other entries to fix the position order |
| 111 | + $query->run(); |
| 112 | + |
| 113 | + // Fire the final state |
| 114 | + $this->firePositionedEvent($query->model()); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Fire the name-spaced validating event. |
| 120 | + * |
| 121 | + * @param AbstractPositionQuery $query |
| 122 | + * |
| 123 | + * @return mixed |
| 124 | + */ |
| 125 | + protected function firePositioningEvent($query) |
| 126 | + { |
| 127 | + $model = $query->model(); |
| 128 | + return $this->events->until('eloquent.positioning: '.get_class($model), [$model, $query]); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Fire the name-spaced post-validation event. |
| 133 | + * |
| 134 | + * @param Model $model |
| 135 | + * |
| 136 | + * @return void |
| 137 | + */ |
| 138 | + protected function firePositionedEvent(Model $model) |
| 139 | + { |
| 140 | + $this->events->fire('eloquent.positioned: '.get_class($model), [$model]); |
| 141 | + } |
| 142 | +} |
0 commit comments