領収証発行サービス
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

158 linhas
4.0KB

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