|
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\AuthorUniqueName; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
use LibraryCatalog\Controller\V1\Transformer\AuthorWithBooks as AuthorTransformer; |
|
13
|
|
|
use LibraryCatalog\Entity\Author as AuthorEntity; |
|
14
|
|
|
use Rakit\Validation\RuleQuashException; |
|
15
|
|
|
use Rakit\Validation\Validator; |
|
16
|
|
|
|
|
17
|
|
|
class Author 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
|
4 |
|
public function getOneHandler(string $uri, string $id): ResponseInterface |
|
28
|
|
|
{ |
|
29
|
|
|
// Check ACL first. |
|
30
|
4 |
|
if (!$this->isAllowed(Acl::AUTHOR, Acl::READ)) { |
|
31
|
1 |
|
return $this->forbiddenError($uri); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
3 |
|
$author = $this->container->get('Catalogue')->fetchAuthor($id, true); |
|
35
|
|
|
|
|
36
|
3 |
|
if ($author) { |
|
37
|
2 |
|
$response = $this->createResponse((new AuthorTransformer())->transform($author)); |
|
38
|
|
|
} else { |
|
39
|
1 |
|
$response = $this->notFoundError($uri, 'Author not found'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
3 |
|
return $response; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $uri |
|
47
|
|
|
* @param array $params |
|
48
|
|
|
* @return ResponseInterface |
|
49
|
|
|
* @throws \DI\DependencyException |
|
50
|
|
|
* @throws \DI\NotFoundException |
|
51
|
|
|
* @throws TransformerException |
|
52
|
|
|
* @throws RuleQuashException |
|
53
|
|
|
*/ |
|
54
|
3 |
|
public function postOneHandler(string $uri, array $params): ResponseInterface |
|
55
|
|
|
{ |
|
56
|
|
|
// Check ACL first. |
|
57
|
3 |
|
if (!$this->isAllowed(Acl::AUTHOR, Acl::ADD)) { |
|
58
|
1 |
|
return $this->forbiddenError($uri); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** @var Catalogue $catalogue */ |
|
62
|
2 |
|
$catalogue = $this->container->get('Catalogue'); |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* In a simple project I do not want to add Request-Hydrators and do validation directly in controller. |
|
66
|
|
|
*/ |
|
67
|
|
|
|
|
68
|
2 |
|
$validator = new Validator(); |
|
69
|
2 |
|
$validator->addValidator('uniqueAuthor', new AuthorUniqueName($catalogue->getAuthorRepository(), $params['birthdate'] ?? '')); |
|
70
|
2 |
|
$validation = $validator->validate($params, [ |
|
71
|
2 |
|
'name' => 'required|min:3|max:255|uniqueAuthor', |
|
72
|
|
|
'birthdate' => 'required|date', |
|
73
|
|
|
'deathdate' => 'date', |
|
74
|
|
|
'biography' => 'max:65534', |
|
75
|
|
|
'summary' => 'max:65534', |
|
76
|
|
|
]); |
|
77
|
|
|
|
|
78
|
2 |
|
if ($validation->fails()) { |
|
79
|
1 |
|
$response = $this->validationError($uri, $validation->errors()->firstOfAll()); |
|
80
|
|
|
} else { |
|
81
|
1 |
|
$author = (new AuthorEntity())->fill($params); |
|
82
|
1 |
|
$catalogue->createAuthor($author); |
|
83
|
1 |
|
$response = $this->createResponse((new AuthorTransformer())->transform($author)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
2 |
|
return $response; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|