Skip to content

Commit

Permalink
Merge pull request idle-user#21 from idle-user/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
idle-user authored Jan 15, 2021
2 parents 147b2fc + 0f84ce6 commit 452d660
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

class DiscordIdAlreadyExistsException extends DomainRecordConflictException
{
protected $message = 'The Discord ID requested is registered.';
protected $message = 'The Discord ID requested is already registered.';
}
2 changes: 1 addition & 1 deletion src/Domain/User/Exception/EmailAlreadyExistsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

class EmailAlreadyExistsException extends DomainRecordConflictException
{
protected $message = 'The email requested is registered.';
protected $message = 'The email requested is already registered.';
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

class TwitterIdAlreadyExistsException extends DomainRecordConflictException
{
protected $message = 'The Twitter ID requested is registered.';
protected $message = 'The Twitter ID requested is already registered.';
}
13 changes: 4 additions & 9 deletions src/Domain/User/Repository/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,10 @@ public function searchByUsername($username)
return $ret;
}

public function register(array $data)
{
$sql = 'INSERT INTO user (username, secret, discord_id, chatango_id, date_created) VALUES (?, ?, ?, ?, NOW())';
$args = [
$data['username'],
password_hash($data['secret'], PASSWORD_BCRYPT),
$data['discord_id'],
$data['chatango_id'],
];
public function register($username, $secret)
{
$sql = 'INSERT INTO user (username, secret, date_created, secret_last_updated) VALUES (?, ?, NOW(), NOW())';
$args = [$username, password_hash($secret, PASSWORD_BCRYPT)];

try {
$this->db->query($sql, $args);
Expand Down
65 changes: 64 additions & 1 deletion src/Domain/User/Service/RegisterUserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

use App\Domain\User\Exception\ChatangoIdAlreadyExistsException;
use App\Domain\User\Exception\DiscordIdAlreadyExistsException;
use App\Domain\User\Exception\EmailAlreadyExistsException;
use App\Domain\User\Exception\TwitterIdAlreadyExistsException;
use App\Domain\User\Exception\UsernameAlreadyExistsException;
use App\Domain\User\Exception\UserNotFoundException;
use App\Exception\ValidationException;
Expand All @@ -13,6 +15,10 @@ final class RegisterUserService extends UserService
{
public function run(array $data)
{
if (!array_key_exists('email', $data)) {
$data['email'] = null;
}

if (!array_key_exists('discord_id', $data)) {
$data['discord_id'] = null;
}
Expand All @@ -21,9 +27,29 @@ public function run(array $data)
$data['chatango_id'] = null;
}

if (!array_key_exists('twitter_id', $data)) {
$data['twitter_id'] = null;
}

$this->validate($data);

$userId = $this->userRepository->register($data);
$userId = $this->userRepository->register($data['username'], $data['secret']);

if ($data['email']) {
$this->userRepository->updateEmailById($userId, $data['email']);
}

if ($data['discord_id']) {
$this->userRepository->updateDiscordIdById($userId, $data['discord_id']);
}

if ($data['chatango_id']) {
$this->userRepository->updateChatangoIdById($userId, $data['chatango_id']);
}

if ($data['twitter_id']) {
$this->userRepository->updateTwitterIdById($userId, $data['twitter_id']);
}

$user = $this->userRepository->findById($userId);
$user->setShowFullDetail(true);
Expand Down Expand Up @@ -53,7 +79,24 @@ private function validate(array $data)
throw new ValidationException('Secret must contain at least 6 characters');
}

if ($data['email'] != null) {
if (!filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
throw new ValidationException('Email is invalid.');
}
try {
$this->userRepository->findByEmail($data['email']);
throw new EmailAlreadyExistsException();
} catch (UserNotFoundException $e) {
}
}

if ($data['discord_id'] != null) {
if (empty($data['discord_id'])) {
throw new ValidationException('chatango_id cannot be empty.');
}
if (strlen($data['discord_id']) > 45) {
throw new ValidationException('discord_id is too long.');
}
try {
$this->userRepository->findByDiscordId($data['discord_id']);
throw new DiscordIdAlreadyExistsException();
Expand All @@ -62,11 +105,31 @@ private function validate(array $data)
}

if ($data['chatango_id'] != null) {
if (empty($data['chatango_id'])) {
throw new ValidationException('chatango_id cannot be empty.');
}
if (strlen($data['chatango_id']) > 45) {
throw new ValidationException('chatango_id is too long.');
}
try {
$this->userRepository->findByChatangoId($data['chatango_id']);
throw new ChatangoIdAlreadyExistsException();
} catch (UserNotFoundException $e) {
}
}

if ($data['twitter_id'] != null) {
if (empty($data['twitter_id'])) {
throw new ValidationException('twitter_id cannot be empty.');
}
if (strlen($data['twitter_id']) > 45) {
throw new ValidationException('twitter_id is too long.');
}
try {
$this->userRepository->findByTwitterId($data['twitter_id']);
throw new TwitterIdAlreadyExistsException();
} catch (UserNotFoundException $e) {
}
}
}
}

0 comments on commit 452d660

Please sign in to comment.