Skip to content
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

Add created and updated on the users events #110

Merged
merged 8 commits into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
<entity name="App\Infrastructure\User\Query\Projections\UserView" table="users">
<id name="uuid" type="uuid_binary" column="uuid"/>
<embedded name="credentials" class="App\Domain\User\ValueObject\Auth\Credentials"/>
<field name="createdAt" column="created_at" type="datetime_immutable" />
<field name="updatedAt" column="updated_at" nullable="true" type="datetime_immutable" />
</entity>
</doctrine-mapping>
1 change: 1 addition & 0 deletions config/packages/doctrine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ doctrine:
server_version: '5.7'
types:
uuid_binary: Ramsey\Uuid\Doctrine\UuidBinaryType
datetime_immutable: App\Infrastructure\Share\Doctrine\DateTimeType
mapping_types:
uuid_binary: binary
orm:
Expand Down
13 changes: 13 additions & 0 deletions src/Domain/Shared/Exception/DateTimeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Domain\Shared\Exception;

class DateTimeException extends \Exception
{
public function __construct(\Exception $e)
{
parent::__construct('Datetime Malformed or not valid', 500, $e);
}
}
59 changes: 59 additions & 0 deletions src/Domain/Shared/ValueObject/DateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace App\Domain\Shared\ValueObject;

use App\Domain\Shared\Exception\DateTimeException;

class DateTime
{
const FORMAT = 'Y-m-d\TH:i:s.uP';

/**
* @throws DateTimeException
*/
public static function now(): self
{
return self::create();
}

/**
* @throws DateTimeException
*/
public static function fromString(string $dateTime): self
{
return self::create($dateTime);
}

/**
* @throws DateTimeException
*/
private static function create(string $dateTime = ''): self
{
$self = new self();

try {
$self->dateTime = new \DateTimeImmutable($dateTime);
} catch (\Exception $e) {
throw new DateTimeException($e);
}

return $self;
}

public function toString(): string
{
return $this->dateTime->format(self::FORMAT);
}

public function toNative(): \DateTimeImmutable
{
return $this->dateTime;
}

/**
* @var \DateTimeImmutable
*/
private $dateTime;
}
17 changes: 13 additions & 4 deletions src/Domain/User/Event/UserEmailChanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Domain\User\Event;

use App\Domain\Shared\ValueObject\DateTime;
use App\Domain\User\ValueObject\Email;
use Assert\Assertion;
use Broadway\Serializer\Serializable;
Expand All @@ -22,22 +23,25 @@ public static function deserialize(array $data): self

return new self(
Uuid::fromString($data['uuid']),
Email::fromString($data['email'])
Email::fromString($data['email']),
DateTime::fromString($data['updated_at'])
);
}

public function serialize(): array
{
return [
'uuid' => $this->uuid->toString(),
'email' => $this->email->toString(),
'uuid' => $this->uuid->toString(),
'email' => $this->email->toString(),
'updated_at' => $this->updatedAt->toString(),
];
}

public function __construct(UuidInterface $uuid, Email $email)
public function __construct(UuidInterface $uuid, Email $email, DateTime $updatedAt)
{
$this->email = $email;
$this->uuid = $uuid;
$this->updatedAt = $updatedAt;
}

/**
Expand All @@ -49,4 +53,9 @@ public function __construct(UuidInterface $uuid, Email $email)
* @var Email
*/
public $email;

/**
* @var DateTime
*/
public $updatedAt;
}
18 changes: 7 additions & 11 deletions src/Domain/User/Event/UserSignedIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@

final class UserSignedIn implements Serializable
{
public static function create(UuidInterface $uuid, Email $email): self
{
$instance = new self();

$instance->uuid = $uuid;
$instance->email = $email;

return $instance;
}

/**
* @throws \Assert\AssertionFailedException
*/
Expand All @@ -30,7 +20,7 @@ public static function deserialize(array $data): self
Assertion::keyExists($data, 'uuid');
Assertion::keyExists($data, 'email');

return self::create(
return new self(
Uuid::fromString($data['uuid']),
Email::fromString($data['email'])
);
Expand All @@ -44,6 +34,12 @@ public function serialize(): array
];
}

public function __construct(UuidInterface $uuid, Email $email)
jorge07 marked this conversation as resolved.
Show resolved Hide resolved
{
$this->uuid = $uuid;
$this->email = $email;
}

/** @var Email */
public $email;

Expand Down
14 changes: 12 additions & 2 deletions src/Domain/User/Event/UserWasCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Domain\User\Event;

use App\Domain\Shared\ValueObject\DateTime;
use App\Domain\User\ValueObject\Auth\Credentials;
use App\Domain\User\ValueObject\Auth\HashedPassword;
use App\Domain\User\ValueObject\Email;
Expand All @@ -15,6 +16,7 @@
final class UserWasCreated implements Serializable
{
/**
* @throws \App\Domain\Shared\Exception\DateTimeException
* @throws \Assert\AssertionFailedException
*/
public static function deserialize(array $data): self
Expand All @@ -27,7 +29,8 @@ public static function deserialize(array $data): self
new Credentials(
Email::fromString($data['credentials']['email']),
HashedPassword::fromHash($data['credentials']['password'])
)
),
DateTime::fromString($data['created_at'])
);
}

Expand All @@ -39,13 +42,15 @@ public function serialize(): array
'email' => $this->credentials->email->toString(),
'password' => $this->credentials->password->toString(),
],
'created_at' => $this->createdAt->toString(),
];
}

public function __construct(UuidInterface $uuid, Credentials $credentials)
public function __construct(UuidInterface $uuid, Credentials $credentials, DateTime $createdAt)
{
$this->uuid = $uuid;
$this->credentials = $credentials;
$this->createdAt = $createdAt;
}

/**
Expand All @@ -57,4 +62,9 @@ public function __construct(UuidInterface $uuid, Credentials $credentials)
* @var Credentials
*/
public $credentials;

/**
* @var DateTime
*/
public $createdAt;
}
41 changes: 38 additions & 3 deletions src/Domain/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Domain\User;

use App\Domain\Shared\ValueObject\DateTime;
use App\Domain\User\Event\UserEmailChanged;
use App\Domain\User\Event\UserSignedIn;
use App\Domain\User\Event\UserWasCreated;
Expand All @@ -18,6 +19,9 @@

class User extends EventSourcedAggregateRoot
{
/**
* @throws \App\Domain\Shared\Exception\DateTimeException
*/
public static function create(
UuidInterface $uuid,
Credentials $credentials,
Expand All @@ -27,17 +31,20 @@ public static function create(

$user = new self();

$user->apply(new UserWasCreated($uuid, $credentials));
$user->apply(new UserWasCreated($uuid, $credentials, DateTime::now()));

return $user;
}

/**
* @throws \App\Domain\Shared\Exception\DateTimeException
*/
public function changeEmail(
Email $email,
UniqueEmailSpecificationInterface $uniqueEmailSpecification
): void {
$uniqueEmailSpecification->isUnique($email);
$this->apply(new UserEmailChanged($this->uuid, $email));
$this->apply(new UserEmailChanged($this->uuid, $email, DateTime::now()));
}

/**
Expand All @@ -51,7 +58,7 @@ public function signIn(string $plainPassword): void
throw new InvalidCredentialsException('Invalid credentials entered.');
}

$this->apply(UserSignedIn::create($this->uuid, $this->email));
$this->apply(new UserSignedIn($this->uuid, $this->email));
}

protected function applyUserWasCreated(UserWasCreated $event): void
Expand All @@ -60,6 +67,7 @@ protected function applyUserWasCreated(UserWasCreated $event): void

$this->setEmail($event->credentials->email);
$this->setHashedPassword($event->credentials->password);
$this->setCreatedAt($event->createdAt);
}

/**
Expand All @@ -70,6 +78,7 @@ protected function applyUserEmailChanged(UserEmailChanged $event): void
Assertion::notEq($this->email->toString(), $event->email->toString(), 'New email should be different');

$this->setEmail($event->email);
$this->setUpdatedAt($event->updatedAt);
}

private function setEmail(Email $email): void
Expand All @@ -82,6 +91,26 @@ private function setHashedPassword(HashedPassword $hashedPassword): void
$this->hashedPassword = $hashedPassword;
}

private function setCreatedAt(DateTime $createdAt): void
{
$this->createdAt = $createdAt;
}

private function setUpdatedAt(DateTime $updatedAt): void
jorge07 marked this conversation as resolved.
Show resolved Hide resolved
{
$this->updatedAt = $updatedAt;
}

public function createdAt(): string
{
return $this->createdAt->toString();
}

public function updatedAt(): ?string
{
return isset($this->updatedAt) ? $this->updatedAt->toString() : null;
}

public function email(): string
{
return $this->email->toString();
Expand All @@ -105,4 +134,10 @@ public function getAggregateRootId(): string

/** @var HashedPassword */
private $hashedPassword;

/** @var DateTime */
private $createdAt;

/** @var DateTime|null */
private $updatedAt;
}
Loading