Completed
Push — master ( 55f4d0...97c563 )
by PROSPER
03:11
created

LinkedInController::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
7
use Auth;
8
use Session;
9
use App\Http\Requests;
10
use App\Http\Controllers\Controller;
11
use LinkedIn\LinkedIn;
12
use GuzzleHttp\Client;
13
14
class LinkedInController extends Controller
15
{
16
     /**
17
     * LinkedIn API base Url
18
     * @var string
19
     */
20
    protected $baseUrl;
21
22
    /**
23
     * Instance of Client
24
     * @var object
25
     */
26
    protected $client;
27
28
    /**
29
     *  Response from requests made to LinkedIn
30
     * @var mixed
31
     */
32
    protected $response;
33
34
     /**
35
     * Initialize the Controller with necessary arguments
36
     */
37
    public function __construct()
38
    {
39
        $this->baseUrl = 'https://api.linkedin.com/v1';
40
        $this->client = new Client(['base_uri' => $this->baseUrl]);
41
    }
42
43
     /**
44
     * Set options for making the Client request
45
     * @return  void
46
     */
47
    private function setRequestOptions()
48
    {
49
        $authBearer = 'Bearer '. Auth::user()->getAccessToken();
50
        $this->client = new Client(['base_uri' => $this->baseUrl,
51
            'headers' => [
52
                'Authorization' => $authBearer,
53
                'Content-Type'  => 'application/json',
54
                'Accept'        => 'application/json'
55
        ]]);
56
    }
57
58
    /**
59
     * Get the response from New York times API
60
     * @param string $relativeUrl
61
     */
62
    private function setGetResponse($relativeUrl)
63
    {
64
        $this->response = $this->client->get($this->baseUrl . $relativeUrl, []);
65
    }
66
67
    /**
68
     * Get the whole response from a get operation
69
     * @return array
70
     */
71
    private function getResponse()
72
    {
73
        return json_decode($this->response->getBody(), true);
74
    }
75
76
    /**
77
     * Get the data response from a get operation
78
     * @return array
79
     */
80
    private function getData()
81
    {
82
        return $this->getResponse();
83
    }
84
85
    /**
86
     * Return all data to the LinkedIn API dashboard
87
     * @return mixed
88
     */
89
    public function getPage()
90
    {
91
        if (Session::get('provider') !== 'linkedin') {
92
            Auth::logout();
93
94
            Session::flush();
95
96
            return redirect('/auth/linkedin');
97
        }
98
99
        $this->setRequestOptions();
100
101
        $relativeUrl = '/people/~:(firstName,lastName,emailAddress,pictureUrl,location,industry,numConnections,numConnectionsCapped,summary,publicProfileUrl)?format=json';
102
103
        $this->setGetResponse($relativeUrl);
104
105
        $userDetails = $this->getData();
106
107
        return view('api.linkedin')->withDetails($userDetails);
108
    }
109
}
110
111