protected function authVersion1(Request $request)
{
switch ($request->getMethod()) {
case $request::METHOD_GET:
$entity = $this->model->fetch();
if (!$entity) {
$response = $this->getResponse();
$response->setStatusCode(204);
return $response;
}
break;
case $request::METHOD_POST:
$entity = $this->model->create($this->bodyParams());
$response = $this->getResponse();
$response->setStatusCode(201);
$response->getHeaders()->addHeaderLine('Location', $this->plugin('Hal')->createLink($this->getRouteForEntity($entity)));
break;
case $request::METHOD_PATCH:
$entity = $this->model->update($this->bodyParams());
break;
case $request::METHOD_DELETE:
if ($this->model->remove()) {
return $this->getResponse()->setStatusCode(204);
}
return new ApiProblemResponse(new ApiProblem(404, 'No authentication configuration found'));
default:
return new ApiProblemResponse(new ApiProblem(405, 'Only the methods GET, POST, PATCH, and DELETE are allowed for this URI'));
}
$halEntity = new Entity($entity, null);
$halEntity->getLinks()->add(Link::factory(['rel' => 'self', 'route' => $this->getRouteForEntity($entity)]));
return new ViewModel(['payload' => $halEntity]);
}