1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LibraryCatalog\Controller\V1; |
6
|
|
|
|
7
|
|
|
use LibraryCatalog\Controller\TransformerException; |
8
|
|
|
use LibraryCatalog\Service\Acl; |
9
|
|
|
use LibraryCatalog\Service\Catalogue; |
10
|
|
|
use LibraryCatalog\Service\Validation\Rule\AuthorExists; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
use LibraryCatalog\Controller\V1\Transformer\BookWithAuthor as BookTransformer; |
13
|
|
|
use LibraryCatalog\Entity\Book as BookEntity; |
14
|
|
|
use Rakit\Validation\RuleQuashException; |
15
|
|
|
use Rakit\Validation\Validator; |
16
|
|
|
|
17
|
|
|
class Book extends AbstractController |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param string $uri |
21
|
|
|
* @param string $id |
22
|
|
|
* @return ResponseInterface |
23
|
|
|
* @throws \DI\DependencyException |
24
|
|
|
* @throws \DI\NotFoundException |
25
|
|
|
* @throws TransformerException |
26
|
|
|
*/ |
27
|
3 |
|
public function getOneHandler(string $uri, string $id): ResponseInterface |
28
|
|
|
{ |
29
|
|
|
// Check ACL first. |
30
|
|
|
if ( |
31
|
3 |
|
!$this->getAcl()->isAllowed( |
32
|
3 |
|
$this->getAcl()->parseUserPayload($this->getAuthIn()->getPayload()), |
|
|
|
|
33
|
3 |
|
Acl::BOOK, |
34
|
3 |
|
Acl::READ |
35
|
|
|
) |
36
|
|
|
) { |
37
|
1 |
|
return $this->forbiddenError($uri); |
38
|
|
|
} |
39
|
|
|
|
40
|
2 |
|
$book = $this->container->get('Catalogue')->fetchBook($id, true); |
41
|
|
|
|
42
|
2 |
|
if ($book) { |
43
|
1 |
|
$response = $this->createResponse((new BookTransformer())->transform($book)); |
44
|
|
|
} else { |
45
|
1 |
|
$response = $this->notFoundError($uri, 'Book not found'); |
46
|
|
|
} |
47
|
2 |
|
return $response; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $uri |
52
|
|
|
* @param array $params |
53
|
|
|
* @return ResponseInterface |
54
|
|
|
* @throws \DI\DependencyException |
55
|
|
|
* @throws \DI\NotFoundException |
56
|
|
|
* @throws TransformerException |
57
|
|
|
* @throws RuleQuashException |
58
|
|
|
*/ |
59
|
3 |
|
public function postOneHandler(string $uri, array $params): ResponseInterface |
60
|
|
|
{ |
61
|
|
|
// Check ACL first. |
62
|
|
|
if ( |
63
|
3 |
|
!$this->getAcl()->isAllowed( |
64
|
3 |
|
$this->getAcl()->parseUserPayload($this->getAuthIn()->getPayload()), |
|
|
|
|
65
|
3 |
|
Acl::BOOK, |
66
|
3 |
|
Acl::ADD |
67
|
|
|
) |
68
|
|
|
) { |
69
|
1 |
|
return $this->forbiddenError($uri); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** @var Catalogue $catalogue */ |
73
|
2 |
|
$catalogue = $this->container->get('Catalogue'); |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* In a simple project I do not want to add Request-Hydrators and do validation directly in controller. |
77
|
|
|
*/ |
78
|
|
|
|
79
|
2 |
|
$validator = new Validator(); |
80
|
2 |
|
$validator->addValidator('authorExists', new AuthorExists($catalogue->getAuthorRepository())); |
81
|
2 |
|
$validation = $validator->validate($params, [ |
82
|
2 |
|
'title' => 'required|min:3|max:4096', |
83
|
|
|
'summary' => 'max:65534', |
84
|
|
|
'authorId' => 'required|authorExists', |
85
|
|
|
]); |
86
|
|
|
|
87
|
2 |
|
if ($validation->fails()) { |
88
|
1 |
|
$response = $this->validationError($uri, $validation->errors()->firstOfAll()); |
89
|
|
|
} else { |
90
|
1 |
|
$book = (new BookEntity())->fill($params); |
91
|
1 |
|
$catalogue->createBook($book); |
92
|
1 |
|
$response = $this->createResponse((new BookTransformer())->transform($book)); |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
return $response; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|