-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from leepeuker/username-instead-of-id-in-routes
User username as route parameter instead of user id
- Loading branch information
Showing
34 changed files
with
386 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php declare(strict_types=1); | ||
|
||
use Phinx\Migration\AbstractMigration; | ||
|
||
final class UpdateUsername extends AbstractMigration | ||
{ | ||
public function down() : void | ||
{ | ||
$this->execute( | ||
<<<SQL | ||
ALTER TABLE user MODIFY COLUMN name VARCHAR(256) DEFAULT NULL AFTER email; | ||
SQL | ||
); | ||
} | ||
|
||
public function up() : void | ||
{ | ||
$this->execute( | ||
<<<SQL | ||
UPDATE user SET name = id WHERE name IS NULL; | ||
ALTER TABLE user MODIFY COLUMN name VARCHAR(256) NOT NULL AFTER email; | ||
SQL | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Movary\Application\User\Exception; | ||
|
||
class EmailNotUnique extends InvalidCredentials | ||
{ | ||
public static function create() : self | ||
{ | ||
return new self('Email is not unique.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Movary\Application\User\Exception; | ||
|
||
class UsernameInvalidFormat extends InvalidCredentials | ||
{ | ||
public static function create() : self | ||
{ | ||
return new self('Name is not valid.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Movary\Application\User\Exception; | ||
|
||
class UsernameNotUnique extends InvalidCredentials | ||
{ | ||
public static function create() : self | ||
{ | ||
return new self('Name is not valid.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Movary\Application\User\Service; | ||
|
||
use Movary\Application\User\Exception\EmailNotUnique; | ||
use Movary\Application\User\Exception\PasswordTooShort; | ||
use Movary\Application\User\Exception\UsernameInvalidFormat; | ||
use Movary\Application\User\Exception\UsernameNotUnique; | ||
use Movary\Application\User\Repository; | ||
|
||
class Validator | ||
{ | ||
private const PASSWORD_MIN_LENGTH = 8; | ||
|
||
public function __construct(private readonly Repository $repository) | ||
{ | ||
} | ||
|
||
public function ensureEmailIsUnique(string $email, ?int $expectUserId = null) : void | ||
{ | ||
$user = $this->repository->findUserByEmail($email); | ||
if ($user === null) { | ||
return; | ||
} | ||
|
||
if ($user->getId() !== $expectUserId) { | ||
throw new EmailNotUnique(); | ||
} | ||
} | ||
|
||
public function ensureNameFormatIsValid(string $name) : void | ||
{ | ||
preg_match('~^[a-zA-Z0-9]+$~', $name, $matches); | ||
if (empty($matches) === true) { | ||
throw new UsernameInvalidFormat(); | ||
} | ||
} | ||
|
||
public function ensureNameIsUnique(string $name, ?int $expectUserId = null) : void | ||
{ | ||
$user = $this->repository->findUserByName($name); | ||
if ($user === null) { | ||
return; | ||
} | ||
|
||
if ($user->getId() !== $expectUserId) { | ||
throw new UsernameNotUnique(); | ||
} | ||
} | ||
|
||
public function ensurePasswordIsValid(string $password) : void | ||
{ | ||
if (strlen($password) < self::PASSWORD_MIN_LENGTH) { | ||
throw new PasswordTooShort(self::PASSWORD_MIN_LENGTH); | ||
} | ||
} | ||
} |
Oops, something went wrong.