You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.1KB

  1. <?php
  2. namespace App\Util;
  3. use App\Features\InstanceAble;
  4. use Illuminate\Support\Facades\DB;
  5. use LogicException;
  6. class DBUtil
  7. {
  8. use InstanceAble;
  9. private bool $isBeginning = false;
  10. public function __destruct()
  11. {
  12. if ($this->isBeginning) {
  13. $this->rollBack();
  14. }
  15. }
  16. public function beginTransaction(): void
  17. {
  18. if ($this->isBeginning) {
  19. throw new LogicException("2重トランザクション開始検知");
  20. }
  21. DB::beginTransaction();
  22. $this->isBeginning = true;
  23. }
  24. public function commit(): void
  25. {
  26. if (!$this->isBeginning) {
  27. throw new LogicException("無効なコミット検知");
  28. }
  29. DB::commit();
  30. $this->isBeginning = false;
  31. }
  32. public function rollBack(): void
  33. {
  34. if (!$this->isBeginning) {
  35. throw new LogicException("無効なロールバック検知");
  36. }
  37. DB::rollBack();
  38. logs()->warning("ロールバック検知");
  39. $this->isBeginning = false;
  40. }
  41. public function isBeginning(): bool
  42. {
  43. return $this->isBeginning;
  44. }
  45. }