Created
August 19, 2019 07:56
-
-
Save nikolaposa/0101c2ff30c9b98cd2cded148e79465e to your computer and use it in GitHub Desktop.
Demonstrates the use of Method Injection technique to capture key bussiness logic in the entity itself.
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
<?php | |
declare(strict_types=1); | |
final class AnonymizeData | |
{ | |
private $payload; | |
public function __construct(array $payload) | |
{ | |
$this->payload = $payload; | |
} | |
public function userId(): UserId | |
{ | |
return UserId::fromString($this->payload['user_id']); | |
} | |
} | |
final class AnonymizeDataHandler | |
{ | |
private $userRepository; | |
private $faker; | |
public function __construct(UserRepository $userRepository, Faker $faker) | |
{ | |
$this->userRepository = $userRepository; | |
$this->faker = $faker; | |
} | |
public function handle(AnonymizeData $command): void | |
{ | |
$user = $this->userRepository->get($command->userId()); | |
$user->anonymizeData($this->faker); | |
$this->userRepository->save($user); | |
} | |
} | |
interface Faker | |
{ | |
public function email(): EmailAddress; | |
public function randomDate(): DateTimeImmutable; | |
} | |
interface UserRepository | |
{ | |
public function get(UserId $id): User; | |
public function save(User $user): void; | |
} | |
final class User | |
{ | |
private $id; | |
private $emailAddress; | |
private $registeredAt; | |
public static function register(EmailAddress $emailAddress): User | |
{ | |
$user = new self(); | |
$user->id = UserId::new(); | |
$user->emailAddress = $emailAddress; | |
$user->registeredAt = new DateTimeImmutable(); | |
return $user; | |
} | |
public function id(): UserId | |
{ | |
return $this->id; | |
} | |
public function emailAddress(): EmailAddress | |
{ | |
return $this->emailAddress; | |
} | |
public function registeredAt(): DateTimeImmutable | |
{ | |
return $this->registeredAt; | |
} | |
public function anonymizeData(Faker $faker): void | |
{ | |
$this->emailAddress = $faker->email(); | |
$this->registeredAt = $faker->randomDate(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment