|
- <?php
-
- namespace App\Logic\User;
-
- use App\Codes\UserRole;
- use App\Exceptions\AppCommonException;
- use App\Features\InstanceAble;
- use App\Models\Contract;
- use App\Models\Ex\LoginUser;
- use App\Models\User;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\Hash;
- use Illuminate\Support\Facades\Validator;
- use LogicException;
-
- abstract class UserManager
- {
-
- use InstanceAble;
-
- static public function getManager(User $user): static
- {
- $loginUser = LoginUser::instance()->user();
-
- if ($user->role === UserRole::SUPER_ADMIN) {
- if ($loginUser->role === UserRole::SUPER_ADMIN) {
- return AdminUserManager::instance();
- }
- throw new AppCommonException("認可不良");
- }
- if ($user->role === UserRole::CONTRACT_ADMIN) {
- if (UserRole::CONTRACT_ADMIN->value <= $loginUser->role->value) {
- return ContractAdminUserManager::instance();
- }
- throw new AppCommonException("認可不良");
- }
- if ($user->role === UserRole::NORMAL_ADMIN) {
- if (UserRole::NORMAL_ADMIN->value <= $loginUser->role->value) {
- return LoginUserManager::instance();
- }
- throw new AppCommonException("認可不良");
- }
-
- throw new LogicException("未定義ロール");
- }
-
-
- protected bool $initialized = false;
- protected ?User $user = null;
- protected ?Contract $contract = null;
-
- public function initForCreate(string|Contract $contractId): static
- {
- $this->setContract($contractId);
- $this->setUser(null);
- $this->initialized = true;
- return $this;
- }
-
- public function initForCreateAdmin(): static
- {
- throw new LogicException("不許可な関数アクセス");
- }
-
- public function initForModify(string|Contract $contractId, string|User $userId): static
- {
- $this->setContract($contractId);
- $this->setUser($userId);
- $this->initialized = true;
- return $this;
- }
-
- public function initForModifyAdmin(string|User $userId): static
- {
- throw new LogicException("不許可な関数アクセス");
- }
-
- public function getTimestamp(): Carbon
- {
- if (!$this->initialized) {
- throw new LogicException("初期化不正");
- }
-
- if ($this->user === null) {
- throw new LogicException("初期化不正");
- }
-
- if ($this->contract === null) {
- return $this->user->updated_at;
- }
-
- 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 [];
- }
-
- protected function setContract(string|Contract|null $contractId)
- {
- if ($contractId instanceof Contract) {
- $this->contract = (new Contract())->copy($contractId);
- $this->initialized = true;
- return;
- } else if ($contractId === null) {
- $this->initialized = true;
- return;
- }
-
- $this->contract = Contract::findOrFail($contractId);
- $this->initialized = true;
- return;
- }
-
- protected function setUser(string|User|null $userId)
- {
- if ($userId instanceof User) {
- $this->user = (new User())->copy($userId);
- return;
- } else if (is_string($userId)) {
- $this->user = User::findOrFail($userId);
- return;
- }
-
- $this->user = new User();
- if ($this->contract) {
- $this->user->setContract($this->contract);
- }
- $this->user->role = $this->role();
- }
-
- protected function checkParam()
- {
- $validator = Validator::make($this->user->toArray(), []);
-
- if ($validator->failed()) {
- throw new LogicException("バリデートエラー");
- }
-
- $messages = [];
-
- $this->checkContract($messages);
- $this->checkEmailUnique($messages);
- $this->passwordEncrypto($messages);
-
- if ($this->user->isDirty(User::COL_NAME_ROLE)) {
- $this->user->role = $this->role();
- }
-
- return $messages;
- }
-
- protected function checkContract(array &$messages)
- {
- if ($this->user->role !== UserRole::SUPER_ADMIN && $this->user->contract_id === null) {
- throw new LogicException("契約nullエラー");
- }
- }
-
- protected function passwordEncrypto(array &$messages)
- {
- if ($this->user->isDirty(User::COL_NAME_PASSWORD)) {
- $this->user->password = Hash::make($this->user->password);
- }
- }
-
- protected 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');
- }
- }
- }
-
- abstract protected function role(): UserRole;
- }
|