Skip to content

Commit b66efa1

Browse files
committed
Simplified user constructor not exposing uneeded properties for domain
1 parent 0d76c7c commit b66efa1

File tree

3 files changed

+50
-45
lines changed

3 files changed

+50
-45
lines changed

spec/BenGor/User/Domain/Event/UserRememberPasswordRequestedMailerSubscriberSpec.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,15 @@ function it_implements_domain_event_subscriber()
5555

5656
function it_handles(UserUrlGenerator $urlGenerator, UserMailableFactory $mailableFactory)
5757
{
58-
$domainEvent = new UserRememberPasswordRequested(
59-
new User(
60-
new UserId(),
61-
new UserEmail('bengor@user.com'),
62-
UserPassword::fromEncoded('endoced-password', 'salt'),
63-
[new UserRole('ROLE_USER')],
64-
null,
65-
null,
66-
null,
67-
null,
68-
new UserToken()
69-
)
58+
$user = new User(
59+
new UserId(),
60+
new UserEmail('bengor@user.com'),
61+
UserPassword::fromEncoded('endoced-password', 'salt'),
62+
[new UserRole('ROLE_USER')]
7063
);
64+
$user->rememberPassword();
65+
66+
$domainEvent = new UserRememberPasswordRequested($user);
7167
$mailable = new UserMailable(
7268
new UserEmail('benatespina@gmail.com'),
7369
new UserEmail('bengor@user.com'),

src/BenGor/User/Domain/Model/User.php

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -100,35 +100,19 @@ class User
100100
/**
101101
* Constructor.
102102
*
103-
* @param UserId $anId The id
104-
* @param UserEmail $anEmail The email
105-
* @param UserPassword $aPassword The encoded password
106-
* @param array $userRoles Array which contains the roles
107-
* @param \DateTimeInterface|null $aCreatedOn The created on
108-
* @param \DateTimeInterface|null $anUpdatedOn The updated on
109-
* @param \DateTimeInterface|null $aLastLogin The last login
110-
* @param UserToken|null $aConfirmationToken The confirmation token
111-
* @param UserToken|null $aRememberPasswordToken The remember me token
103+
* @param UserId $anId The id
104+
* @param UserEmail $anEmail The email
105+
* @param UserPassword $aPassword The encoded password
106+
* @param array $userRoles Array which contains the roles
112107
*/
113-
public function __construct(
114-
UserId $anId,
115-
UserEmail $anEmail,
116-
UserPassword $aPassword,
117-
array $userRoles,
118-
\DateTimeInterface $aCreatedOn = null,
119-
\DateTimeInterface $anUpdatedOn = null,
120-
\DateTimeInterface $aLastLogin = null,
121-
UserToken $aConfirmationToken = null,
122-
UserToken $aRememberPasswordToken = null
123-
) {
108+
public function __construct(UserId $anId, UserEmail $anEmail, UserPassword $aPassword, array $userRoles)
109+
{
124110
$this->id = $anId;
125111
$this->email = $anEmail;
126112
$this->password = $aPassword;
127-
$this->confirmationToken = $aConfirmationToken ?: new UserToken();
128-
$this->createdOn = $aCreatedOn ?: new \DateTimeImmutable();
129-
$this->updatedOn = $anUpdatedOn ?: new \DateTimeImmutable();
130-
$this->lastLogin = $aLastLogin ?: null;
131-
$this->rememberPasswordToken = $aRememberPasswordToken;
113+
$this->confirmationToken = new UserToken();
114+
$this->createdOn = new \DateTimeImmutable();
115+
$this->updatedOn = new \DateTimeImmutable();
132116

133117
$this->roles = [];
134118
foreach ($userRoles as $userRole) {

src/BenGor/User/Infrastructure/Persistence/Sql/SqlUserRepository.php

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ private function exist(User $aUser)
163163
'SELECT COUNT(*) FROM user WHERE id = :id', [':id' => $aUser->id()->id()]
164164
)->fetchColumn();
165165

166-
return (int) $count === 1;
166+
return (int)$count === 1;
167167
}
168168

169169
/**
@@ -262,6 +262,8 @@ private function execute($aSql, array $parameters)
262262
*/
263263
private function buildUser($row)
264264
{
265+
$createdOn = new \DateTimeImmutable($row['created_on']);
266+
$updatedOn = new \DateTimeImmutable($row['updated_on']);
265267
$lastLogin = null === $row['last_login']
266268
? null
267269
: new \DateTimeImmutable($row['last_login']);
@@ -272,17 +274,20 @@ private function buildUser($row)
272274
? null
273275
: new UserToken($row['remember_password_token']);
274276

275-
return new User(
277+
$user = new User(
276278
new UserId($row['id']),
277279
new UserEmail($row['email']),
278280
UserPassword::fromEncoded($row['password'], $row['salt']),
279-
$this->rolesToArray($row['roles']),
280-
new \DateTimeImmutable($row['created_on']),
281-
new \DateTimeImmutable($row['updated_on']),
282-
$lastLogin,
283-
$confirmationToken,
284-
$rememberPasswordToken
281+
$this->rolesToArray($row['roles'])
285282
);
283+
284+
$user = $this->set($user, 'createdOn', $createdOn);
285+
$user = $this->set($user, 'updatedOn', $updatedOn);
286+
$user = $this->set($user, 'lastLogin', $lastLogin);
287+
$user = $this->set($user, 'confirmationToken', $confirmationToken);
288+
$user = $this->set($user, 'rememberPasswordToken', $rememberPasswordToken);
289+
290+
return $user;
286291
}
287292

288293
/**
@@ -314,4 +319,24 @@ private function rolesToArray($userRoles)
314319
return new UserRole($userRole);
315320
}, json_decode($userRoles));
316321
}
322+
323+
/**
324+
* Populates by Reflection the domain object with the given SQL plain values.
325+
*
326+
* @param User $user The user domain object
327+
* @param string $propertyName The property name
328+
* @param mixed $propertyValue The property value
329+
*
330+
* @return User
331+
*/
332+
private function set(User $user, $propertyName, $propertyValue)
333+
{
334+
$reflectionUser = new \ReflectionClass($user);
335+
336+
$reflectionCreatedOn = $reflectionUser->getProperty($propertyName);
337+
$reflectionCreatedOn->setAccessible(true);
338+
$reflectionCreatedOn->setValue($user, $propertyValue);
339+
340+
return $user;
341+
}
317342
}

0 commit comments

Comments
 (0)