Created
June 23, 2015 17:08
-
-
Save jeremyb/b07bb2866e4348e7e985 to your computer and use it in GitHub Desktop.
Repository interface example
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 | |
namespace AppBundle\Model; | |
interface UserRepository | |
{ | |
public function add(User $user); | |
public function remove(User $user); | |
public function findById($id); | |
} |
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 | |
namespace AppBundle\Repository; | |
use AppBundle\Model\User; | |
use AppBundle\Model\UserRepository; | |
use Doctrine\ORM\EntityNotFoundException; | |
use Doctrine\ORM\EntityRepository; | |
final class DoctrineUserRepository extends EntityRepository implements UserRepository | |
{ | |
public function add(User $user) | |
{ | |
$this->getEntityManager()->persist($user); | |
$this->getEntityManager()->flush($user); | |
} | |
public function remove(User $user) | |
{ | |
$this->getEntityManager()->remove($user); | |
$this->getEntityManager()->flush($user); | |
} | |
public function findById($id) | |
{ | |
if (null === $user = $this->find($id)) { | |
throw new EntityNotFoundException(); | |
} | |
return $user; | |
} | |
} |
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 | |
namespace AppBundle\Repository; | |
use AppBundle\Model\User; | |
use AppBundle\Model\UserRepository; | |
use Doctrine\ORM\EntityNotFoundException; | |
use Everzet\PersistedObjects\AccessorObjectIdentifier; | |
use Everzet\PersistedObjects\InMemoryRepository; | |
final class InMemoryUserRepository implements UserRepository | |
{ | |
public function __construct() | |
{ | |
$this->repository = new InMemoryRepository( | |
new AccessorObjectIdentifier('getId') | |
); | |
} | |
public function add(User $user) | |
{ | |
$this->repository->save($user); | |
} | |
public function remove(User $user) | |
{ | |
$this->repository->remove($user); | |
} | |
public function findById($id) | |
{ | |
if (null === $user = $this->repository->findById($id)) { | |
throw new EntityNotFoundException(); | |
} | |
return $user; | |
} | |
} |
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 | |
namespace AppBundle\Model; | |
final class User | |
{ | |
private $id; | |
private $username; | |
public function __construct(Username $username) | |
{ | |
$this->username = $username; | |
} | |
public static function withId($id, Username $username) | |
{ | |
$user = new self($username); | |
$user->id = $id; | |
return $user; | |
} | |
public function getId() | |
{ | |
return $this->id; | |
} | |
public function getUsername() | |
{ | |
return $this->username; | |
} | |
} |
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 | |
namespace AppBundle\Model; | |
use Webmozart\Assert\Assert; | |
final class Username | |
{ | |
const MIN_LENGTH = 5; | |
const FORMAT = '/^[a-zA-Z0-9_]+$/'; | |
/** | |
* @var string | |
*/ | |
private $username; | |
public function __construct($username) | |
{ | |
Assert::notEmpty($username, 'The username should not be blank.'); | |
Assert::minLength($username, self::MIN_LENGTH, 'The username should contains at least %2$s characters.'); | |
Assert::regex($username, self::FORMAT, 'The username is invalid.'); | |
$this->username = $username; | |
} | |
/** | |
* @return string | |
*/ | |
public function getUsername() | |
{ | |
return $this->username; | |
} | |
} |
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 | |
$user = User::withId(1, new Username('jbarthe')); | |
$repository = new InMemoryUserRepository(); | |
$repository->add($user); | |
dump($repository->findById(1)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment