Completed
Push — master ( d2e951...b1f2c8 )
by Haridarshan
02:18
created

Instagram::createBody()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 3
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 3
rs 10
cc 3
eloc 2
nc 4
nop 2
1
<?php
2
/*
3
* Instagram API class
4
*
5
* API Documentation: http://instagram.com/developer/
6
* Class Documentation: https://github.com/haridarshan/Instagram-php
7
*
8
* @author Haridarshan Gorana
9
* @since May 09, 2016
10
* @copyright Haridarshan Gorana
11
* @version 2.0
12
* @license: MIT
13
*
14
*/
15
namespace Haridarshan\Instagram;
16
17
use Haridarshan\Instagram\Constants;
18
use Haridarshan\Instagram\InstagramOAuth;
19
use Haridarshan\Instagram\HelperFactory;
20
use Haridarshan\Instagram\Exceptions\InstagramException;
21
use Haridarshan\Instagram\Exceptions\InstagramOAuthException;
22
use Haridarshan\Instagram\Exceptions\InstagramThrottleException;
23
24
class Instagram
25
{
26
    /** @var string */
27
    private $clientId;
28
	
29
    /** @var string */
30
    private $clientSecret;
31
	
32
    /** @var string */
33
    private $callbackUrl;
34
    
35
    /** @var array<string> */
36
    private $defaultScopes = array("basic", "public_content", "follower_list", "comments", "relationships", "likes");
37
	
38
    /** @var array<string> */
39
    private $scopes = array();
40
	
41
    /*
42
    * Random string indicating the state to prevent spoofing
43
    * @var string
44
    */
45
    private $state;
46
		
47
    /** @var \GuzzleHttp\Client $client */
48
    protected $client;
49
	
50
    /** @var object $oauthResponse */
51
    private $oauthResponse;
52
	
53
    /*
54
     * Default Constructor
55
     * Instagram Configuration Data
56
     * @param array|object|string $config
57
     */
58
    public function __construct($config)
59
    {
60
        if (is_array($config)) {
61
            $this->setClientId($config['ClientId']);
62
            $this->setClientSecret($config['ClientSecret']);
63
            $this->setCallbackUrl($config['Callback']);
64
            $this->state = isset($config['State']) ? $config['State'] : substr(md5(rand()), 0, 7);
65
        } else {
66
            throw new InstagramException('Invalid Instagram Configuration data', 400);
67
        }
68
        $this->client = HelperFactory::client(Constants::API_HOST);
69
    }
70
	
71
    /*
72
     * Make URLs for user browser navigation
73
     * @param string $path
0 ignored issues
show
Bug introduced by
There is no parameter named $path. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
74
     * @param array  $parameters
75
     * @return string
76
     */
77
    public function getLoginUrl(array $parameters)
78
    {
79
        if (!isset($parameters['scope'])) {
80
            throw new InstagramException("Missing or Invalid Scope permission used", 400);
81
        }
82
        if (count(array_diff($parameters['scope'], $this->defaultScopes)) === 0) {
83
            $this->scopes = $parameters['scope'];
84
        } else {
85
            throw new InstagramException("Missing or Invalid Scope permission used", 400);
86
        }
87
        $query = 'client_id='.$this->getClientId().'&redirect_uri='.urlencode($this->getCallbackUrl()).'&response_type=code&state='.$this->state;
88
        $query .= isset($this->scopes) ? '&scope='.urlencode(str_replace(",", " ", implode(",", $parameters['scope']))) : '';
89
        return sprintf('%s%s?%s', Constants::API_HOST, Constants::API_AUTH, $query);
90
    }
91
	
92
    /*
93
     * Get the Oauth Access Token of a user from callback code
94
     *
95
     * @param string $path - OAuth Access Token Path
0 ignored issues
show
Bug introduced by
There is no parameter named $path. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
96
     * @param string $code - Oauth2 Code returned with callback url after successfull login
97
     * @param boolean $token - true will return only access token
98
     */
99
    public function oauth($code, $token = false)
0 ignored issues
show
Unused Code introduced by
The parameter $token is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
    {
101
        $options = array(
102
            "grant_type" => "authorization_code",
103
            "client_id" => $this->getClientId(),
104
            "client_secret" => $this->getClientSecret(),
105
            "redirect_uri" => $this->getCallbackUrl(),
106
            "code" => $code,
107
            "state" => $this->state
108
        );
109
		$this->oauthResponse = new InstagramOAuth(
110
			json_decode((HelperFactory::request($this->client, Constants::API_TOKEN, $options, 'POST'))->getBody()->getContents())
111
		);
112
        return $this->oauthResponse;
113
    }
114
   
115
    /*
116
     * Set Client Id
117
     * @param string $clientId
118
     * @return void
119
     */
120
    public function setClientId($clientId)
121
    {
122
        $this->clientId = $clientId;
123
    }
124
	
125
    /*
126
     * Get Client Id
127
     * @return string
128
     */
129
    public function getClientId()
130
    {
131
        return $this->clientId;
132
    }
133
	
134
    /*
135
     * Set Client Secret
136
     * @param string $secret
137
     * @return void
138
     */
139
    public function setClientSecret($secret)
140
    {
141
        $this->clientSecret = $secret;
142
    }
143
	
144
    /*
145
     * Getter: Client Id
146
     * @return string
147
     */
148
    public function getClientSecret()
149
    {
150
        return $this->clientSecret;
151
    }
152
	
153
    /*
154
     * Setter: Callback Url
155
     * @param string $url
156
     * @return void
157
     */
158
    public function setCallbackUrl($url)
159
    {
160
        $this->callbackUrl = $url;
161
    }
162
	
163
    /*
164
     * Getter: Callback Url
165
     * @return string
166
     */
167
    public function getCallbackUrl()
168
    {
169
        return $this->callbackUrl;
170
    }
171
	
172
    /*
173
     * Get InstagramOAuth
174
     * @return InstagramOAuth
175
     */
176
    public function getOAuth()
177
    {
178
        if ($this->oauthResponse instanceof InstagramOAuth) {
179
            return $this->oauthResponse;
180
        } else {
181
            $this->oauthResponse = new InstagramOAuth(json_decode(json_encode(["access_token" => null])));
182
            return $this->oauthResponse;
183
        }
184
    }
185
    /*
186
     * @return Client
187
     */
188
    public function getHttpClient()
189
    {
190
        return $this->client;
191
    }
192
	
193
    /*
194
     * Setter: User Access Token
195
     * @param string $token
196
     * @return void
197
     */
198
    public function setAccessToken($token)
199
    {
200
        if (!$this->oauthResponse instanceof InstagramOAuth) {
201
            $this->oauthResponse = new InstagramOAuth(json_decode(json_encode(["access_token" => $token])));
202
        }
203
    }
204
205
    /*
206
     * Get a string containing the version of the library.
207
     * @return string
208
     */
209
    public function getLibraryVersion()
210
    {
211
        return Constants::VERSION;
212
    }
213
	
214
    /*
215
     * Get state value
216
     * @return string|mixed
217
     */
218
    public function getState()
219
    {
220
        return $this->state;
221
    }
222
}
223