-
Notifications
You must be signed in to change notification settings - Fork 5
Converted the generic concept of User
entity to a complete aggregate object
#4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ocramius
wants to merge
1
commit into
feature/value-types
Choose a base branch
from
feature/move-entity-to-fully-fledged-aggregate
base: feature/value-types
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,53 @@ | ||
<?php | ||
|
||
namespace Authentication\Aggregate; | ||
|
||
use Authentication\ReadModel\EmailIsRegistered; | ||
use Authentication\ReadModel\IsUserBlocked; | ||
use Authentication\Service\NotifyOfIntrusionDetection; | ||
use Authentication\Value\ClearTextPassword; | ||
use Authentication\Value\EmailAddress; | ||
use Authentication\Value\PasswordHash; | ||
use Webmozart\Assert\Assert; | ||
|
||
class User | ||
{ | ||
/** @var EmailAddress */ | ||
private $email; | ||
|
||
/** @var PasswordHash */ | ||
private $passwordHash; | ||
|
||
private function __construct(EmailAddress $email, ClearTextPassword $password) | ||
{ | ||
$this->email = $email; | ||
$this->passwordHash = $password->makeHash(); | ||
} | ||
|
||
public static function register( | ||
EmailAddress $email, | ||
ClearTextPassword $password, | ||
EmailIsRegistered $emailIsRegistered | ||
) : self { | ||
Assert::false($emailIsRegistered->__invoke($email)); | ||
|
||
return new self($email, $password); | ||
} | ||
|
||
public function authenticate( | ||
ClearTextPassword $password, | ||
IsUserBlocked $blockedUsers, | ||
NotifyOfIntrusionDetection $intrusionDetection | ||
) : void { | ||
Assert::true($password->verify($this->passwordHash)); | ||
|
||
if ($blockedUsers->__invoke($this->email)) { | ||
$intrusionDetection->__invoke($this->email); | ||
|
||
throw new \RuntimeException(sprintf( | ||
'User %s is blocked, and cannot authenticate', | ||
$this->email->toString() | ||
)); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,10 @@ | ||
<?php | ||
|
||
namespace Authentication\ReadModel; | ||
|
||
use Authentication\Value\EmailAddress; | ||
|
||
interface EmailIsRegistered | ||
{ | ||
public function __invoke(EmailAddress $emailAddress) : bool; | ||
} |
This file contains hidden or 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,10 @@ | ||
<?php | ||
|
||
namespace Authentication\ReadModel; | ||
|
||
use Authentication\Value\EmailAddress; | ||
|
||
interface IsUserBlocked | ||
{ | ||
public function __invoke(EmailAddress $emailAddress) : bool; | ||
} |
This file contains hidden or 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 hidden or 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,10 @@ | ||
<?php | ||
|
||
namespace Authentication\Service; | ||
|
||
use Authentication\Value\EmailAddress; | ||
|
||
interface NotifyOfIntrusionDetection | ||
{ | ||
public function __invoke(EmailAddress $emailAddress) : void; | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Infrastructure/Authentication/ReadModel/CheckRegisteredEmailFromRepository.php
This file contains hidden or 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); | ||
|
||
namespace Infrastructure\Authentication\ReadModel; | ||
|
||
use Authentication\ReadModel\EmailIsRegistered; | ||
use Authentication\Repository\Users; | ||
use Authentication\Value\EmailAddress; | ||
|
||
final class CheckRegisteredEmailFromRepository implements EmailIsRegistered | ||
{ | ||
/** @var Users */ | ||
private $users; | ||
|
||
public function __construct(Users $users) | ||
{ | ||
$this->users = $users; | ||
} | ||
|
||
public function __invoke(EmailAddress $emailAddress) : bool | ||
{ | ||
return $this->users->exists($emailAddress); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Infrastructure/Authentication/ReadModel/HardcodedIsUserBlocked.php
This file contains hidden or 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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Infrastructure\Authentication\ReadModel; | ||
|
||
use Authentication\ReadModel\IsUserBlocked; | ||
use Authentication\Value\EmailAddress; | ||
|
||
final class HardcodedIsUserBlocked implements IsUserBlocked | ||
{ | ||
public function __invoke(EmailAddress $emailAddress) : bool | ||
{ | ||
return $emailAddress->toString() === '[email protected]'; | ||
} | ||
} |
This file contains hidden or 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
19 changes: 19 additions & 0 deletions
19
src/Infrastructure/Authentication/Service/SendNotifyOfIntrusionDetectionToStderr.php
This file contains hidden or 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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Infrastructure\Authentication\Service; | ||
|
||
use Authentication\Service\NotifyOfIntrusionDetection; | ||
use Authentication\Value\EmailAddress; | ||
|
||
final class SendNotifyOfIntrusionDetectionToStderr implements NotifyOfIntrusionDetection | ||
{ | ||
public function __invoke(EmailAddress $emailAddress) : void | ||
{ | ||
error_log(sprintf( | ||
'Intrusion detected: unauthorised user %s', | ||
$emailAddress->toString() | ||
)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can I ask.. Why magic method
__invoke
is used, but it is not called like this$blockedUsers($this->email)
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question: this is because of an IDE issue. This is a known issue of PHPStorm, which cannot detect usages of callable objects used with the syntax that you proposed.
While it is true that I shouldn't please my tools too much, the IDE is such a massive central point in my day-to-day operations (especially around refactoring/inspection) that I'd rather keep the
__invoke
explicit until the bug is solved (should already be for PHPStorm 2019.02)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow such a quick reply :) Ok I understand. Thank you!