Split ContentBasedErrorHandler responsibilities into two separated classes

This commit is contained in:
Alejandro Celaya 2016-07-30 10:08:21 +02:00
parent ab6aa99a6d
commit 2f5119d0b3
5 changed files with 57 additions and 21 deletions

View file

@ -1,6 +1,8 @@
<?php
use Shlinkio\Shlink\Common\Expressive\ContentBasedErrorHandlerFactory;
use Shlinkio\Shlink\Rest\Expressive\JsonErrorHandler;
use Acelaya\ZsmAnnotatedServices\Factory\V3\AnnotatedFactory;
use Shlinkio\Shlink\Common\Expressive\ContentBasedErrorHandler;
use Shlinkio\Shlink\Common\Expressive\ErrorHandlerManager;
use Shlinkio\Shlink\Common\Expressive\ErrorHandlerManagerFactory;
use Zend\Expressive;
use Zend\Expressive\Container;
use Zend\Expressive\Helper;
@ -23,11 +25,13 @@ return [
Router\FastRouteRouter::class => InvokableFactory::class,
// View
'Zend\Expressive\FinalHandler' => ContentBasedErrorHandlerFactory::class,
ContentBasedErrorHandler::class => AnnotatedFactory::class,
ErrorHandlerManager::class => ErrorHandlerManagerFactory::class,
Template\TemplateRendererInterface::class => Twig\TwigRendererFactory::class,
],
'aliases' => [
Router\RouterInterface::class => Router\FastRouteRouter::class,
'Zend\Expressive\FinalHandler' => ContentBasedErrorHandler::class,
],
],

View file

@ -1,27 +1,29 @@
<?php
namespace Shlinkio\Shlink\Common\Expressive;
use Psr\Http\Message\ServerRequestInterface as Request;
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\Exception\InvalidServiceException;
class ContentBasedErrorHandler extends AbstractPluginManager implements ErrorHandlerInterface
class ContentBasedErrorHandler implements ErrorHandlerInterface
{
const DEFAULT_CONTENT = 'text/html';
public function validate($instance)
{
if (is_callable($instance)) {
return;
}
/**
* @var ErrorHandlerManagerInterface
*/
private $errorHandlerManager;
throw new InvalidServiceException(sprintf(
'Only callables are valid plugins for "%s". "%s" provided',
__CLASS__,
is_object($instance) ? get_class($instance) : gettype($instance)
));
/**
* ContentBasedErrorHandler constructor.
* @param ErrorHandlerManagerInterface|ErrorHandlerManager $errorHandlerManager
*
* @Inject({ErrorHandlerManager::class})
*/
public function __construct(ErrorHandlerManagerInterface $errorHandlerManager)
{
$this->errorHandlerManager = $errorHandlerManager;
}
/**
@ -50,11 +52,11 @@ class ContentBasedErrorHandler extends AbstractPluginManager implements ErrorHan
$accepts = $request->hasHeader('Accept') ? $request->getHeaderLine('Accept') : self::DEFAULT_CONTENT;
$accepts = explode(',', $accepts);
foreach ($accepts as $accept) {
if (! $this->has($accept)) {
if (! $this->errorHandlerManager->has($accept)) {
continue;
}
return $this->get($accept);
return $this->errorHandlerManager->get($accept);
}
throw new InvalidArgumentException(sprintf(

View file

@ -0,0 +1,21 @@
<?php
namespace Shlinkio\Shlink\Common\Expressive;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\Exception\InvalidServiceException;
class ErrorHandlerManager extends AbstractPluginManager implements ErrorHandlerManagerInterface
{
public function validate($instance)
{
if (is_callable($instance)) {
return;
}
throw new InvalidServiceException(sprintf(
'Only callables are valid plugins for "%s". "%s" provided',
__CLASS__,
is_object($instance) ? get_class($instance) : gettype($instance)
));
}
}

View file

@ -7,7 +7,7 @@ use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\Factory\FactoryInterface;
class ContentBasedErrorHandlerFactory implements FactoryInterface
class ErrorHandlerManagerFactory implements FactoryInterface
{
/**
* Create an object
@ -25,6 +25,6 @@ class ContentBasedErrorHandlerFactory implements FactoryInterface
{
$config = $container->get('config')['error_handler'];
$plugins = isset($config['plugins']) ? $config['plugins'] : [];
return new ContentBasedErrorHandler($container, $plugins);
return new ErrorHandlerManager($container, $plugins);
}
}

View file

@ -0,0 +1,9 @@
<?php
namespace Shlinkio\Shlink\Common\Expressive;
use Interop\Container\ContainerInterface;
interface ErrorHandlerManagerInterface extends ContainerInterface
{
}