RuleGroupObserver.php 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. namespace App\Observers;
  3. use App\Jobs\VNet\reloadNode;
  4. use App\Models\Node;
  5. use App\Models\RuleGroup;
  6. use Arr;
  7. class RuleGroupObserver
  8. {
  9. public function updated(RuleGroup $ruleGroup): void
  10. {
  11. $changes = $ruleGroup->getChanges();
  12. if ($ruleGroup->nodes && Arr::hasAny($changes, ['type', 'rules'])) {
  13. $nodes = Node::whereType(4)->whereIn('id', $ruleGroup->nodes)->get();
  14. if ($nodes) {
  15. reloadNode::dispatchNow($nodes);
  16. }
  17. } elseif ($ruleGroup->rules && Arr::exists($changes, 'nodes')) {
  18. $arrayDiff = array_merge(array_diff($ruleGroup->nodes ?? [], $ruleGroup->getOriginal('nodes') ?? []),
  19. array_diff($ruleGroup->getOriginal('nodes') ?? [], $ruleGroup->nodes ?? []));
  20. if ($arrayDiff) {
  21. $nodes = Node::whereType(4)->whereIn('id', $arrayDiff)->get();
  22. if ($nodes) {
  23. reloadNode::dispatchNow($nodes);
  24. }
  25. }
  26. }
  27. }
  28. }