Completed
Pull Request — master (#479)
by Andrey
05:46
created

Yandex::setAccessType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\Common\Consumer\CredentialsInterface;
6
use OAuth\Common\Http\Client\ClientInterface;
7
use OAuth\Common\Http\Uri\UriInterface;
8
use OAuth\Common\Storage\TokenStorageInterface;
9
use OAuth\Common\Token\TokenInterface;
10
use OAuth\OAuth2\Token\StdOAuth2Token;
11
use OAuth\Common\Http\Exception\TokenResponseException;
12
use OAuth\OAuth2\Service\Exception\InvalidAccessTypeException;
13
use OAuth\Common\Http\Uri\Uri;
14
15
class Yandex extends AbstractService {
16
17
18
    public function __construct(
19
      CredentialsInterface $credentials,
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 6
Loading history...
20
      ClientInterface $httpClient,
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 6
Loading history...
21
      TokenStorageInterface $storage,
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 6
Loading history...
22
      $scopes = array(),
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 6
Loading history...
23
      UriInterface $baseApiUri = null
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 6
Loading history...
24
    ) {
25
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
26
27
        if (null === $baseApiUri) {
28
            $this->baseApiUri = new Uri('https://login.yandex.ru/info/');
29
        }
30
    }
31
32
    /**
33
     * Parses the access token response and returns a TokenInterface.
34
     *
35
     *
36
     * @param string $responseBody
37
     *
38
     * @return TokenInterface
39
     *
40
     * @throws TokenResponseException
41
     */
42
    protected function parseAccessTokenResponse($responseBody)
43
    {
44
        $data = json_decode($responseBody, true);
45
46
        if (null === $data || !is_array($data)) {
47
            throw new TokenResponseException('Unable to parse response.');
48
        } elseif (isset($data['error'])) {
49
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
50
        }
51
52
        $token = new StdOAuth2Token();
53
        $token->setAccessToken($data['access_token']);
54
55
        unset($data['access_token']);
56
57
        $token->setExtraParams($data);
58
59
        return $token;
60
    }
61
62
    /**
63
     * Returns the authorization API endpoint.
64
     *
65
     * @return UriInterface
66
     */
67
    public function getAuthorizationEndpoint()
68
    {
69
        return new Uri('https://oauth.yandex.ru/authorize');
70
    }
71
72
    /**
73
     * Returns the access token API endpoint.
74
     *
75
     * @return UriInterface
76
     */
77
    public function getAccessTokenEndpoint()
78
    {
79
        return new Uri('https://oauth.yandex.ru/token');
80
    }
81
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getAuthorizationUri(array $additionalParameters = array())
87
    {
88
        $parameters = array_merge(
89
          $additionalParameters,
90
          array(
91
            'client_id'     => $this->credentials->getConsumerId(),
92
            'response_type' => 'code',
93
          )
94
        );
95
        
96
        // Build the url
97
        $url = clone $this->getAuthorizationEndpoint();
98
        foreach ($parameters as $key => $val) {
99
            $url->addToQuery($key, $val);
100
        }
101
102
        return $url;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function requestAccessToken($code, $state = null)
109
    {
110
        if (null !== $state) {
111
            $this->validateAuthorizationState($state);
112
        }
113
114
        $bodyParams = array(
115
          'grant_type'    => 'authorization_code',
116
          'code'          => $code,
117
          'client_id'     => $this->credentials->getConsumerId(),
118
          'client_secret' => $this->credentials->getConsumerSecret(),
119
        );
120
121
        $responseBody = $this->httpClient->retrieveResponse(
122
          $this->getAccessTokenEndpoint(),
123
          $bodyParams,
124
          $this->getExtraOAuthHeaders(),
125
          'POST'
126
        );
127
128
        $token = $this->parseAccessTokenResponse($responseBody);
129
        $this->storage->storeAccessToken($this->service(), $token);
130
131
        return $token;
132
    }
133
134
    /**
135
     * @not-implemented
136
     * @param $accessType
137
     * @return bool
138
     */
139
    public function setAccessType($accessType)
0 ignored issues
show
Unused Code introduced by
The parameter $accessType 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...
140
    {
141
        return true;
142
    }
143
}