Automation/app/Libraries/MyStorage/Mangboard/UserLibrary.php
2024-09-13 18:33:27 +09:00

55 lines
1.7 KiB
PHP

<?php
namespace App\Libraries\MyStorage\Mangboard;
use App\Libraries\MySocket\WebLibrary;
use App\Models\Mangboard\UserModel;
use App\Entities\Mangboard\UserEntity;
class UserLibrary extends WebLibrary
{
private $_web_library = null;
private $_user_model = null;
private $_user_entity = null;
public function __construct() {}
public function getUserModel(): UserModel
{
if ($this->_user_model === null) {
return $this->_user_model = new UserModel();
}
return $this->_user_model;
}
public function getWebLibrary(string $host): WebLibrary
{
if ($this->_web_library === null) {
$this->_web_library = new WebLibrary($host);
}
return $this->_web_library;
}
// 로그인 메서드
public function login(string $host, string $id, string $password): bool|UserEntity
{
$response = $this->getWebLibrary($host)->getResponse(
$host . getenv("mangboard.login.url"),
"post",
[
'form_params' => [
'user_id' => $id,
'password' => $password,
],
]
);
if ($response->getStatusCode() == 200) {
// $entity = $this->getUserModel()->getEntityByLoginCheck($id, $password);
$entity = $this->getUserModel()->getEntityByID($id);
if ($entity === null) {
throw new \Exception("{$id}는 회원이 아니거나 암호가 맞지 않습니다.");
}
} else {
throw new \Exception("연결실패:" . $response->getStatusCode());
}
log_message("notice", "{$id}로 로그인 성공");
return $entity;
}
}