Completed
Pull Request — master (#10)
by
unknown
02:57
created

Slack   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 32.26%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 0
loc 129
ccs 10
cts 31
cp 0.3226
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseAuthorizationUrl() 0 4 1
A getBaseAccessTokenUrl() 0 4 1
A getAuthorizedUserTestUrl() 0 4 1
A getResourceOwnerDetailsUrl() 0 13 1
A checkResponse() 0 6 2
A createResourceOwner() 0 4 1
A getDefaultScopes() 0 4 1
A fetchAuthorizedUserDetails() 0 8 1
A getAuthorizedUser() 0 6 1
A createAuthorizedUser() 0 4 1
1
<?php
2
3
namespace AdamPaterson\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 Psr\Http\Message\ResponseInterface;
9
10
/**
11
 * Class Slack
12
 * @author Adam Paterson <[email protected]>
13
 *
14
 * @package AdamPaterson\OAuth2\Client\Provider
15
 */
16
class Slack extends AbstractProvider
17
{
18
    /**
19
     * Returns the base URL for authorizing a client.
20
     *
21
     * @return string
22
     */
23 6
    public function getBaseAuthorizationUrl()
24
    {
25 6
        return "https://slack.com/oauth/authorize";
26
    }
27
28
    /**
29
     * Returns the base URL for requesting an access token.
30
     *
31
     * @param array $params
32
     *
33
     * @return string
34
     */
35 12
    public function getBaseAccessTokenUrl(array $params)
36
    {
37 12
        return "https://slack.com/api/oauth.access";
38
    }
39
40
    /**
41
     * Returns the URL for requesting the resource owner's details.
42
     *
43
     * @param AccessToken $token
44
     *
45
     * @return string
46
     */
47
    public function getResourceOwnerDetailsUrl(AccessToken $token)
48
    {
49
        $authorizedUser = $this->getAuthorizedUser($token);
50
51
        $params = [
52
            'token' => $token->getToken(),
53
            'user'  => $authorizedUser->getId()
54
        ];
55
56
        $url = 'https://slack.com/api/users.info?'.http_build_query($params);
57
58
        return $url;
59
    }
60
61
    /**
62
     * @param $token
63
     *
64
     * @return string
65
     */
66 3
    public function getAuthorizedUserTestUrl($token)
67
    {
68 3
        return "https://slack.com/api/auth.test?token=".$token;
69
    }
70
71
    /**
72
     * Checks a provider response for errors.
73
     *
74
     * @throws IdentityProviderException
75
     *
76
     * @param  ResponseInterface $response
77
     * @param  array|string $data Parsed response data
78
     *
79
     * @return void
80
     */
81 12
    protected function checkResponse(ResponseInterface $response, $data)
82
    {
83 12
        $decodedData = json_decode($data);
84
        if($decodedData->{'ok'} == "false")
85
            throw new IdentityProviderException($decodedData->{'error'},$response->getStatusCode(),$response);
0 ignored issues
show
Documentation introduced by
$response is of type object<Psr\Http\Message\ResponseInterface>, but the function expects a array|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
86
    }
87
88
    /**
89
     * Create new resources owner using the generated access token.
90
     *
91
     * @param array $response
92
     * @param AccessToken $token
93
     *
94
     * @return SlackResourceOwner
95
     */
96
    protected function createResourceOwner(array $response, AccessToken $token)
97
    {
98
        return new SlackResourceOwner($response);
99
    }
100
101
    /**
102
     * @return array
103
     */
104 6
    protected function getDefaultScopes()
105
    {
106 6
        return [];
107
    }
108
109
    /**
110
     * @param AccessToken $token
111
     *
112
     * @return mixed
113
     */
114
    public function fetchAuthorizedUserDetails(AccessToken $token)
115
    {
116
        $url = $this->getAuthorizedUserTestUrl($token);
117
118
        $request = $this->getAuthenticatedRequest(self::METHOD_GET, $url, $token);
119
120
        return $this->getResponse($request);
121
    }
122
123
    /**
124
     * @param AccessToken $token
125
     *
126
     * @return SlackAuthorizedUser
127
     */
128
    public function getAuthorizedUser(AccessToken $token)
129
    {
130
        $response = $this->fetchAuthorizedUserDetails($token);
131
132
        return $this->createAuthorizedUser($response);
133
    }
134
135
    /**
136
     * @param $response
137
     *
138
     * @return SlackAuthorizedUser
139
     */
140
    protected function createAuthorizedUser($response)
141
    {
142
        return new SlackAuthorizedUser($response);
143
    }
144
}
145