Passed
Push — master ( 5d7891...ad38a6 )
by Manuele
02:20
created

OrbitronDevProvider::getDefaultScopes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Votifier PHP Client
5
 *
6
 * @package   OAuth2-OrbitronDev
7
 *
8
 * @author    Manuele Vaccari <[email protected]>
9
 * @copyright Copyright (c) 2017-2018 Manuele Vaccari <[email protected]>
10
 * @license   https://github.com/D3strukt0r/oauth2-orbitrondev/blob/master/LICENSE.md MIT License
11
 *
12
 * @link      https://github.com/D3strukt0r/oauth2-orbitrondev
13
 */
14
15
namespace OrbitronDev\OAuth2\Client\Provider;
16
17
use League\OAuth2\Client\Provider\AbstractProvider;
18
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
19
use League\OAuth2\Client\Token\AccessToken;
20
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
21
use Psr\Http\Message\ResponseInterface;
22
23
/**
24
 * The Class in which all user information will be stored.
25
 */
26
class OrbitronDevProvider extends AbstractProvider
27
{
28
    use BearerAuthorizationTrait;
29
30
    const ACCESS_TOKEN_RESOURCE_OWNER_ID = 'id';
31
32
    /**
33
     * Default host.
34
     *
35
     * @var string
36
     */
37
    protected $host = 'https://account.orbitrondev.org';
38
39
    /**
40
     * Gets host.
41
     *
42
     * @return string
43
     */
44 4
    public function getHost()
45
    {
46 4
        return $this->host;
47
    }
48
49
    /**
50
     * Sets host. Can be used for example when you testing the service-account in localhost.
51
     *
52
     * @param string $host The domain for accessing the user data
53
     *
54
     * @return string
55
     */
56 2
    public function setHost($host)
57
    {
58 2
        $this->host = $host;
59
60 2
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type OrbitronDev\OAuth2\Clien...der\OrbitronDevProvider which is incompatible with the documented return type string.
Loading history...
61
    }
62
63
    /**
64
     * Returns the base URL for authorizing a client.
65
     *
66
     * Eg. https://oauth.service.com/authorize
67
     *
68
     * @return string
69
     */
70 6
    public function getBaseAuthorizationUrl()
71
    {
72 6
        return $this->host.'/oauth/authorize';
73
    }
74
75
    /**
76
     * Returns the base URL for requesting an access token.
77
     *
78
     * Eg. https://oauth.service.com/token
79
     *
80
     * @param array $params Special parameters
81
     *
82
     * @return string
83
     */
84 6
    public function getBaseAccessTokenUrl(array $params)
85
    {
86 6
        return $this->host.'/oauth/token';
87
    }
88
89
    /**
90
     * Returns the URL for requesting the resource owner's details.
91
     *
92
     * @param AccessToken $token The received access token from the server
93
     *
94
     * @return string
95
     */
96 2
    public function getResourceOwnerDetailsUrl(AccessToken $token)
97
    {
98 2
        return $this->host.'/oauth/resource';
99
    }
100
101
    /**
102
     * Returns the default scopes used by this provider.
103
     *
104
     * This should only be the scopes that are required to request the details
105
     * of the resource owner, rather than all the available scopes.
106
     *
107
     * @return array
108
     */
109 4
    protected function getDefaultScopes()
110
    {
111 4
        return [];
112
    }
113
114
    /**
115
     * Get the string used to separate scopes.
116
     *
117
     * @return string
118
     */
119 6
    protected function getScopeSeparator()
120
    {
121 6
        return ' ';
122
    }
123
124
    /**
125
     * Checks a provider response for errors.
126
     *
127
     * @param ResponseInterface $response The response from the server
128
     * @param array|string      $data     Parsed response data
129
     *
130
     * @throws IdentityProviderException
131
     */
132 4
    protected function checkResponse(ResponseInterface $response, $data)
133
    {
134 4
        if ($response->getStatusCode() >= 400) {
135
            $errorMessage = isset($data['message']) ? $data['message'] : $response->getReasonPhrase();
136 4
        } elseif (isset($data['error'])) {
137
            $errorMessage = isset($data['error']) ? $data['error'] : $response->getReasonPhrase();
138
        }
139
140 4
        if (isset($errorMessage)) {
141
            throw new IdentityProviderException(
142
                $errorMessage,
143
                $response->getStatusCode(),
144
                $response->getBody()
145
            );
146
        }
147 4
    }
148
149
    /**
150
     * Generates a resource owner object from a successful resource owner
151
     * details request.
152
     *
153
     * @param array       $response Response data from server
154
     * @param AccessToken $token    The used access token
155
     *
156
     * @return \League\OAuth2\Client\Provider\ResourceOwnerInterface
157
     */
158 2
    protected function createResourceOwner(array $response, AccessToken $token)
159
    {
160 2
        return new OrbitronDevResourceOwner($response);
161
    }
162
}
163