Instagram::getOAuth()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2016 Haridarshan Gorana
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
namespace Haridarshan\Instagram;
26
27
use Haridarshan\Instagram\Exceptions\InstagramException;
28
use InvalidArgumentException;
29
use stdClass;
30
31
/**
32
 * PHP Instagram API wrapper class
33
 *
34
 * @library			instagram-php
35
 *
36
 * @license 		https://opensource.org/licenses/MIT MIT
37
 *
38
 * @link			http://github.com/haridarshan/instagram-php Class Documentation
39
 * @link			http://instagram.com/developer/ API Documentation
40
 *
41
 * @author			Haridarshan Gorana 	<[email protected]>
42
 *
43
 * @since			May 09, 2016
44
 *
45
 * @copyright		Haridarshan Gorana
46
 *
47
 * @version			2.2.2
48
 */
49
class Instagram
50
{
51
    /** @var InstagramApp */
52
    protected $app;
53
54
    /** @var string */
55
    protected $callbackUrl;
56
57
    /** @var array */
58
    protected $defaultScopes = ['basic', 'public_content', 'follower_list', 'comments', 'relationships', 'likes'];
59
60
    /** @var array */
61
    protected $scopes = [];
62
63
    /**
64
     * Random string indicating the state to prevent spoofing
65
     *
66
     * @var string
67
     */
68
    protected $state;
69
70
    /** @var \GuzzleHttp\Client $client */
71
    protected $client;
72
73
    /** @var InstagramOAuth $oauthResponse */
74
    protected $oauthResponse;
75
76
    /**
77
     * Default Constructor
78
     * Instagram Configuration Data
79
     *
80
     * @param array $config
81
     *
82
     * @throws InstagramException|InvalidArgumentException
83
     */
84
    public function __construct(array $config = [])
85
    {
86
        if (!is_array($config)) {
87
            throw new InstagramException('Invalid Instagram Configuration data');
88
        }
89
90
        $this->app = new InstagramApp($config['ClientId'], $config['ClientSecret']);
91
92
        if (isset($config['Callback'])) {
93
            $this->setCallbackUrl($config['Callback']);
94
        }
95
        
96
        $this->state = isset($config['State']) ? $config['State'] : substr(md5(rand()), 0, 7);
97
98
        $this->client = HelperFactory::getInstance()->client(Constants::API_HOST);
99
    }
100
101
    /**
102
     * Returns InstagramApp entity.
103
     *
104
     * @return InstagramApp
105
     */
106
    public function getApp()
107
    {
108
        return $this->app;
109
    }
110
111
    /**
112
     * Make URLs for user browser navigation
113
     *
114
     * @param array $parameters
115
     *
116
     * @throws InstagramException
117
     *
118
     * @return string
119
     */
120
    public function getLoginUrl(array $parameters)
121
    {
122
        if ($this->callbackUrl === null) {
123
            throw new InstagramException('Missing Callback Url', 400);
124
        }
125
        
126
        if (!isset($parameters['scope'])) {
127
            throw new InstagramException('Missing or Invalid Scope permission used', 400);
128
        }
129
        if (count(array_diff($parameters['scope'], $this->defaultScopes)) !== 0) {
130
            throw new InstagramException('Missing or Invalid Scope permission used', 400);
131
        }
132
133
        $this->scopes = $parameters['scope'];
134
135
        $loginUrl = new LoginUrl(
136
            $this->getApp(),
137
            $this->getCallbackUrl(),
138
            $this->getState(),
139
            $this->scopes
140
        );
141
142
        return $loginUrl->loginUrl();
143
    }
144
145
    /**
146
     * Get the Oauth Access Token of a user from callback code
147
     *
148
     * @param string $code - Oauth2 Code returned with callback url after successfull login
149
     *
150
     * @return InstagramOAuth
151
     */
152
    public function oauth($code)
153
    {
154
        $options = [
155
            'grant_type' => 'authorization_code',
156
            'client_id' => $this->app->getId(),
157
            'client_secret' => $this->app->getSecret(),
158
            'redirect_uri' => $this->getCallbackUrl(),
159
            'code' => $code,
160
            'state' => $this->state,
161
        ];
162
163
        $response = HelperFactory::getInstance()->request($this->client, Constants::API_TOKEN, $options, 'POST');
164
165
        $this->oauthResponse = new InstagramOAuth(
166
            json_decode($response->getBody()->getContents())
167
        );
168
169
        return $this->oauthResponse;
170
    }
171
172
    /**
173
     * Setter: Callback Url
174
     *
175
     * @param string $url
176
     */
177
    public function setCallbackUrl($url)
178
    {
179
        $this->callbackUrl = $url;
180
    }
181
182
    /**
183
     * Getter: Callback Url
184
     *
185
     * @return string
186
     */
187
    public function getCallbackUrl()
188
    {
189
        return $this->callbackUrl;
190
    }
191
192
    /**
193
     * Get InstagramOAuth
194
     *
195
     * @return InstagramOAuth
196
     */
197
    public function getOAuth()
198
    {
199
        if ($this->oauthResponse instanceof InstagramOAuth) {
200
            return $this->oauthResponse;
201
        }
202
203
        $accessToken = new stdClass;
204
        $accessToken->access_token = null;
205
206
        $this->oauthResponse = new InstagramOAuth($accessToken);
207
208
        return $this->oauthResponse;
209
    }
210
211
    /**
212
     * Get Http Client
213
     *
214
     * @return Client
215
     */
216
    public function getHttpClient()
217
    {
218
        return $this->client;
219
    }
220
221
    /**
222
     * Set User Access Token
223
     *
224
     * @param string $token
225
     */
226
    public function setAccessToken($token)
227
    {
228
        if (!$this->oauthResponse instanceof InstagramOAuth) {
229
            $this->oauthResponse = new InstagramOAuth(json_decode(json_encode(['access_token' => $token])));
230
        }
231
    }
232
233
    /**
234
     * Get state value
235
     *
236
     * @return string|mixed
237
     */
238
    public function getState()
239
    {
240
        return $this->state;
241
    }
242
243
    /**
244
     * Get a string containing the version of the library.
245
     *
246
     * @return string
247
     */
248
    public function getLibraryVersion()
249
    {
250
        return Constants::VERSION;
251
    }
252
}
253