Skip to content

Instantly share code, notes, and snippets.

@fesor
Last active February 4, 2016 13:03
Show Gist options
  • Save fesor/e5f8f36a7c4e83ff3fdb to your computer and use it in GitHub Desktop.
Save fesor/e5f8f36a7c4e83ff3fdb to your computer and use it in GitHub Desktop.
<?php
class SecureCodeGenerator {
private $secureRandom;
public function __construct(SecureRandomInterface $secureRandom) {
this->secureRandom = $secureRandom;
}
public function generate() {
$confirmationCode = $this
->secureRandom
->nextBytes(4);
return md5($confirmationCode);
}
}
class AccountManager
{
private $secureCodeGenerator;
public function __construct(
SecureCodeGenerator $secureCodeGenerator
) {
$this->secureCodeGenerator = $secureCodeGenerator;
}
public function createAccount(Account $account)
{
$account->setConfirmationCode(
$this->secureCodeGenerator->generate()
);
}
}
<?php
class AccountManager
{
private $entityManager;
private $secureRandom;
public function __construct(
SecureRandomInterface $secureRandom
) {
$this->secureRandom = $secureRandom;
}
public function createAccount(Account $account)
{
$confirmationCode = $this
->secureRandom
->nextBytes(4);
$account
->setConfirmationCode(md5($confirmationCode));
}
}
<?php
class AccountManager
{
private $entityManager;
private $secureRandom;
public function __construct(
EntityManger $entityManager,
SecureRandomInterface $secureRandom
) {
$this->entityManager = $entityManager;
$this->secureRandom = $secureRandom;
}
public function createAccount(Account $account)
{
$confirmationCode = $this
->secureRandom
->nextBytes(4);
$account
->setConfirmationCode(md5($confirmationCode));
$this->entityManager->persist($account);
$this->entityManager->flush();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment