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

Размер файла: 5.05Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Controllers\User;
  6.  
  7. use App\Controllers\Controller;
  8. use App\Models\User;
  9. use App\Services\Session;
  10. use App\Services\Validator;
  11. use App\Services\View;
  12. use Intervention\Image\Constraint;
  13. use Intervention\Image\ImageManager;
  14. use Psr\Http\Message\ResponseInterface as Response;
  15. use Psr\Http\Message\ServerRequestInterface as Request;
  16.  
  17. /**
  18. * ProfileController
  19. */
  20. class ProfileController extends Controller
  21. {
  22. protected User $user;
  23.  
  24. public function __construct(
  25. protected View $view,
  26. protected Session $session,
  27. protected Validator $validator,
  28. ) {
  29. $this->user = getUser();
  30. }
  31.  
  32. /**
  33. * Profile
  34. *
  35. * @param Response $response
  36. *
  37. * @return Response
  38. */
  39. public function index(Response $response): Response
  40. {
  41. return $this->view->render(
  42. $response,
  43. 'profile/profile',
  44. ['user' => $this->user],
  45. );
  46. }
  47.  
  48. /**
  49. * Store
  50. *
  51. * @param Request $request
  52. * @param Response $response
  53. * @param ImageManager $manager
  54. *
  55. * @return Response
  56. */
  57. public function store(
  58. Request $request,
  59. Response $response,
  60. ImageManager $manager,
  61. ): Response {
  62. $input = (array) $request->getParsedBody();
  63. $files = $request->getUploadedFiles();
  64. $input = array_merge($input, $files);
  65.  
  66. $this->validator
  67. ->required(['csrf', 'email'])
  68. ->same('csrf', $this->session->get('csrf'), 'Неверный идентификатор сессии, повторите действие!')
  69. ->length('email', 5, 100)
  70. ->email('email')
  71. ->length('name', 3, 20)
  72. ->file('picture', [
  73. 'size_max' => setting('file.size_max'),
  74. 'weight_max' => setting('image.weight_max'),
  75. 'weight_min' => setting('image.weight_min'),
  76. ]);
  77.  
  78. if ($this->validator->isValid($input)) {
  79. if ($input['picture']->getError() === UPLOAD_ERR_OK) {
  80. // Удаляем старое фото
  81. if ($this->user->picture && file_exists(publicPath($this->user->picture))) {
  82. unlink(publicPath($this->user->picture));
  83. }
  84.  
  85. if ($this->user->avatar && file_exists(publicPath($this->user->avatar))) {
  86. unlink(publicPath($this->user->avatar));
  87. }
  88.  
  89. $extension = getExtension($input['picture']->getClientFilename());
  90. $picturePath = '/uploads/pictures/' . uniqueName($extension);
  91.  
  92. $img = $manager->make($input['picture']->getFilePath());
  93. $img->resize(setting('image.resize'), setting('image.resize'), static function (Constraint $constraint) {
  94. $constraint->aspectRatio();
  95. $constraint->upsize();
  96. });
  97.  
  98. $img->save(publicPath($picturePath));
  99.  
  100. $avatarPath = '/uploads/avatars/' . uniqueName('png');
  101. $img = $manager->make($input['picture']->getFilePath());
  102. $img->fit(64);
  103. $img->save(publicPath($avatarPath));
  104.  
  105. $this->user->update([
  106. 'picture' => $picturePath,
  107. 'avatar' => $avatarPath,
  108. ]);
  109. }
  110.  
  111. $this->user->update([
  112. 'email' => sanitize($input['email']),
  113. 'name' => sanitize($input['name']),
  114. ]);
  115.  
  116. $this->session->set('flash', ['success' => 'Данные успешно изменены!']);
  117. } else {
  118. $this->session->set('flash', ['errors' => $this->validator->getErrors(), 'old' => $input]);
  119. }
  120.  
  121. return $this->redirect($response, '/profile');
  122. }
  123.  
  124. /**
  125. * Delete photo
  126. *
  127. * @param Request $request
  128. * @param Response $response
  129. * @return Response
  130. */
  131. public function deletePhoto(Request $request, Response $response): Response
  132. {
  133. $input = (array) $request->getParsedBody();
  134. $this->validator
  135. ->required('csrf')
  136. ->same('csrf', $this->session->get('csrf'), 'Неверный идентификатор сессии, повторите действие!');
  137.  
  138. if ($this->validator->isValid($input)) {
  139. // Удаляем старое фото
  140. if ($this->user->picture && file_exists(publicPath($this->user->picture))) {
  141. unlink(publicPath($this->user->picture));
  142. }
  143.  
  144. if ($this->user->avatar && file_exists(publicPath($this->user->avatar))) {
  145. unlink(publicPath($this->user->avatar));
  146. }
  147.  
  148. $this->user->update([
  149. 'picture' => '',
  150. 'avatar' => '',
  151. ]);
  152.  
  153. $this->session->set('flash', ['success' => 'Фото успешно удалено!']);
  154. } else {
  155. $this->session->set('flash', ['errors' => $this->validator->getErrors()]);
  156. }
  157.  
  158. return $this->redirect($response, '/profile');
  159. }
  160. }