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

Размер файла: 3.09Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Http\Controllers;
  6.  
  7. use App\Classes\Validator;
  8. use App\Models\Social;
  9. use App\Models\User;
  10. use Exception;
  11. use GuzzleHttp\Client;
  12. use GuzzleHttp\Exception\GuzzleException;
  13. use Illuminate\Http\RedirectResponse;
  14. use Illuminate\Http\Request;
  15. use Illuminate\View\View;
  16.  
  17. class SocialController extends Controller
  18. {
  19. /**
  20. * @var User
  21. */
  22. public $user;
  23.  
  24. /**
  25. * Конструктор
  26. */
  27. public function __construct()
  28. {
  29. $this->middleware('check.user');
  30.  
  31. $this->middleware(function ($request, $next) {
  32. $this->user = getUser();
  33.  
  34. return $next($request);
  35. });
  36. }
  37.  
  38. /**
  39. * Главная страница
  40. *
  41. * @param Request $request
  42. *
  43. * @return View|RedirectResponse
  44. * @throws GuzzleException
  45. */
  46. public function index(Request $request)
  47. {
  48. if ($request->isMethod('post')) {
  49. $client = new Client(['timeout' => 30.0]);
  50.  
  51. $response = $client->get('//ulogin.ru/token.php', [
  52. 'query' => [
  53. 'token' => $request->input('token'),
  54. 'host' => $_SERVER['HTTP_HOST'],
  55. ]
  56. ]);
  57.  
  58. if ($response->getStatusCode() === 200) {
  59. $network = json_decode($response->getBody()->getContents());
  60.  
  61. $social = Social::query()
  62. ->where('network', $network->network)
  63. ->where('uid', $network->uid)
  64. ->first();
  65.  
  66. if (! $social) {
  67. Social::query()->create([
  68. 'user_id' => $this->user->id,
  69. 'network' => $network->network,
  70. 'uid' => $network->uid,
  71. 'created_at' => SITETIME,
  72. ]);
  73.  
  74. setFlash('success', __('socials.success_binding'));
  75. } else {
  76. setFlash('danger', __('socials.already_binding'));
  77. }
  78.  
  79. return redirect('socials');
  80. }
  81.  
  82. setFlash('danger', __('socials.failed_binding'));
  83. }
  84.  
  85. $socials = Social::query()
  86. ->where('user_id', $this->user->id)
  87. ->get();
  88.  
  89. return view('socials/index', compact('socials'));
  90. }
  91.  
  92. /**
  93. * Удаление привязки
  94. *
  95. * @param int $id
  96. * @param Request $request
  97. * @param Validator $validator
  98. *
  99. * @return RedirectResponse
  100. */
  101. public function delete(int $id, Request $request, Validator $validator): RedirectResponse
  102. {
  103. $social = Social::query()->where('user_id', $this->user->id)->find($id);
  104.  
  105. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
  106. ->notEmpty($social, __('socials.not_found_binding'));
  107.  
  108. if ($validator->isValid()) {
  109. $social->delete();
  110.  
  111. setFlash('success', __('socials.success_deleted'));
  112. } else {
  113. setFlash('danger', $validator->getErrors());
  114. }
  115.  
  116. return redirect('socials');
  117. }
  118. }