LinkedInController::getPage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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