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

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