From ddaa4d6a38d26d2792d64454c404511f2f778c36 Mon Sep 17 00:00:00 2001 From: Nic Date: Tue, 11 Jun 2019 15:46:50 +0100 Subject: [PATCH] Can now update a user resource! (added BodyParamsMiddleware to the pipeline). --- config/pipeline.php | 1 + src/User/src/ConfigProvider.php | 5 +- src/User/src/Handler/UpdateHandler.php | 79 ++++++++++++++++++- src/User/src/Handler/UpdateHandlerFactory.php | 27 ++++++- 4 files changed, 109 insertions(+), 3 deletions(-) diff --git a/config/pipeline.php b/config/pipeline.php index 8b32144..cc8a391 100644 --- a/config/pipeline.php +++ b/config/pipeline.php @@ -24,6 +24,7 @@ $app->pipe(ErrorHandler::class); $app->pipe(Zend\ProblemDetails\ProblemDetailsMiddleware::class); $app->pipe(ServerUrlMiddleware::class); + $app->pipe(Zend\Expressive\Helper\BodyParams\BodyParamsMiddleware::class); // Pipe more middleware here that you want to execute on every request: // - bootstrapping diff --git a/src/User/src/ConfigProvider.php b/src/User/src/ConfigProvider.php index 1bdc828..0afd4ab 100644 --- a/src/User/src/ConfigProvider.php +++ b/src/User/src/ConfigProvider.php @@ -71,7 +71,10 @@ public function getRoutes() : array [ 'name' => 'user.update', 'path' => '/user/{id}', - 'middleware' => Handler\UpdateHandler::class, + 'middleware' => [ + Middleware\UserFetchMiddleware::class, + Handler\UpdateHandler::class, + ], 'allowed_methods' => ['PUT'], ], [ diff --git a/src/User/src/Handler/UpdateHandler.php b/src/User/src/Handler/UpdateHandler.php index 8721a56..0ffb6a6 100644 --- a/src/User/src/Handler/UpdateHandler.php +++ b/src/User/src/Handler/UpdateHandler.php @@ -4,14 +4,91 @@ namespace User\Handler; +use Exception; +use User\Entity\User; +use User\Filter\UserFilter; +use User\Service\UserService; use Psr\Http\Message\ResponseInterface; +use Zend\Diactoros\Response\EmptyResponse; +use Zend\Expressive\Hal\ResourceGenerator; +use Zend\Expressive\Hal\HalResponseFactory; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; +use Zend\ProblemDetails\ProblemDetailsResponseFactory; +/** + * Class UpdateHandler + * @package User\Handler + */ class UpdateHandler implements RequestHandlerInterface { + /** @var UserService $userService */ + private $userService; + + /** @var UserFilter $userFilter */ + private $userFilter; + + /** @var ResourceGenerator $resourceGenerator */ + private $resourceGenerator; + + /** @var HalResponseFactory $halResponseFactory */ + private $halResponseFactory; + + /** @var ProblemDetailsResponseFactory $problemDetailsResponseFactory */ + private $problemDetailsResponseFactory; + + /** + * UpdateHandler constructor. + * @param UserService $userService + * @param UserFilter $userFilter + * @param ResourceGenerator $resourceGenerator + * @param HalResponseFactory $halResponseFactory + * @param ProblemDetailsResponseFactory $problemDetailsResponseFactory + */ + public function __construct( + UserService $userService, + UserFilter $userFilter, + ResourceGenerator $resourceGenerator, + HalResponseFactory $halResponseFactory, + ProblemDetailsResponseFactory $problemDetailsResponseFactory + ) { + $this->userService = $userService; + $this->resourceGenerator = $resourceGenerator; + $this->halResponseFactory = $halResponseFactory; + $this->problemDetailsResponseFactory = $problemDetailsResponseFactory; + $this->userFilter = $userFilter; + } + + /** + * @param ServerRequestInterface $request + * @return ResponseInterface + */ public function handle(ServerRequestInterface $request) : ResponseInterface { - // Create and return a response + /** @var User $user */ + $user = $request->getAttribute(User::class); + $this->userFilter->setData($request->getParsedBody()); + + if (! $this->userFilter->isValid()) { + return $this->problemDetailsResponseFactory->createResponse( + $request, + 400, + 'Validation error occurred', + 'Bad Request', + 'https://httpstatus.es/400', + ['errors' => $this->userFilter->getMessages()] + ); + } + + try { + $this->userService->update($user, $this->userFilter->getValues()); + } catch (Exception $e) { + return $this->problemDetailsResponseFactory->createResponseFromThrowable( + $request, + $e + ); + } + + return new EmptyResponse(200); } } diff --git a/src/User/src/Handler/UpdateHandlerFactory.php b/src/User/src/Handler/UpdateHandlerFactory.php index 0ed282d..1ceff32 100644 --- a/src/User/src/Handler/UpdateHandlerFactory.php +++ b/src/User/src/Handler/UpdateHandlerFactory.php @@ -4,12 +4,37 @@ namespace User\Handler; +use User\Filter\UserFilter; +use User\Service\UserService; use Psr\Container\ContainerInterface; +use Zend\Expressive\Hal\ResourceGenerator; +use Zend\Expressive\Hal\HalResponseFactory; +use Zend\ProblemDetails\ProblemDetailsResponseFactory; +/** + * Class UpdateHandlerFactory + * @package User\Handler + */ class UpdateHandlerFactory { + /** + * @param ContainerInterface $container + * @return UpdateHandler + */ public function __invoke(ContainerInterface $container) : UpdateHandler { - return new UpdateHandler(); + $userService = $container->get(UserService::class); + $userFilter = $container->get('InputFilterManager')->get(UserFilter::class); + $resourceGenerator = $container->get(ResourceGenerator::class); + $halResponseFactory = $container->get(HalResponseFactory::class); + $problemDetailsResponseFactory = $container->get(ProblemDetailsResponseFactory::class); + + return new UpdateHandler( + $userService, + $userFilter, + $resourceGenerator, + $halResponseFactory, + $problemDetailsResponseFactory + ); } }