|
- <?php
-
- namespace App\Logic\User;
-
- use App\Codes\UserRole;
- use App\Models\Contract;
- use App\Models\User;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\Hash;
- use Illuminate\Support\Facades\Validator;
- use LogicException;
-
- class LoginUserManager
- {
-
- private bool $initialized = false;
-
- private User $user;
- private Contract $contract;
-
- public function __construct()
- {
- }
-
- public function initForCreate(string|Contract $contractId)
- {
- $this->setContract($contractId);
- $this->setUser(null);
- $this->initialized = true;
- return $this;
- }
-
- public function initForModify(string|Contract $contractId, string|User $userId)
- {
- $this->setContract($contractId);
- $this->setUser($userId);
- $this->initialized = true;
- return $this;
- }
-
- public function getTimestamp(): Carbon
- {
- if (!$this->initialized) {
- throw new LogicException("初期化不正");
- }
- return $this->user->updated_at < $this->contract->updated_at ? $this->contract->updated_at : $this->user->updated_at;
- }
-
- public function fill(array $attr)
- {
- if (!$this->initialized) {
- throw new LogicException("初期化不正");
- }
- $this->user->fill($attr);
- return $this;
- }
-
- public function create(): array
- {
- $messages = $this->checkParam();
-
- if (count($messages) !== 0) {
- return $messages;
- }
-
- $this->user->save();
- return [];
- }
- public function update(): array
- {
- $messages = $this->checkParam();
-
- if (count($messages) !== 0) {
- return $messages;
- }
-
- $this->user->save();
- return [];
- }
-
- private function setContract(string|Contract $contractId)
- {
- if ($contractId instanceof Contract) {
- $this->contract = $contractId;
- $this->initialized = true;
- return;
- }
-
- $this->contract = Contract::findOrFail($contractId);
- $this->initialized = true;
- return;
- }
-
- private function setUser(string|User|null $userId)
- {
- if ($userId instanceof User) {
- $this->user = $userId;
- return;
- } else if (is_string($userId)) {
- $this->user = User::findOrFail($userId);
- return;
- }
-
- $this->user = new User();
- $this->user->setContract($this->contract);
- $this->user->role = UserRole::NORMAL_ADMIN;
- }
-
- private function checkParam()
- {
- $validator = Validator::make($this->user->toArray(), []);
-
- if ($validator->failed()) {
- throw new LogicException("バリデートエラー");
- }
-
- $messages = [];
-
- $this->checkEmailUnique($messages);
- $this->passwordEncrypto($messages);
-
- return $messages;
- }
-
- private function passwordEncrypto(array &$messages)
- {
- if ($this->user->isDirty(User::COL_NAME_PASSWORD)) {
- $this->user->password = Hash::make($this->user->password);
- }
- }
-
- private function checkEmailUnique(array &$messages)
- {
- if ($this->user->isDirty(User::COL_NAME_EMAIL)) {
-
- $exists = User::whereEmail($this->user->email)
- ->where(User::COL_NAME_ID, '<>', $this->user->id)
- ->exists();
-
- if ($exists) {
- $messages[User::COL_NAME_EMAIL] = trans('validation.unique');
- }
- }
- }
- }
|