forked from stefanotorresi/frameworkless-php-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToDoDelete.php
50 lines (41 loc) · 1.25 KB
/
ToDoDelete.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php declare(strict_types=1);
namespace Acme\ToDo\Http\RouteHandler;
use League\Route\Http;
use Acme\ToDo\Http\RouteHandler;
use Acme\ToDo\Model\ToDoDataMapper;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Ramsey\Uuid\Uuid;
use Laminas\Diactoros\Response\EmptyResponse;
class ToDoDelete implements RouteHandler
{
/**
* @var ToDoDataMapper
*/
private $todoDataMapper;
public function __construct(ToDoDataMapper $todoDataMapper)
{
$this->todoDataMapper = $todoDataMapper;
}
/**
* @param Request $request
* @param string[] $args
*
* @return Response
*
* @throws Http\Exception\BadRequestException
* @throws Http\Exception\NotFoundException
*/
public function __invoke(Request $request, array $args): Response
{
if (! Uuid::isValid($args['id'])) {
throw new Http\Exception\BadRequestException('Invalid UUID');
}
$item = $this->todoDataMapper->find($args['id']);
if ($item === null) {
throw new Http\Exception\NotFoundException('Resource not found');
}
$this->todoDataMapper->delete($args['id']);
return new EmptyResponse();
}
}