Completed
Push — master ( 7e93b1...fb0071 )
by Дмитрий
03:44
created

AbstractProvider::makeAccessTokenRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 13
cts 13
cp 1
rs 9.4285
cc 1
eloc 12
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\OAuth2;
8
9
use InvalidArgumentException;
10
use SocialConnect\Auth\AbstractBaseProvider;
11
use SocialConnect\Auth\AccessTokenInterface;
12
use SocialConnect\Auth\Provider\Exception\InvalidAccessToken;
13
use SocialConnect\Auth\Provider\Exception\InvalidResponse;
14
use SocialConnect\Common\Entity\User;
15
use SocialConnect\Common\Http\Client\Client;
16
17
abstract class AbstractProvider extends AbstractBaseProvider
18
{
19
    /**
20
     * HTTP method for access token request
21
     *
22
     * @var string
23
     */
24
    protected $requestHttpMethod = Client::POST;
25
26
    /**
27
     * @return string
28
     */
29
    abstract public function getAuthorizeUri();
30
31
    /**
32
     * @return string
33
     */
34
    abstract public function getRequestTokenUri();
35
36
    /**
37
     * Default parameters for auth url, can be redeclared inside implementation of the Provider
38
     *
39
     * @return array
40
     */
41 1
    public function getAuthUrlParameters()
42
    {
43
        return array(
44 1
            'client_id' => $this->consumer->getKey(),
45 1
            'redirect_uri' => $this->getRedirectUrl(),
46 1
            'response_type' => 'code',
47 1
        );
48
    }
49
50
    /**
51
     * @return string
52
     */
53 1
    public function makeAuthUrl()
54
    {
55 1
        $urlParameters = $this->getAuthUrlParameters();
56
57 1
        if (count($this->scope) > 0) {
58
            $urlParameters['scope'] = $this->getScopeInline();
59
        }
60
61 1
        if (count($this->fields) > 0) {
62
            $urlParameters['fields'] = $this->getFieldsInline();
63
        }
64
65 1
        return $this->getAuthorizeUri() . '?' . http_build_query($urlParameters);
66
    }
67
68
    /**
69
     * Parse access token from response's $body
70
     *
71
     * @param string|bool $body
72
     * @return AccessToken
73
     * @throws InvalidAccessToken
74
     */
75
    public function parseToken($body)
76
    {
77
        if (empty($body)) {
78
            throw new InvalidAccessToken('Provider response with empty body');
79
        }
80
81
        parse_str($body, $token);
82
83
        if (!is_array($token) || !isset($token['access_token'])) {
84
            throw new InvalidAccessToken('Provider API returned an unexpected response');
85
        }
86
87
        return new AccessToken($token);
88
    }
89
90
    /**
91
     * @param string $code
92
     * @return \SocialConnect\Common\Http\Request
93
     */
94 1
    protected function makeAccessTokenRequest($code)
95
    {
96
        $parameters = array(
97 1
            'client_id' => $this->consumer->getKey(),
98 1
            'client_secret' => $this->consumer->getSecret(),
99 1
            'code' => $code,
100 1
            'grant_type' => 'authorization_code',
101 1
            'redirect_uri' => $this->getRedirectUrl()
102 1
        );
103
104 1
        return new \SocialConnect\Common\Http\Request(
105 1
            $this->getRequestTokenUri(),
106 1
            $parameters,
107 1
            $this->requestHttpMethod,
108
            [
109
                'Content-Type' => 'application/x-www-form-urlencoded'
110 1
            ]
111 1
        );
112
    }
113
114
    /**
115
     * @param string $code
116
     * @return AccessToken
117
     * @throws InvalidResponse
118
     */
119 1
    public function getAccessToken($code)
120
    {
121 1
        if (!is_string($code)) {
122 1
            throw new InvalidArgumentException('Parameter $code must be a string');
123
        }
124
125
        $response = $this->service->getHttpClient()->fromRequest(
126
            $this->makeAccessTokenRequest($code)
127
        );
128
129
        if (!$response->isSuccess()) {
130
            throw new InvalidResponse(
131
                'API response with error code',
132
                $response
133
            );
134
        }
135
136
        $body = $response->getBody();
137
        return $this->parseToken($body);
138
    }
139
140
141
    /**
142
     * @param array $parameters
143
     * @return AccessToken
144
     */
145
    public function getAccessTokenByRequestParameters(array $parameters)
146
    {
147
        return $this->getAccessToken($parameters['code']);
148
    }
149
}
150