Passed
Push — master ( bdcf08...aa5f9e )
by Manuele
02:26
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
namespace OrbitronDev\OAuth2\Client\Provider;
4
5
use League\OAuth2\Client\Provider\AbstractProvider;
6
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
7
use League\OAuth2\Client\Token\AccessToken;
8
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
9
use Psr\Http\Message\ResponseInterface;
10
11
class OrbitronDevProvider extends AbstractProvider
12
{
13
    use BearerAuthorizationTrait;
14
15
    const ACCESS_TOKEN_RESOURCE_OWNER_ID = 'id';
16
17
    /**
18
     * Default host
19
     *
20
     * @var string
21
     */
22
    protected $host = 'https://account.orbitrondev.org';
23
24
    /**
25
     * Gets host.
26
     *
27
     * @return string
28
     */
29 4
    public function getHost()
30
    {
31 4
        return $this->host;
32
    }
33
34
    /**
35
     * Sets host. Can be used for example when you testing the service-account in localhost
36
     *
37
     * @param string $host The domain for accessing the user data
38
     *
39
     * @return string
40
     */
41 2
    public function setHost($host)
42
    {
43 2
        $this->host = $host;
44 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...
45
    }
46
47
    /**
48
     * Returns the base URL for authorizing a client.
49
     *
50
     * Eg. https://oauth.service.com/authorize
51
     *
52
     * @return string
53
     */
54 6
    public function getBaseAuthorizationUrl()
55
    {
56 6
        return $this->host.'/oauth/authorize';
57
    }
58
59
    /**
60
     * Returns the base URL for requesting an access token.
61
     *
62
     * Eg. https://oauth.service.com/token
63
     *
64
     * @param array $params Special parameters
65
     *
66
     * @return string
67
     */
68 12
    public function getBaseAccessTokenUrl(array $params)
69
    {
70 12
        return $this->host.'/oauth/token';
71
    }
72
73
    /**
74
     * Returns the URL for requesting the resource owner's details.
75
     *
76
     * @param AccessToken $token The received access token from the server
77
     *
78
     * @return string
79
     */
80
    public function getResourceOwnerDetailsUrl(AccessToken $token)
81
    {
82
        return $this->host.'/oauth/resource';
83
    }
84
85
    /**
86
     * Returns the default scopes used by this provider.
87
     *
88
     * This should only be the scopes that are required to request the details
89
     * of the resource owner, rather than all the available scopes.
90
     *
91
     * @return array
92
     */
93 4
    protected function getDefaultScopes()
94
    {
95 4
        return [];
96
    }
97
98
    /**
99
     * Get the string used to separate scopes.
100
     *
101
     * @return string
102
     */
103 6
    protected function getScopeSeparator()
104
    {
105 6
        return ' ';
106
    }
107
108
    /**
109
     * Checks a provider response for errors.
110
     *
111
     * @param ResponseInterface $response The response from the server
112
     * @param array|string      $data     Parsed response data
113
     *
114
     * @return void
115
     * @throws IdentityProviderException
116
     */
117 10
    protected function checkResponse(ResponseInterface $response, $data)
118
    {
119 10
        if ($response->getStatusCode() >= 400) {
120 4
            $errorMessage = isset($data['message']) ? $data['message'] : $response->getReasonPhrase();
121 2
        } elseif (isset($data['error'])) {
122
            $errorMessage = isset($data['error']) ? $data['error'] : $response->getReasonPhrase();
123
        }
124
125 4
        if (isset($errorMessage)) {
126
            throw new IdentityProviderException(
127
                $errorMessage,
128
                $response->getStatusCode(),
129
                $response->getBody()
130
            );
131
        }
132 4
    }
133
134
    /**
135
     * Generates a resource owner object from a successful resource owner
136
     * details request.
137
     *
138
     * @param array       $response Response data from server
139
     * @param AccessToken $token    The used access token
140
     *
141
     * @return \League\OAuth2\Client\Provider\ResourceOwnerInterface
142
     */
143
    protected function createResourceOwner(array $response, AccessToken $token)
144
    {
145
        return new OrbitronDevResourceOwner($response);
146
    }
147
}
148