GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( fec26d...4e4046 )
by Dirk
05:35
created

PinterestOAuth   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 51.61%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 144
ccs 16
cts 31
cp 0.5161
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getLoginUrl() 0 14 1
A generateState() 0 4 1
A getState() 0 4 1
A setState() 0 4 1
A getOAuthToken() 0 15 1
A setOAuthToken() 0 4 1
1
<?php
2
/**
3
 * Copyright 2015 Dirk Groenen
4
 *
5
 * (c) Dirk Groenen <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace DirkGroenen\Pinterest\Auth;
12
13
use DirkGroenen\Pinterest\Transport\Request;
14
use DirkGroenen\Pinterest\Exceptions\PinterestException;
15
16
class PinterestOAuth {
17
18
    /**
19
     * The application ID
20
     *
21
     * @var string
22
     */
23
    private $client_id;
24
25
    /**
26
     * The app secret
27
     *
28
     * @var string
29
     */
30
    private $client_secret;
31
32
    /**
33
     * Random string indicating the state
34
     * to prevent spoofing
35
     *
36
     * @var void
37
     */
38
    private $state;
39
40
    /**
41
     * A reference to the request instance
42
     *
43
     * @var Request
44
     */
45
    private $request;
46
47
    /**
48
     * Pinterest's oauth endpoint
49
     */
50
    const AUTH_HOST = "https://api.pinterest.com/oauth/";
51
52
    /**
53
     * Construct
54
     *
55
     * @param  string   $client_id
56
     * @param  string   $client_secret
57
     * @param  Request  $request
58
     */
59 39
    public function __construct($client_id, $client_secret, $request)
60
    {
61 39
        $this->client_id = $client_id;
62 39
        $this->client_secret = $client_secret;
63
64
        // Generate and set the state
65 39
        $this->state = $this->generateState();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->generateState() of type string is incompatible with the declared type null of property $state.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
66
67
        // Set request instance
68 39
        $this->request = $request;
69 39
    }
70
71
    /**
72
     * Returns the login url
73
     *
74
     * @access public
75
     * @param  array    $scopes
76
     * @param  string   $redirect_uri
77
     * @return string
78
     */
79
    public function getLoginUrl($redirect_uri, $scopes = array("read_public"), $response_type = "code")
80
    {
81
        $queryparams = array(
82
            "response_type"     => $response_type,
83
            "redirect_uri"      => $redirect_uri,
84
            "client_id"         => $this->client_id,
85
            "client_secret"     => $this->client_secret,
86
            "scope"             => implode(",", $scopes),
87
            "state"             => $this->state
88
        );
89
90
        // Build url and return it
91
        return sprintf("%s?%s", self::AUTH_HOST, http_build_query($queryparams));
92
    }
93
94
    /**
95
     * Generates a random string and returns is
96
     *
97
     * @access private
98
     * @return string       random string
99
     */
100 39
    private function generateState()
101
    {
102 39
        return substr(md5(rand()), 0, 7);
103
    }
104
105
    /**
106
     * Get the generated state
107
     *
108
     * @return string
109
     */
110 2
    public function getState()
111
    {
112 2
        return $this->state;
113
    }
114
115
    /**
116
     * Set a state manually
117
     *
118
     * @param  string    $state
119
     * @return void
120
     */
121 1
    public function setState($state)
122
    {
123 1
        $this->state = $state;
0 ignored issues
show
Documentation Bug introduced by
It seems like $state of type string is incompatible with the declared type null of property $state.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
124 1
    }
125
126
    /**
127
     * Change the code for an access_token
128
     *
129
     * @param  string   $code
130
     * @return \DirkGroenen\Pinterest\Transport\Response
131
     */
132
    public function getOAuthToken($code)
133
    {
134
        // Build data array
135
        $data = array(
136
            "grant_type"    => "authorization_code",
137
            "client_id"     => $this->client_id,
138
            "client_secret" => $this->client_secret,
139
            "code"          => $code
140
        );
141
142
        // Perform post request
143
        $response = $this->request->post("oauth/token", $data);
144
145
        return $response;
146
    }
147
148
    /**
149
     * Set the access_token for further requests
150
     *
151
     * @access public
152
     * @param  string   $access_token
153
     * @return void
154
     */
155 39
    public function setOAuthToken($access_token)
156
    {
157 39
        $this->request->setAccessToken($access_token);
158
    }
159
}