Skip to content

Commit

Permalink
Refactored everything :p
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamindulau committed Jul 29, 2013
1 parent 2d5cd5e commit 81fc7d6
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 121 deletions.
70 changes: 70 additions & 0 deletions src/Tbbc/RestUtil/Error/DefaultErrorResolver.php
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tbbc\RestUtil\Response\Error;
namespace Tbbc\RestUtil\Error;


class Error
Expand Down Expand Up @@ -41,7 +41,7 @@ class Error
private $moreInforUrl;


public function __construct($status, $code, $message, $extendedMessage = null, $moreInfoUrl = null)
public function __construct($status, $code, $message, $moreInfoUrl = null, $extendedMessage = null)
{
if (null == $status) {
throw new \InvalidArgumentException('Http status cannot be null.');
Expand Down
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
{
Expand Down
23 changes: 23 additions & 0 deletions src/Tbbc/RestUtil/Error/ExceptionToErrorConverter.php
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 src/Tbbc/RestUtil/Error/ExceptionToErrorConverterInterface.php
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);
}
21 changes: 21 additions & 0 deletions src/Tbbc/RestUtil/Exception/ExceptionHandlerInterface.php
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 src/Tbbc/RestUtil/Exception/ExceptionMapFactoryInterface.php
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);
}
48 changes: 48 additions & 0 deletions src/Tbbc/RestUtil/Exception/ExceptionMapInterface.php
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();
}
21 changes: 0 additions & 21 deletions src/Tbbc/RestUtil/Response/Error/DefaultErrorResolver.php

This file was deleted.

91 changes: 0 additions & 91 deletions src/Tbbc/RestUtil/Response/Error/ExceptionToErrorConverter.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<?php

namespace Tbbc\RestUtil\Tests\Response;
namespace Tbbc\RestUtil\Tests\Error;


use Tbbc\RestUtil\Response\Error\DefaultErrorResolver;
use Tbbc\RestUtil\Response\Error\ExceptionToErrorConverter;
use Tbbc\RestUtil\Response\Error\Error;
use Tbbc\RestUtil\Error\DefaultErrorResolver;
use Tbbc\RestUtil\Error\ExceptionToErrorConverter;
use Tbbc\RestUtil\Error\Error;
use Tbbc\RestUtil\Response\HttpCodes;

class DefaultErrorResolverTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Tbbc\RestUtil\Response\Error\ExceptionToErrorConverter
* @var \Tbbc\RestUtil\Error\ExceptionToErrorConverter
*/
protected $exceptionToErrorConverter;

/**
* @var \Tbbc\RestUtil\Response\Error\ErrorResolverInterface
* @var \Tbbc\RestUtil\Error\ErrorResolverInterface
*/
protected $errorResolver;

Expand Down

0 comments on commit 81fc7d6

Please sign in to comment.