領収証発行サービス
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.

147 lines
3.5KB

  1. <?php
  2. namespace App\Logic\User;
  3. use App\Codes\UserRole;
  4. use App\Models\Contract;
  5. use App\Models\User;
  6. use Illuminate\Support\Arr;
  7. use Illuminate\Support\Carbon;
  8. use Illuminate\Support\Facades\Hash;
  9. use Illuminate\Support\Facades\Validator;
  10. use LogicException;
  11. class LoginUserManager
  12. {
  13. private bool $initialized = false;
  14. private User $user;
  15. private Contract $contract;
  16. public function __construct()
  17. {
  18. }
  19. public function initForCreate(string|Contract $contractId)
  20. {
  21. $this->setContract($contractId);
  22. $this->setUser(null);
  23. $this->initialized = true;
  24. return $this;
  25. }
  26. public function initForModify(string|Contract $contractId, string|User $userId)
  27. {
  28. $this->setContract($contractId);
  29. $this->setUser($userId);
  30. $this->initialized = true;
  31. return $this;
  32. }
  33. public function getTimestamp(): Carbon
  34. {
  35. if (!$this->initialized) {
  36. throw new LogicException("初期化不正");
  37. }
  38. return $this->user->updated_at < $this->contract->updated_at ? $this->contract->updated_at : $this->user->updated_at;
  39. }
  40. public function fill(array $attr)
  41. {
  42. if (!$this->initialized) {
  43. throw new LogicException("初期化不正");
  44. }
  45. $this->user->fill($attr);
  46. return $this;
  47. }
  48. public function create(): array
  49. {
  50. $messages = $this->checkParam();
  51. if (count($messages) !== 0) {
  52. return $messages;
  53. }
  54. $this->user->save();
  55. return [];
  56. }
  57. public function update(): array
  58. {
  59. $messages = $this->checkParam();
  60. if (count($messages) !== 0) {
  61. return $messages;
  62. }
  63. $this->user->save();
  64. return [];
  65. }
  66. private function setContract(string|Contract $contractId)
  67. {
  68. if ($contractId instanceof Contract) {
  69. $this->contract = $contractId;
  70. $this->initialized = true;
  71. return;
  72. }
  73. $this->contract = Contract::findOrFail($contractId);
  74. $this->initialized = true;
  75. return;
  76. }
  77. private function setUser(string|User|null $userId)
  78. {
  79. if ($userId instanceof User) {
  80. $this->user = $userId;
  81. return;
  82. } else if (is_string($userId)) {
  83. $this->user = User::findOrFail($userId);
  84. return;
  85. }
  86. $this->user = new User();
  87. $this->user->setContract($this->contract);
  88. $this->user->role = UserRole::NORMAL_ADMIN;
  89. }
  90. private function checkParam()
  91. {
  92. $validator = Validator::make($this->user->toArray(), []);
  93. if ($validator->failed()) {
  94. throw new LogicException("バリデートエラー");
  95. }
  96. $messages = [];
  97. $this->checkEmailUnique($messages);
  98. $this->passwordEncrypto($messages);
  99. return $messages;
  100. }
  101. private function passwordEncrypto(array &$messages)
  102. {
  103. if ($this->user->isDirty(User::COL_NAME_PASSWORD)) {
  104. $this->user->password = Hash::make($this->user->password);
  105. }
  106. }
  107. private function checkEmailUnique(array &$messages)
  108. {
  109. if ($this->user->isDirty(User::COL_NAME_EMAIL)) {
  110. $exists = User::whereEmail($this->user->email)
  111. ->where(User::COL_NAME_ID, '<>', $this->user->id)
  112. ->exists();
  113. if ($exists) {
  114. $messages[User::COL_NAME_EMAIL] = trans('validation.unique');
  115. }
  116. }
  117. }
  118. }