Skip to content

Commit

Permalink
added findOrFail to the read model repository interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Miliooo committed Oct 28, 2015
1 parent f1b7f72 commit f26a8e6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
4 changes: 2 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/ReadModel/DoctrineORM/DoctrineORMRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Broadway\ReadModel\ReadModelInterface;
use Doctrine\ORM\EntityManagerInterface;
use Milio\CQRS\Readmodel\Exception\NotFoundReadModelException;
use Milio\CQRS\ReadModel\ReadModelRepositoryInterface;

/**
Expand All @@ -14,6 +15,7 @@ class DoctrineORMRepository implements ReadModelRepositoryInterface
private $entityManager;
private $repository;
private $identifierName;
private $class;

/**
* @param EntityManagerInterface $entityManager An entity manager instance
Expand All @@ -22,6 +24,7 @@ class DoctrineORMRepository implements ReadModelRepositoryInterface
*/
public function __construct(EntityManagerInterface $entityManager, $class, $identifierName)
{
$this->class = $class;
$this->entityManager = $entityManager;
$this->repository = $entityManager->getRepository($class);
$this->identifierName = $identifierName;
Expand Down Expand Up @@ -77,4 +80,15 @@ public function remove($id)
$this->entityManager->remove($model);
$this->entityManager->flush($model);
}

public function findOrFail($id)
{
$result = $this->find($id);

if (null === $result) {
throw new NotFoundReadModelException('no result for id '.$id.' and class '.$this->class);
}

return $result;
}
}
11 changes: 11 additions & 0 deletions src/ReadModel/Exception/NotFoundReadModelException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Milio\CQRS\Readmodel\Exception;

/**
* Exception that gets thrown when read model could not be found by it's identifier
*/
class NotFoundReadModelException extends \Exception
{

}
9 changes: 9 additions & 0 deletions src/ReadModel/ReadModelRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@

use Broadway\ReadModel\ReadModelInterface;
use Broadway\ReadModel\RepositoryInterface;
use Milio\CQRS\Readmodel\Exception\NotFoundReadModelException;

interface ReadModelRepositoryInterface extends RepositoryInterface
{
/**
* @param $id
* @return ReadModelInterface
*
* @throws NotFoundReadModelException when read model was not found
*/
public function findOrFail($id);

/**
* Finds a single read model by a set of criteria.
*
Expand Down

0 comments on commit f26a8e6

Please sign in to comment.