Просмотр файла app/Controllers/GuestbookController.php

Размер файла: 5.94Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Controllers;
  6.  
  7. use App\Models\Guestbook;
  8. use App\Repositories\GuestbookRepository;
  9. use App\Services\Session;
  10. use App\Services\Validator;
  11. use App\Services\View;
  12. use Psr\Http\Message\ResponseInterface as Response;
  13. use Psr\Http\Message\ServerRequestInterface as Request;
  14.  
  15. /**
  16. * GuestbookController
  17. */
  18. class GuestbookController extends Controller
  19. {
  20. public function __construct(
  21. protected View $view,
  22. protected Session $session,
  23. protected Validator $validator,
  24. protected GuestbookRepository $guestbookRepository,
  25. ) {}
  26.  
  27. /**
  28. * Index
  29. *
  30. * @param Response $response
  31. *
  32. * @return Response
  33. */
  34. public function index(Response $response): Response
  35. {
  36. $messages = $this->guestbookRepository->getMessages(setting('guestbook.per_page'));
  37.  
  38. return $this->view->render(
  39. $response,
  40. 'guestbook/index',
  41. compact('messages')
  42. );
  43. }
  44.  
  45. /**
  46. * Store
  47. *
  48. * @param Request $request
  49. * @param Response $response
  50. *
  51. * @return Response
  52. */
  53. public function store(
  54. Request $request,
  55. Response $response,
  56. ): Response {
  57. $user = getUser();
  58.  
  59. if (! $user && ! setting('guestbook.allow_guests')) {
  60. abort(403, 'Доступ запрещен!');
  61. }
  62.  
  63. $input = (array) $request->getParsedBody();
  64.  
  65. $this->validator
  66. ->required(['csrf', 'text'])
  67. ->same('csrf', $this->session->get('csrf'), 'Неверный идентификатор сессии, повторите действие!')
  68. ->length('text', setting('guestbook.text_min_length'), setting('guestbook.text_max_length'));
  69.  
  70. if (! $user) {
  71. $this->validator
  72. ->required('captcha')
  73. ->length('name', setting('guestbook.name_min_length'), setting('guestbook.name_max_length'))
  74. ->same('captcha', $this->session->get('captcha'), 'Не удалось пройти проверку captcha!');
  75. }
  76.  
  77. if ($this->validator->isValid($input)) {
  78. if (! $user) {
  79. $name = isset($input['name']) ? sanitize($input['name']) : setting('main.guest_name');
  80. }
  81.  
  82. Guestbook::query()->create([
  83. 'user_id' => $user->id ?? null,
  84. 'text' => sanitize($input['text']),
  85. 'name' => $name ?? null,
  86. 'created_at' => time(),
  87. ]);
  88.  
  89. $this->session->set('flash', ['success' => 'Сообщение успешно добавлено!']);
  90. } else {
  91. $this->session->set('flash', ['errors' => $this->validator->getErrors(), 'old' => $input]);
  92. }
  93.  
  94. return $this->redirect($response, '/guestbook');
  95. }
  96.  
  97. /**
  98. * Edit
  99. *
  100. * @param int $id
  101. * @param Response $response
  102. *
  103. * @return Response
  104. */
  105. public function edit(int $id, Response $response): Response
  106. {
  107. if (! isAdmin()) {
  108. abort(403, 'Доступ запрещен!');
  109. }
  110.  
  111. $message = $this->guestbookRepository->getById($id);
  112. if (! $message) {
  113. abort(404, 'Сообщение не найдено');
  114. }
  115.  
  116. return $this->view->render(
  117. $response,
  118. 'guestbook/edit',
  119. compact('message')
  120. );
  121. }
  122.  
  123. /**
  124. * Store
  125. *
  126. * @param int $id
  127. * @param Request $request
  128. * @param Response $response
  129. *
  130. * @return Response
  131. */
  132. public function update(
  133. int $id,
  134. Request $request,
  135. Response $response,
  136. ): Response
  137. {
  138. if (! isAdmin()) {
  139. abort(403, 'Доступ запрещен!');
  140. }
  141.  
  142. $message = $this->guestbookRepository->getById($id);
  143. if (! $message) {
  144. abort(404, 'Сообщение не найдено');
  145. }
  146.  
  147. $input = (array) $request->getParsedBody();
  148.  
  149. $this->validator
  150. ->required(['csrf', 'text'])
  151. ->same('csrf', $this->session->get('csrf'), 'Неверный идентификатор сессии, повторите действие!')
  152. ->length('text', setting('guestbook.text_min_length'), setting('guestbook.text_max_length'));
  153.  
  154. if ($this->validator->isValid($input)) {
  155. $message->update([
  156. 'text' => sanitize($input['text']),
  157. ]);
  158. } else {
  159. $this->session->set('flash', ['errors' => $this->validator->getErrors(), 'old' => $input]);
  160.  
  161. return $this->redirect($response, '/guestbook/' . $id . '/edit');
  162. }
  163.  
  164. $this->session->set('flash', ['success' => 'Сообщение успешно изменено!']);
  165.  
  166. return $this->redirect($response, '/guestbook');
  167. }
  168.  
  169. /**
  170. * Destroy
  171. *
  172. * @param int $id
  173. * @param Request $request
  174. * @param Response $response
  175. *
  176. * @return Response
  177. */
  178. public function destroy(int $id, Request $request, Response $response): Response
  179. {
  180. if (! isAdmin()) {
  181. abort(403, 'Доступ запрещен!');
  182. }
  183.  
  184. $message = $this->guestbookRepository->getById($id);
  185. if (! $message) {
  186. abort(404, 'Сообщение не найдено');
  187. }
  188.  
  189. $input = (array) $request->getParsedBody();
  190.  
  191. $this->validator
  192. ->required('csrf')
  193. ->same('csrf', $this->session->get('csrf'), 'Неверный идентификатор сессии, повторите действие!');
  194.  
  195. if ($this->validator->isValid($input)) {
  196. $message->delete();
  197.  
  198. $this->session->set('flash', ['success' => 'Сообщение успешно удалено!']);
  199. } else {
  200. $this->session->set('flash', ['errors' => $this->validator->getErrors()]);
  201. }
  202.  
  203. return $this->redirect($response, '/guestbook');
  204. }
  205. }