Skip to content

Commit

Permalink
introduce query-exception
Browse files Browse the repository at this point in the history
  • Loading branch information
floriansemm committed Nov 3, 2016
1 parent 1635e35 commit 9108d15
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 9 deletions.
8 changes: 8 additions & 0 deletions Query/Exception/QueryException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace FS\SolrBundle\Query\Exception;

class QueryException extends \RuntimeException
{

}
6 changes: 4 additions & 2 deletions Query/FindByDocumentNameQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace FS\SolrBundle\Query;

use FS\SolrBundle\Query\Exception\QueryException;

/**
* Builds a wildcard query to find all documents
*
Expand All @@ -25,14 +27,14 @@ public function setDocumentName($documentName)
/**
* @return string
*
* @throws \RuntimeException if documentName is null
* @throws QueryException if documentName is null
*/
public function getQuery()
{
$documentName = $this->documentName;

if ($documentName == null) {
throw new \RuntimeException('documentName should not be null');
throw new QueryException('documentName should not be null');
}

$documentLimitation = $this->createFilterQuery('id')->setQuery(sprintf('id:%s_*', $documentName));
Expand Down
6 changes: 4 additions & 2 deletions Query/FindByIdentifierQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace FS\SolrBundle\Query;

use FS\SolrBundle\Query\Exception\QueryException;

class FindByIdentifierQuery extends AbstractQuery
{
/**
Expand All @@ -20,14 +22,14 @@ public function setDocumentKey($documentKey)
/**
* @return string
*
* @throws \RuntimeException when id or document_name is null
* @throws QueryException when id or document_name is null
*/
public function getQuery()
{
$idField = $this->documentKey;

if ($idField == null) {
throw new \RuntimeException('id should not be null');
throw new QueryException('id should not be null');
}

$documentLimitation = $this->createFilterQuery('id')->setQuery(sprintf('id:%s', $idField));
Expand Down
7 changes: 4 additions & 3 deletions Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use FS\SolrBundle\Doctrine\Mapper\MetaInformation;
use FS\SolrBundle\Doctrine\Mapper\MetaInformationInterface;
use FS\SolrBundle\Query\Exception\UnknownFieldException;
use FS\SolrBundle\SolrInterface;
use Minimalcode\Search\Criteria;

Expand Down Expand Up @@ -41,7 +42,7 @@ public function where($field)
{
$solrField = $this->metaInformation->getField($field);
if ($solrField === null) {
throw new \RuntimeException(sprintf('Field %s does not exists', $field));
throw new UnknownFieldException(sprintf('Field %s does not exists', $field));
}

$fieldName = $solrField->getNameWithAlias();
Expand All @@ -64,7 +65,7 @@ public function andWhere($field)

$solrField = $this->metaInformation->getField($field);
if ($solrField === null) {
throw new \RuntimeException(sprintf('Field %s does not exists', $field));
throw new UnknownFieldException(sprintf('Field %s does not exists', $field));
}

$fieldName = $solrField->getNameWithAlias();
Expand All @@ -87,7 +88,7 @@ public function orWhere($field)

$solrField = $this->metaInformation->getField($field);
if ($solrField === null) {
throw new \RuntimeException(sprintf('Field %s does not exists', $field));
throw new UnknownFieldException(sprintf('Field %s does not exists', $field));
}

$fieldName = $solrField->getNameWithAlias();
Expand Down
8 changes: 8 additions & 0 deletions Query/QueryBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,34 @@

namespace FS\SolrBundle\Query;

use FS\SolrBundle\Query\Exception\UnknownFieldException;

interface QueryBuilderInterface
{
/**
* @param string $field
*
* @return QueryBuilderInterface
*
* @throws UnknownFieldException if $field does not exists
*/
public function where($field);

/**
* @param string $field
*
* @return QueryBuilderInterface
*
* @throws UnknownFieldException if $field does not exists
*/
public function andWhere($field);

/**
* @param string $field
*
* @return QueryBuilderInterface
*
* @throws UnknownFieldException if $field does not exists
*/
public function orWhere($field);

Expand Down
2 changes: 1 addition & 1 deletion Tests/Query/FindByDocumentNameQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testGetQuery_SearchInAllFields()
}

/**
* @expectedException \RuntimeException
* @expectedException FS\SolrBundle\Query\Exception\QueryException
* @expectedExceptionMessage documentName should not be null
*/
public function testGetQuery_DocumentnameMissing()
Expand Down
2 changes: 1 addition & 1 deletion Tests/Query/FindByIdentifierQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testGetQuery_SearchInAllFields()
}

/**
* @expectedException \RuntimeException
* @expectedException FS\SolrBundle\Query\Exception\QueryException
* @expectedExceptionMessage id should not be null
*/
public function testGetQuery_IdMissing()
Expand Down

0 comments on commit 9108d15

Please sign in to comment.