Skip to content

Commit

Permalink
More refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
galeaspablo committed Sep 16, 2024
1 parent 5982a1a commit 5ca1f73
Show file tree
Hide file tree
Showing 76 changed files with 608 additions and 752 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public function accountDetails(): AccountDetails
}

public static function fromProperties(
Id $id,
Id $aggregateId,
int $aggregateVersion,
VerifiedEmail|UnverifiedEmail|VerifiedButRequestedNewEmail $primaryEmailStatus,
HashedPassword $hashedPassword,
AccountDetails $accountDetails
): User {
$user = new self($id, $aggregateVersion);
$user = new self($aggregateId, $aggregateVersion);

$user->primaryEmailStatus = $primaryEmailStatus;
$user->hashedPassword = $hashedPassword;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SignUp

public string $username;

public string $termsOfUseAccepted;
public bool $termsOfUseAccepted;

public array $metadata;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@

class VerifyPrimaryEmail
{
/**
* @var array
*/
public $metadata;
public array $metadata;

/**
* @var string
*/
public $verificationCode;
public string $verificationCode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Galeas\Api\BoundedContext\Identity\User\Aggregate\User;
use Galeas\Api\BoundedContext\Identity\User\Command\RequestPrimaryEmailChange;
use Galeas\Api\BoundedContext\Identity\User\CommandHandler\SignUp\SignUpHandler;
use Galeas\Api\BoundedContext\Identity\User\CommandHandler\VerifyPrimaryEmail\NoUserFoundForCode;
use Galeas\Api\BoundedContext\Identity\User\Event\PrimaryEmailChangeRequested;
use Galeas\Api\BoundedContext\Identity\User\ValueObject\VerifiedButRequestedNewEmail;
use Galeas\Api\BoundedContext\Identity\User\ValueObject\UnverifiedEmail;
Expand All @@ -18,21 +19,16 @@
use Galeas\Api\Common\Id\Id;
use Galeas\Api\Common\Id\InvalidId;
use Galeas\Api\Primitive\PrimitiveComparison\Email\AreEmailsEquivalent;
use Galeas\Api\Primitive\PrimitiveCreation\Email\EmailVerificationCodeCreator;
use Galeas\Api\Primitive\PrimitiveValidation\Email\EmailValidator;
use Galeas\Api\Service\EventStore\EventStore;
use Galeas\Api\Service\Queue\Queue;

class RequestPrimaryEmailChangeHandler
{
/**
* @var EventStore
*/
private $eventStore;
private EventStore $eventStore;

/**
* @var IsEmailTaken
*/
private $isEmailTaken;
private IsEmailTaken $isEmailTaken;

public function __construct(
EventStore $eventStore,
Expand All @@ -55,10 +51,14 @@ public function handle(RequestPrimaryEmailChange $command): void
{
$this->eventStore->beginTransaction();

$user = $this->eventStore->find($command->authenticatedUserId);
$aggregateAndEventIds = $this->eventStore->find($command->authenticatedUserId);
if (null === $aggregateAndEventIds) {
throw new NoUserFoundForCode();
}

$user = $aggregateAndEventIds->aggregate();
if (!($user instanceof User)) {
throw new UserNotFound();
throw new NoUserFoundForCode();
}

if (
Expand Down Expand Up @@ -113,11 +113,16 @@ public function handle(RequestPrimaryEmailChange $command): void
throw new InvalidEmail();
}

$event = PrimaryEmailChangeRequested::fromProperties(
Id::fromId($command->authenticatedUserId),
Id::fromId($command->authenticatedUserId),
$event = PrimaryEmailChangeRequested::new(
Id::createNew(),
$user->aggregateId(),
$user->aggregateVersion() + 1,
$aggregateAndEventIds->lastEventId(),
$aggregateAndEventIds->firstEventId(),
new \DateTimeImmutable("now"),
$command->metadata,
$command->newEmailRequested,
EmailVerificationCodeCreator::create(),
$user->hashedPassword()->hash()
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,15 @@
use Galeas\Api\Common\ExceptionBase\EventStoreCannotRead;
use Galeas\Api\Common\ExceptionBase\EventStoreCannotWrite;
use Galeas\Api\Common\ExceptionBase\ProjectionCannotRead;
use Galeas\Api\Common\ExceptionBase\QueuingFailure;
use Galeas\Api\Common\Id\Id;
use Galeas\Api\Common\Id\InvalidId;
use Galeas\Api\Service\EventStore\EventStore;
use Galeas\Api\Service\Queue\Queue;

class VerifyPrimaryEmailHandler
{
/**
* @var EventStore
*/
private $eventStore;
private EventStore $eventStore;

/**
* @var UserIdFromPrimaryEmailVerificationCode
*/
private $userIdFromVerificationCode;
private UserIdFromPrimaryEmailVerificationCode $userIdFromVerificationCode;

public function __construct(
EventStore $eventStore,
Expand All @@ -58,12 +50,14 @@ public function handle(VerifyPrimaryEmail $command): void
throw new NoUserFoundForCode();
}

$authenticatedUserId = $userId;

$this->eventStore->beginTransaction();

$user = $this->eventStore->find($userId);
$aggregateAndEventIds = $this->eventStore->find($userId);
if (null === $aggregateAndEventIds) {
throw new NoUserFoundForCode();
}

$user = $aggregateAndEventIds->aggregate();
if (!($user instanceof User)) {
throw new NoUserFoundForCode();
}
Expand All @@ -87,8 +81,12 @@ public function handle(VerifyPrimaryEmail $command): void
}

$event = PrimaryEmailVerified::new(
Id::fromId($userId),
Id::fromId($authenticatedUserId),
Id::createNew(),
$user->aggregateId(),
$user->aggregateVersion() + 1,
$aggregateAndEventIds->lastEventId(),
$aggregateAndEventIds->firstEventId(),
new \DateTimeImmutable("now"),
$command->metadata,
$command->verificationCode
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@

class PrimaryEmailVerificationCode
{
/**
* @var string
*/
private $id;
private string $id;

/**
* @var string|null
*/
private $primaryEmailVerificationCode;
private ?string $primaryEmailVerificationCode;

private function __construct()
{
Expand All @@ -30,19 +24,13 @@ public function getPrimaryEmailVerificationCode(): ?string
return $this->primaryEmailVerificationCode;
}

/**
* @return $this
*/
public function updateVerificationCode(?string $primaryEmailVerificationCode)
public function updateVerificationCode(?string $primaryEmailVerificationCode): self
{
$this->primaryEmailVerificationCode = $primaryEmailVerificationCode;

return $this;
}

/**
* @return PrimaryEmailVerificationCode
*/
public static function fromUserIdAndVerificationCode(string $userId, ?string $primaryEmailVerificationCode): self
{
$primaryEmailVerificationCodeObject = new self();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,18 @@
use Galeas\Api\BoundedContext\Identity\User\Event\SignedUp;
use Galeas\Api\Common\Event\Event;
use Galeas\Api\Common\ExceptionBase\ProjectionCannotProcess;
use Galeas\Api\Service\QueueProcessor\ProjectionEventProcessor;
use Galeas\Api\Service\QueueProcessor\EventProjector;

class PrimaryEmailVerificationCodeProcessor implements ProjectionEventProcessor
class PrimaryEmailVerificationCodeProjector implements EventProjector
{
/**
* @var DocumentManager
*/
private $projectionDocumentManager;
private DocumentManager $projectionDocumentManager;

public function __construct(DocumentManager $projectionDocumentManager)
{
$this->projectionDocumentManager = $projectionDocumentManager;
}

/**
* {@inheritdoc}
*/
public function process(Event $event): void
public function project(Event $event): void
{
try {
if ($event instanceof SignedUp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

class UserIdFromPrimaryEmailVerificationCode implements UserIdFromPrimaryEmailVerificationCodeVPE
{
/**
* @var DocumentManager
*/
private $projectionDocumentManager;
private DocumentManager $projectionDocumentManager;

public function __construct(DocumentManager $projectionDocumentManager)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
use Galeas\Api\BoundedContext\Identity\User\Event\SignedUp;
use Galeas\Api\Common\Event\Event;
use Galeas\Api\Common\ExceptionBase\ProjectionCannotProcess;
use Galeas\Api\Service\QueueProcessor\ProjectionEventProcessor;
use Galeas\Api\Service\QueueProcessor\EventProjector;

class TakenEmailProcessor implements ProjectionEventProcessor
class TakenEmailProjector implements EventProjector
{
/**
* @var DocumentManager
Expand All @@ -27,7 +27,7 @@ public function __construct(DocumentManager $projectionDocumentManager)
/**
* {@inheritdoc}
*/
public function process(Event $event): void
public function project(Event $event): void
{
try {
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use Galeas\Api\BoundedContext\Identity\User\Event\SignedUp;
use Galeas\Api\Common\Event\Event;
use Galeas\Api\Common\ExceptionBase\ProjectionCannotProcess;
use Galeas\Api\Service\QueueProcessor\ProjectionEventProcessor;
use Galeas\Api\Service\QueueProcessor\EventProjector;

class TakenUsernameProcessor implements ProjectionEventProcessor
class TakenUsernameProjector implements EventProjector
{
/**
* @var DocumentManager
Expand All @@ -25,7 +25,7 @@ public function __construct(DocumentManager $projectionDocumentManager)
/**
* {@inheritdoc}
*/
public function process(Event $event): void
public function project(Event $event): void
{
try {
if ($event instanceof SignedUp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@

namespace Galeas\Api\BoundedContext\Identity\User\Reaction\SendPrimaryEmailVerification;

use Galeas\Api\BoundedContext\Identity\User\Aggregate\User;
use Galeas\Api\BoundedContext\Identity\User\CommandHandler\VerifyPrimaryEmail\NoUserFoundForCode;
use Galeas\Api\BoundedContext\Identity\User\Event\PrimaryEmailChangeRequested;
use Galeas\Api\BoundedContext\Identity\User\Event\PrimaryEmailVerificationCodeSent;
use Galeas\Api\BoundedContext\Identity\User\Event\SignedUp;
use Galeas\Api\Common\Event\Event;
use Galeas\Api\Common\ExceptionBase\EventStoreCannotRead;
use Galeas\Api\Common\ExceptionBase\EventStoreCannotWrite;
use Galeas\Api\Common\ExceptionBase\ProjectionCannotRead;
use Galeas\Api\Common\ExceptionBase\QueuingFailure;
use Galeas\Api\Common\Id\Id;
use Galeas\Api\Service\Email\Emailer;
use Galeas\Api\Service\EventStore\EventStore;
use Galeas\Api\Service\QueueProcessor\EventReactor;

class SendPrimaryEmailVerificationReactor
class SendPrimaryEmailVerificationReactor implements EventReactor
{
private EventStore $eventStore;

Expand All @@ -29,7 +31,7 @@ public function __construct(EventStore $eventStore, Emailer $emailer) {

/**
* @throws PrimaryEmailVerificationAlreadySent
* @throws ProjectionCannotRead|EventStoreCannotRead|EventStoreCannotWrite|QueuingFailure
* @throws ProjectionCannotRead|EventStoreCannotRead|EventStoreCannotWrite
*/
public function react(Event $event): void
{
Expand All @@ -47,32 +49,47 @@ public function react(Event $event): void
$newEventId = Id::createNewByHashing(
"Identity/User/PrimaryEmailVerificationSent:" . $event->eventId()->id()
);
$existingEvent = $this->eventStore->findEvent($newEventId->id());

if ($existingEvent instanceof Event) {
$existingReaction = $this->eventStore->findEvent($newEventId->id());
if ($existingReaction instanceof Event) {
throw new PrimaryEmailVerificationAlreadySent();
}

$fromEmailAddress = "system.development-application.example.com";
$subjectLine = "Your Verification Code";
$emailContents = "This is your verification code: https://example.com/page/?verificationCode=" . $verificationCode;
$this->emailer->send(
"[email protected]",
"https://domain.example/frontend/verifyEmail?verificationCode=verificationCode",
"text"
$sendToEmailAddress,
$subjectLine,
$emailContents,
$fromEmailAddress
);

$event = PrimaryEmailVerificationCodeSent::fromProperties(
$newEventId,
$event->aggregateId(),
$event->eventId(),
$aggregateAndEventIds = $this->eventStore->find($existingReaction->aggregateId()->id());
if (null === $aggregateAndEventIds) {
throw new NoUserFoundForCode();
}

$user = $aggregateAndEventIds->aggregate();
if (!($user instanceof User)) {
throw new NoUserFoundForCode();
}

$newEvent = PrimaryEmailVerificationCodeSent::new(
Id::createNew(),
$user->aggregateId(),
$user->aggregateVersion() + 1,
$aggregateAndEventIds->lastEventId(),
$aggregateAndEventIds->firstEventId(),
new \DateTimeImmutable("now"),
[],
$verificationCode,
$sendToEmailAddress,
"This is your verification code: https://example.com/page/?verificationCodeode=" . $verificationCode
$emailContents,
$fromEmailAddress,
$subjectLine
);

$this->eventStore->save($event);
try {
$this->eventStore->completeTransaction();
} catch (\Exception $e) {
// todo , duplicates -> return success
}
$this->eventStore->save($newEvent);
$this->eventStore->completeTransaction();
}
}
Loading

0 comments on commit 5ca1f73

Please sign in to comment.