Skip to content

Instantly share code, notes, and snippets.

@jeremyb
Created June 23, 2015 17:08
Show Gist options
  • Save jeremyb/b07bb2866e4348e7e985 to your computer and use it in GitHub Desktop.
Save jeremyb/b07bb2866e4348e7e985 to your computer and use it in GitHub Desktop.
Repository interface example
<?php
namespace AppBundle\Model;
interface UserRepository
{
public function add(User $user);
public function remove(User $user);
public function findById($id);
}
<?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;
}
}
<?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;
}
}
<?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;
}
}
<?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;
}
}
<?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