Commit 37e956af authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Service RedirectResponse fabricant une Response à partir du paramètre GET/POST...

Service RedirectResponse fabricant une Response à partir du paramètre GET/POST 'redirect' trouvée dans la requête
parent 76dfbbc4
Pipeline #10149 failed with stages
in 34 seconds
......@@ -3,6 +3,8 @@
namespace UnicaenApp;
use UnicaenApp\Controller\CacheControllerFactory;
use UnicaenApp\Controller\ConsoleController;
use UnicaenApp\Controller\ConsoleControllerFactory;
use UnicaenApp\Controller\InstadiaControllerFactory;
use UnicaenApp\Form\View\Helper\FormControlGroup;
use UnicaenApp\Form\View\Helper\FormControlGroupFactory;
......@@ -10,11 +12,15 @@ use UnicaenApp\HostLocalization\HostLocalization;
use UnicaenApp\HostLocalization\HostLocalizationFactory;
use UnicaenApp\Message\View\Helper\MessageHelper;
use UnicaenApp\Message\View\Helper\MessageHelperFactory;
use UnicaenApp\Mvc\RedirectResponse;
use UnicaenApp\Mvc\RedirectResponseFactory;
use UnicaenApp\ORM\Query\Functions\CompriseEntre;
use UnicaenApp\ORM\Query\Functions\PasHistorise;
use UnicaenApp\Service\InstadiaServiceFactory;
use UnicaenApp\Service\Mailer\MailerService;
use UnicaenApp\Service\Mailer\MailerServiceFactory;
use UnicaenApp\Service\SQL\RunSQLService;
use UnicaenApp\Service\SQL\RunSQLServiceFactory;
use UnicaenApp\ServiceManager\ServiceLocatorAwareInitializer;
use UnicaenApp\View\Helper\AppInfos;
use UnicaenApp\View\Helper\AppInfosFactory;
......@@ -36,12 +42,8 @@ use UnicaenApp\View\Helper\QueryParams;
use UnicaenApp\View\Helper\QueryParamsHelperFactory;
use UnicaenApp\View\Helper\Upload\UploaderHelper;
use UnicaenApp\View\Helper\Upload\UploaderHelperFactory;
use UnicaenApp\View\Helper\UserProfileSelectFactory;
use UnicaenApp\View\Helper\UserProfileSelect;
use UnicaenApp\Controller\ConsoleController;
use UnicaenApp\Controller\ConsoleControllerFactory;
use UnicaenApp\Service\SQL\RunSQLService;
use UnicaenApp\Service\SQL\RunSQLServiceFactory;
use UnicaenApp\View\Helper\UserProfileSelectFactory;
use Zend\Mvc\Console\Router\Simple;
return [
......@@ -329,6 +331,7 @@ return [
RunSQLService::class => RunSQLServiceFactory::class,
'instadia' => InstadiaServiceFactory::class,
HostLocalization::class => HostLocalizationFactory::class,
RedirectResponse::class => RedirectResponseFactory::class,
],
'shared' => [
'MouchardListenerErrorHandler' => false,
......
<?php
namespace UnicaenApp\Mvc;
use Zend\Http\PhpEnvironment\Request;
use Zend\Http\PhpEnvironment\Response;
use Zend\Mvc\Application;
use Zend\Router\Exception;
use Zend\Router\RouteInterface;
/**
* Buils a redirect response based on the current routing and parameters
*/
class RedirectResponse
{
/** @var RouteInterface */
private $router;
/** @var Application */
private $application;
/**
* @param Application $application
* @param RouteInterface $router
*/
public function __construct(Application $application, RouteInterface $router)
{
$this->router = $router;
$this->application = $application;
}
/**
* @param string $defaultRedirect
* @return Response
*/
public function __invoke(string $defaultRedirect): Response
{
$redirect = $this->getRedirect($defaultRedirect);
/** @var Response $response */
$response = $this->application->getResponse();
$response->getHeaders()->addHeaderLine('Location', $redirect);
$response->setStatusCode(302);
return $response;
}
/**
* @param string $defaultRedirect
* @return string|null
*/
protected function getRedirect(string $defaultRedirect): ?string
{
$redirect = $this->getRedirectFromRequest() ?: $defaultRedirect;
if ($this->routeExists($redirect)) {
return $this->router->assemble([], ['name' => $redirect]);
}
return $redirect;
}
/**
* @return string
*/
private function getRedirectFromRequest(): ?string
{
/** @var Request $request */
$request = $this->application->getRequest();
if ($redirectFromQuery = $request->getQuery('redirect')) {
return $redirectFromQuery;
}
if ($redirectFromPost = $request->getPost('redirect')) {
return $redirectFromPost;
}
return null;
}
/**
* @param string $route
* @return bool
*/
private function routeExists(string $route): bool
{
try {
$this->router->assemble([], ['name' => $route]);
} catch (Exception\RuntimeException $e) {
return false;
}
return true;
}
}
<?php
namespace UnicaenApp\Mvc;
use Interop\Container\ContainerInterface;
use UnicaenApp\Mvc\RedirectResponse;
use Zend\Mvc\Application;
use Zend\Router\RouteInterface;
class RedirectResponseFactory
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): RedirectResponse
{
/* @var RouteInterface $router */
$router = $container->get('Router');
/* @var Application $application */
$application = $container->get('Application');
return new RedirectResponse($application, $router);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment