-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d5cd5e
commit 81fc7d6
Showing
11 changed files
with
207 additions
and
121 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Tbbc\RestUtil\Error; | ||
|
||
use Tbbc\RestUtil\Exception\ExceptionHandlerInterface; | ||
use Tbbc\RestUtil\Exception\ExceptionMapInterface; | ||
|
||
class DefaultErrorResolver implements ErrorResolverInterface | ||
{ | ||
protected $handlers; | ||
protected $exceptionMappings; | ||
protected $exceptionToErrorConverter; | ||
|
||
/** | ||
* @param ExceptionToErrorConverterInterface $exceptionToErrorConverter | ||
* @param ExceptionMapInterface[]|array $exceptionMappings | ||
*/ | ||
public function __construct(ExceptionToErrorConverterInterface $exceptionToErrorConverter, array $exceptionMappings) | ||
{ | ||
$this->handlers = array(); | ||
$this->exceptionToErrorConverter = $exceptionToErrorConverter; | ||
$this->exceptionMappings = $exceptionMappings; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function resolveError(\Exception $exception) | ||
{ | ||
$map = null; | ||
foreach($this->exceptionMappings as $mapping) { | ||
if ($this->getFqcn($exception) === $mapping->getMappedExceptionClassName()) { | ||
$map = $mapping; | ||
|
||
break; | ||
} | ||
} | ||
|
||
if (null === $map) { | ||
return null; | ||
} | ||
|
||
$error = $this->exceptionToErrorConverter->convert($exception, $map); | ||
|
||
if (isset($this->handlers[$map->getExceptionHandlerName()])) { | ||
// TODO | ||
} | ||
|
||
return $error; | ||
} | ||
|
||
/** | ||
* Registers a new exception handler | ||
* | ||
* @param ExceptionHandlerInterface $handler | ||
*/ | ||
public function registerHandler(ExceptionHandlerInterface $handler) | ||
{ | ||
$this->handlers[$handler->getName()] = $handler; | ||
} | ||
|
||
/** | ||
* @param mixed $object | ||
* @return string | ||
*/ | ||
private function getFqcn($object) | ||
{ | ||
return get_class($object); | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...Response/Error/ErrorResolverInterface.php → ...RestUtil/Error/ErrorResolverInterface.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
namespace Tbbc\RestUtil\Response\Error; | ||
namespace Tbbc\RestUtil\Error; | ||
|
||
interface ErrorResolverInterface | ||
{ | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Tbbc\RestUtil\Error; | ||
|
||
use Tbbc\RestUtil\Exception\ExceptionMapInterface; | ||
|
||
class ExceptionToErrorConverter implements ExceptionToErrorConverterInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function convert(\Exception $exception, ExceptionMapInterface $exceptionMap) | ||
{ | ||
$status = $exceptionMap->getHttpCode(); | ||
$code = $exceptionMap->getErrorCode(); | ||
$message = $exceptionMap->getErrorMessage(); | ||
$moreInfoUrl = $exceptionMap->getErrorMoreInfoUrl(); | ||
|
||
$error = new Error($status, $code, $message, $moreInfoUrl); | ||
|
||
return $error; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Tbbc/RestUtil/Error/ExceptionToErrorConverterInterface.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Tbbc\RestUtil\Error; | ||
|
||
use Tbbc\RestUtil\Exception\ExceptionMapInterface; | ||
|
||
interface ExceptionToErrorConverterInterface | ||
{ | ||
/** | ||
* Converts the given exception into an Error object by using the given ExceptionMap | ||
* | ||
* @param \Exception $exception | ||
* @param ExceptionMapInterface $exceptionMap | ||
* @return Error | ||
*/ | ||
function convert(\Exception $exception, ExceptionMapInterface $exceptionMap); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Tbbc\RestUtil\Exception; | ||
|
||
interface ExceptionHandlerInterface | ||
{ | ||
/** | ||
* Handles the given exception, converting it into a valid Error object | ||
* | ||
* @param \Exception $e | ||
* @return \Tbbc\RestUtil\Error\Error | ||
*/ | ||
function handle(\Exception $e); | ||
|
||
/** | ||
* Returns handler unique name identifier | ||
* | ||
* @return string | ||
*/ | ||
function getName(); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Tbbc/RestUtil/Exception/ExceptionMapFactoryInterface.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Tbbc\RestUtil\Exception; | ||
|
||
interface ExceptionMapFactoryInterface | ||
{ | ||
/** | ||
* @param int $httpCode | ||
* @param string $mappedExceptionClassName | ||
* @param int $errorCode | ||
* @param string $errorMessage | ||
* @param string $errorMoreInfoUrl | ||
* @param string $exceptionHandlerName | ||
* | ||
* @return ExceptionMapInterface | ||
*/ | ||
public function createExceptionMap($httpCode, $mappedExceptionClassName, $errorCode, $errorMessage = null, | ||
$errorMoreInfoUrl = null, $exceptionHandlerName = null); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Tbbc\RestUtil\Exception; | ||
|
||
interface ExceptionMapInterface | ||
{ | ||
/** | ||
* Returns the Http status code | ||
* | ||
* @return int | ||
*/ | ||
function getHttpCode(); | ||
|
||
/** | ||
* Returns the full qualified class name of the mapped exception | ||
* | ||
* @return string | ||
*/ | ||
function getMappedExceptionClassName(); | ||
|
||
/** | ||
* Returns the error code that Error object should be created with | ||
* | ||
* @return int | ||
*/ | ||
function getErrorCode(); | ||
|
||
/** | ||
* Returns the message that Error object should be created with | ||
* | ||
* @return string | ||
*/ | ||
function getErrorMessage(); | ||
|
||
/** | ||
* Returns more info url that Error object should be created with | ||
* | ||
* @return string | ||
*/ | ||
function getErrorMoreInfoUrl(); | ||
|
||
/** | ||
* Returns an exception handler name if this exception needs one | ||
* | ||
* @return string | ||
*/ | ||
function getExceptionHandlerName(); | ||
} |
This file was deleted.
Oops, something went wrong.
91 changes: 0 additions & 91 deletions
91
src/Tbbc/RestUtil/Response/Error/ExceptionToErrorConverter.php
This file was deleted.
Oops, something went wrong.
12 changes: 6 additions & 6 deletions
12
...sts/Response/DefaultErrorResolverTest.php → .../Tests/Error/DefaultErrorResolverTest.php
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