Login::getLogin()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 4
cts 6
cp 0.6667
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 0
crap 3.3332
1
<?php
2
3
declare ( strict_types = 1 );
4
5
namespace Ch0c01dxyz\InstaToken\Endpoints;
6
7
use Http\Client\HttpClient;
8
use Http\Discovery\HttpClientDiscovery;
9
use Http\Discovery\MessageFactoryDiscovery;
10
use Http\Message\RequestFactory;
11
use Ch0c01dxyz\InstaToken\Interfaces\LoginInterface;
12
use Ch0c01dxyz\InstaToken\Exceptions\LoginException;
13
14
/**
15
 * @author Egar Rizki <[email protected]>
16
 */
17
class Login implements LoginInterface
18
{
19
	/**
20
	 * @var [type]
21
	 */
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
22
	protected $appId;
23
24
	/**
25
	 * @var [type]
26
	 */
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
27
	protected $appSecret;
28
29
	/**
30
	 * @var [type]
31
	 */
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
32
	protected $appCallback;
33
34
	/**
35
	 * @var [type]
36
	 */
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
37
	protected $appScope;
38
39
	/**
40
	 * @var [type]
41
	 */
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
42
	protected $responseCode;
43
44
	/**
45
	 * @var [type]
46
	 */
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
47
	protected $result;
48
49
	/**
50
	 * @var \Http\Client\HttpClient
51
	 */
52
	protected $httpClient;
53
54
	/**
55
	 * @var \Http\Message\RequestFactory
56
	 */
57
	protected $requestFactory;
58
59
	/**
60
	 * New instance of Login class
61
	 *
62
	 * @param string $appId
63
	 * @param string $appSecret
64
	 * @param string $appCallback
65
	 * @param \Http\Client\HttpClient|null $httpClient
66
	 * @param \Http\Message\RequestFactory|null $requestFactory
67
	 */
68 2
	public function __construct ( string $appId, string $appSecret, string $appCallback, string $appScope, HttpClient $httpClient = null, RequestFactory $requestFactory = null )
69
	{
70 2
		$this->appId = ( string ) $appId;
71
72 2
		$this->appSecret = ( string ) $appSecret;
73
74 2
		$this->appCallback = ( string ) $appCallback;
75
76 2
		$this->appScope = ( string ) $appScope;
77
78 2
		$this->httpClient = $httpClient ?: HttpClientDiscovery::find ();
79
80 2
		$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find ();
81 2
	}
82
83
	/**
84
	 * Get URL Login redirect Instagram
85
	 */
86 1
	public function getLogin () : string
87
	{
88 1
		if ( empty ( $this->appId ) )
89
		{
90
			throw new LoginException ( "AppID required." );
91
		}
92
93 1
		if ( empty ( $this->appScope ) )
94
		{
95
			throw new LoginException ( "AppScope required." );
96
		}
97
98 1
		return "https://api.instagram.com/oauth/authorize/?client_id=" . $this->appId . "&redirect_uri=" . $this->appCallback . "&response_type=code" . "&scope=" . $this->appScope;
99
	}
100
101
	/**
102
	 * Get Instagram Authorization
103
	 *
104
	 * param string $code
105
	 */
106
	public function doAuth ( string $code ) : array
107
	{
108
		if ( empty ( $code ) )
109
		{
110
			throw new LoginException ( "Code required." );
111
		}
112
113
		$uri = "https://api.instagram.com/oauth/access_token";
114
115
		$this->builder->addResource ( "client_id", $this->appId );
0 ignored issues
show
Bug Best Practice introduced by
The property builder does not exist on Ch0c01dxyz\InstaToken\Endpoints\Login. Did you maybe forget to declare it?
Loading history...
116
		$this->builder->addResource ( "client_secret", $this->appSecret );
117
		$this->builder->addResource ( "grant_type", "authorization_code" );
118
		$this->builder->addResource ( "redirect_uri", $this->appCallback );
119
		$this->builder->addResource ( "code", $code );
120
121
		$request = $this->requestFactory->createRequest ( "POST", $uri, [
122
			"Content-Type" => 'multipart/form-data; boundary="' . $this->builder->getBoundary () . '"'
123
		], ( string ) $this->builder->build () );
124
125
		$response = $this->httpClient->sendRequest ( $request );
126
127
		if ( $response->getStatusCode () === 400 )
128
		{
129
			$body = json_decode ( ( string ) $response->getBody () );
130
131
			throw new LoginException ( $body->meta->error_message );
132
		}
133
134
		return json_decode ( ( string ) $response->getBody ()->getContents (), true );
135
	}
136
}