· 5 years ago · Oct 03, 2020, 10:18 PM
1<?php
2
3namespace App\Helpers;
4
5use App\User;
6use GuzzleHttp\Client;
7use Illuminate\Support\Facades\Log;
8
9class ConnectHelper
10{
11
12 protected $client;
13 protected $baseUrl;
14
15 public function __construct()
16 {
17 $this->client = $this->client();
18 $this->baseUrl = env('VATSIM_OAUTH_BASE', 'https://auth.vatsim.net');
19 }
20
21 protected function client() {
22 return new Client([
23 'base_uri' => $this->baseUrl,
24 'headers' => [
25 'Content-type' => 'application/json',
26 ],
27 ]);
28 }
29
30 public function fetchUser(User $user) {
31
32 try {
33 $response = $this->client->get($this->baseUrl . '/api/user', [
34 'headers' => [
35 'Authorization' => 'Bearer ' . $user->access_token,
36 ],
37 ]);
38
39 return json_decode($response->getBody());
40 } catch(\Exception $exception) {
41 Log::critical($exception->getMessage());;
42 }
43
44 }
45
46 public function refreshToken(User $user) {
47 try {
48 $response = $this->client->post($this->baseUrl . '/oauth/token', [
49 'form_params' => [
50 'grant_type' => 'refresh_token',
51 'refresh_token' => $user->refresh_token,
52 'client_id' => env('VATSIM_OAUTH_CLIENT'),
53 'client_secret' => env('VATSIM_OAUTH_SECRET'),
54 'scope' => str_replace(',', ' ', env('VATSIM_OAUTH_SCOPES')),
55 ]
56 ]);
57
58 if ($response->getStatusCode() != 200) {
59 return false;
60 }
61
62 return json_decode($response->getBody());
63 } catch(\Exception $exception) {
64 Log::critical($exception->getMessage());
65 }
66
67 }
68}