I have been developing a simple login registration page with PHP using the repository pattern. Following are the business rules :
During registration
- Email must be unique.
- Email must be a valid email address.
User table columns: id, name, username, password, created_at, updated_at
register.php page
if($_POST)
{
try{
$input['name'] = $_POST['name'];
$input['email'] = $_POST['email'];
$input['username'] = $_POST['username'];
$input['password'] = $_POST['password'];
$user_register_obj = new UserRegister(new UserInfoRepository());
$user_register_obj->register($input);
header('Location: login.php');
exit();
}
catch(CustomException $e)
{
var_dump($e->getCustomMessage());
}
}
?>
Following is the userinfo interface: This is for the user table. IUserInfo.php
interface IUserInfo {
public function getId();
public function getName();
public function getEmail();
public function getUserName();
public function getCreatedAt();
public function getUpdatedAt();
public function getPassword();
}
User.php file :
class User implements IUserInfo{
private $id;
private $name;
private $email;
private $username;
private $created_at;
private $updated_at;
private $password;
public function __construct($id,
$name,
$email,
$username,
$created_at,
$updated_at,
$password)
{
$this->id = $id;
$this->name = $name;
$this->email = $email;
$this->username = $username;
$this->created_at = $created_at;
$this->updated_at = $updated_at;
$this->password = $password;
}
public function getEmail()
{
return $this->email;
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function getUserName()
{
return $this->username;
}
public function getCreatedAt()
{
return $this->created_at;
}
public function getUpdatedAt()
{
return $this->updated_at;
}
public function getPassword()
{
return $this->password;
}
}
UserInfoRepository :
interface IUserInfoRepository {
public function getById($id);
public function getByEmail($email);
public function getAll();
public function insert($input);
public function update($id, $input);
public function delete($id);
}
This is the repository class for userinfo :
class UserInfoRepository implements IUserInfoRepository{
private $table = 'users';
private $db_obj;
public function __construct()
{
$this->db_obj = new \DatabaseHandler\Database();
}
public function getById($id)
{
$query = 'select * from users where id = ' . $id;
return $this->db_obj->GetOneRow($query);
}
public function getAll()
{
// TODO: Implement getAll() method.
}
public function getByEmail($email)
{
$query = "select * from user where email = '$email'";
$obj = $this->db_obj->GetOneRow($query);
if(!$obj)
{
return null;
}
else
{
return new User($obj['id'], $obj['name'], $obj['email'], $obj['username'], $obj['created_at'], $obj['updated_at'], $obj['password']);
}
}
public function insert($input)
{
$name = $input['name'];
$email = $input['email'];
$username = $input['username'];
$password = md5($input['password']);
$query = "insert into user (name, email, username, password) values ('$name', '$email', '$username', '$password' )";
return $this->db_obj->InsertAndGetId($query);
}
public function update($id, $input)
{
// TODO: Implement update() method.
}
public function delete($id)
{
// TODO: Implement delete() method.
}
}
UserRegister class :
class UserRegister {
private $userRepo;
public function __construct(\IUserInfoRepository $userRepo)
{
$this->userRepo = $userRepo;
}
public function register($input)
{
$email = $input['email'];
$result = filter_var( $email , FILTER_VALIDATE_EMAIL );
if(!$result)
throw new \CustomException("Invalid Email");
$obj = $this->userRepo->getByEmail($email);
if(!is_null($obj))
throw new \CustomException("Email already found");
$this->userRepo->insert($input);
return true;
}
}
Please ignore the database handler Database right now. That class seems fine and I am not posting it as it may make it more complex.
Can you please review my code design ? I will accept answer in any languages.