2023-07-07 00:07:19 +06:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Services\Private;
|
|
|
|
|
|
|
|
use App\Dto\Request\Private\Profile\Update;
|
2023-07-10 21:42:55 +06:00
|
|
|
use App\Dto\Request\Private\Profile\UpdateSettings;
|
2023-08-01 22:04:35 +06:00
|
|
|
use App\Dto\Request\Private\User\UpdatePassword;
|
2023-12-05 00:53:04 +06:00
|
|
|
use App\Helpers\Helpers;
|
2023-07-07 00:07:19 +06:00
|
|
|
use App\Models\User;
|
|
|
|
use App\ServiceResults\ServiceResultError;
|
|
|
|
use App\ServiceResults\ServiceResultSuccess;
|
|
|
|
use App\Services\Service;
|
|
|
|
use App\Services\User\UserCommandHandler;
|
|
|
|
|
|
|
|
final class ProfileService extends Service
|
|
|
|
{
|
|
|
|
public function __construct(
|
|
|
|
private readonly UserCommandHandler $userCommandHandler
|
|
|
|
) { }
|
|
|
|
|
|
|
|
public function update(Update $update, User $user): ServiceResultError | ServiceResultSuccess
|
|
|
|
{
|
2023-12-05 00:53:04 +06:00
|
|
|
if (Helpers::isDemoModeAndUserDenyUpdate($user)) {
|
|
|
|
return $this->errValidate(__('Demo Mode'));
|
|
|
|
}
|
|
|
|
|
2023-07-07 00:07:19 +06:00
|
|
|
try {
|
|
|
|
$data = [
|
|
|
|
'name' => $update->getName()
|
|
|
|
];
|
|
|
|
$this->userCommandHandler->handleUpdate($user, $data);
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
report($e->getMessage());
|
|
|
|
return $this->errService($e->getMessage());
|
|
|
|
}
|
|
|
|
return $this->ok(__('Profile saved successfully'));
|
|
|
|
}
|
2023-07-07 18:08:14 +06:00
|
|
|
|
|
|
|
public function updatePassword(UpdatePassword $update, User $user): ServiceResultError | ServiceResultSuccess
|
|
|
|
{
|
2023-12-05 00:53:04 +06:00
|
|
|
if (Helpers::isDemoModeAndUserDenyUpdate($user)) {
|
|
|
|
return $this->errValidate(__('Demo Mode'));
|
|
|
|
}
|
|
|
|
|
2023-07-07 18:08:14 +06:00
|
|
|
try {
|
|
|
|
$this->userCommandHandler->handleUpdatePassword($user, $update->getPassword());
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
report($e->getMessage());
|
|
|
|
return $this->errService($e->getMessage());
|
|
|
|
}
|
|
|
|
return $this->ok(__('The password has been changed'));
|
|
|
|
}
|
2023-07-10 21:42:55 +06:00
|
|
|
|
|
|
|
public function updateSettings(UpdateSettings $update, User $user): ServiceResultError | ServiceResultSuccess
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$data = [
|
|
|
|
'lang' => $update->getLang(),
|
|
|
|
'timezone' => $update->getTimezone(),
|
|
|
|
];
|
|
|
|
$this->userCommandHandler->handleUpdate($user, $data);
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
report($e->getMessage());
|
|
|
|
return $this->errService($e->getMessage());
|
|
|
|
}
|
|
|
|
return $this->ok(__('The settings have been saved'));
|
|
|
|
}
|
2023-07-07 00:07:19 +06:00
|
|
|
}
|