Completed
Push — master ( 8f8221...a2309d )
by Haridarshan
02:33
created

Instagram::getApp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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
 */
26
namespace Haridarshan\Instagram;
27
28
use stdClass;
29
use InvalidArgumentException;
30
use Haridarshan\Instagram\Constants;
31
use Haridarshan\Instagram\Exceptions\InstagramException;
32
use Haridarshan\Instagram\HelperFactory;
33
use Haridarshan\Instagram\InstagramOAuth;
34
35
/**
36
 * PHP Instagram API wrapper class
37
 * 
38
 * @library			instagram-php
39
 * @license 		https://opensource.org/licenses/MIT MIT
40
 * @link			http://github.com/haridarshan/instagram-php Class Documentation
41
 * @link			http://instagram.com/developer/ API Documentation
42
 * @author			Haridarshan Gorana 	<[email protected]>
43
 * @since			May 09, 2016
44
 * @copyright		Haridarshan Gorana
45
 * @version			2.2.2
46
 */
47
class Instagram
48
{
49
    /** @var InstagramApp */
50
    protected $app;
51
	
52
    /** @var string */
53
    protected $callbackUrl;
54
    
55
    /** @var array */
56
    protected $defaultScopes = array("basic", "public_content", "follower_list", "comments", "relationships", "likes");
57
	
58
    /** @var array */
59
    protected $scopes = array();
60
	
61
    /**
62
     * Random string indicating the state to prevent spoofing
63
     * @var string
64
     */
65
    protected $state;
66
		
67
    /** @var \GuzzleHttp\Client $client */
68
    protected $client;
69
	
70
    /** @var InstagramOAuth $oauthResponse */
71
    protected $oauthResponse;
72
	
73
    /**
74
     * Default Constructor
75
     * Instagram Configuration Data
76
	 *
77
     * @param array $config
78
	 *
79
	 * @throws InstagramException|InvalidArgumentException
80
	 * 
81
	 * @todo validate callback url
82
     */
83
    public function __construct(array $config = [])
84
    {
85
        if (!is_array($config)) {
86
            throw new InstagramException('Invalid Instagram Configuration data');
87
        }
88
		
89
		if (!$config['ClientId']) {
90
            throw new InstagramException('Missing "ClientId" key not supplied in config');
91
        }
92
		
93
		if (!$config['ClientSecret']) {
94
            throw new InstagramException('Missing "ClientSecret" key not supplied in config');
95
        }
96
		
97
		if (!$config['Callback']) {
98
            throw new InstagramException('Missing "Callback" key not supplied in config');
99
        }
100
		
101
		$this->app = new InstagramApp($config['ClientId'], $config['ClientSecret']);	
102
		
103
        $this->setCallbackUrl($config['Callback']);
104
        $this->state = isset($config['State']) ? $config['State'] : substr(md5(rand()), 0, 7);
105
        
106
        $this->client = HelperFactory::getInstance()->client(Constants::API_HOST);
107
    }
108
	
109
	/**
110
     * Returns InstagramApp entity.
111
     *
112
     * @return InstagramApp
113
     */
114
    public function getApp()
115
    {
116
        return $this->app;
117
    }
118
	
119
    /**
120
     * Make URLs for user browser navigation
121
	 *
122
     * @param array  $parameters
123
	 *
124
     * @return string
125
	 *
126
	 * @throws InstagramException
127
     */
128
    public function getLoginUrl(array $parameters)
129
    {
130
        if (!isset($parameters['scope'])) {
131
            throw new InstagramException("Missing or Invalid Scope permission used", 400);
132
        }
133
        if (count(array_diff($parameters['scope'], $this->defaultScopes)) !== 0) {
134
            throw new InstagramException("Missing or Invalid Scope permission used", 400);
135
        }
136
		
137
        $this->scopes = $parameters['scope'];
138
		
139
		$loginUrl = new LoginUrl(
140
			$this->getApp(),
141
			$this->getCallbackUrl(),
142
			$this->getState(),
143
			$this->scopes
144
		);
145
		
146
		return $loginUrl->loginUrl();
147
    }
148
	
149
    /**
150
     * Get the Oauth Access Token of a user from callback code
151
	 *
152
     * @param string $code - Oauth2 Code returned with callback url after successfull login
153
	 *
154
     * @return InstagramOAuth
155
     */
156
    public function oauth($code)
157
    {
158
        $options = array(
159
            "grant_type" => "authorization_code",
160
            "client_id" => $this->app->getId(),
161
            "client_secret" => $this->app->getSecret(),
162
            "redirect_uri" => $this->getCallbackUrl(),
163
            "code" => $code,
164
            "state" => $this->state
165
        );
166
		
167
		$response = HelperFactory::getInstance()->request($this->client, Constants::API_TOKEN, $options, 'POST');
168
		
169
        $this->oauthResponse = new InstagramOAuth(
170
			json_decode($response->getBody()->getContents())
171
		);
172
		
173
        return $this->oauthResponse;
174
    }
175
	
176
    /**
177
     * Setter: Callback Url
178
	 *
179
     * @param string $url
180
	 *
181
     * @return void
182
     */
183
    public function setCallbackUrl($url)
184
    {
185
        $this->callbackUrl = $url;
186
    }
187
	
188
    /**
189
     * Getter: Callback Url
190
	 *
191
     * @return string
192
     */
193
    public function getCallbackUrl()
194
    {
195
        return $this->callbackUrl;
196
    }
197
	
198
    /**
199
     * Get InstagramOAuth
200
	 *
201
     * @return InstagramOAuth
202
     */
203
    public function getOAuth()
204
    {
205
        if ($this->oauthResponse instanceof InstagramOAuth) {
206
            return $this->oauthResponse;
207
        }
208
		
209
		$accessToken = new stdClass;
210
		$accessToken->access_token = null;
211
		
212
        $this->oauthResponse = new InstagramOAuth($accessToken);
213
        return $this->oauthResponse;
214
    }
215
	
216
    /**
217
	 * Get Http Client
218
	 *
219
     * @return Client
220
     */
221
    public function getHttpClient()
222
    {
223
        return $this->client;
224
    }
225
	
226
    /**
227
     * Set User Access Token
228
	 *
229
     * @param string $token
230
	 *
231
     * @return void
232
     */
233
    public function setAccessToken($token)
234
    {
235
        if (!$this->oauthResponse instanceof InstagramOAuth) {
236
            $this->oauthResponse = new InstagramOAuth(json_decode(json_encode(["access_token" => $token])));
237
        }
238
    }
239
	
240
    /**
241
     * Get state value
242
	 *
243
     * @return string|mixed
244
     */
245
    public function getState()
246
    {
247
        return $this->state;
248
    }
249
250
    /**
251
     * Get a string containing the version of the library.
252
	 *
253
     * @return string
254
     */
255
    public function getLibraryVersion()
256
    {
257
        return Constants::VERSION;
258
    }
259
}
260