Skip to content

Commit

Permalink
Can now update a user resource! (added BodyParamsMiddleware to the pi…
Browse files Browse the repository at this point in the history
…peline).
  • Loading branch information
Nic committed Jun 11, 2019
1 parent 608b52f commit ddaa4d6
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 3 deletions.
1 change: 1 addition & 0 deletions config/pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

This comment has been minimized.

Copy link
@nicdavies

nicdavies Jun 11, 2019

Owner

Ran into this issue when trying to call getParsedBody() from the ServerRequestInterface, it was returning empty.
Here's the solution: zendframework/zend-expressive#371


// Pipe more middleware here that you want to execute on every request:
// - bootstrapping
Expand Down
5 changes: 4 additions & 1 deletion src/User/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
],
[
Expand Down
79 changes: 78 additions & 1 deletion src/User/src/Handler/UpdateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
27 changes: 26 additions & 1 deletion src/User/src/Handler/UpdateHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}

0 comments on commit ddaa4d6

Please sign in to comment.