Completed
Pull Request — master (#456)
by
unknown
09:01
created

Slack::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 13
rs 9.4285
c 1
b 0
f 1
cc 2
eloc 9
nc 2
nop 5
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\OAuth2\Token\StdOAuth2Token;
6
use OAuth\Common\Http\Exception\TokenResponseException;
7
use OAuth\Common\Http\Uri\Uri;
8
use OAuth\Common\Consumer\CredentialsInterface;
9
use OAuth\Common\Http\Client\ClientInterface;
10
use OAuth\Common\Storage\TokenStorageInterface;
11
use OAuth\Common\Http\Uri\UriInterface;
12
13
/**
14
 * Slack service.
15
 *
16
 * @author Antoine Corcy <[email protected]>
17
 */
18
class Slack extends AbstractService
19
{
20
    /**
21
     * Defined scopes
22
     * @link https://api.slack.com/docs/oauth-scopes
23
     */
24
    const SCOPE_INCOMING_WEBHOOK = 'incoming-webhook';
25
    const SCOPE_COMMANDS         = 'commands';
26
    const SCOPE_BOT              = 'bot';
27
28
    public function __construct(
29
        CredentialsInterface $credentials,
30
        ClientInterface $httpClient,
31
        TokenStorageInterface $storage,
32
        $scopes = array(),
33
        UriInterface $baseApiUri = null
34
    ) {
35
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
36
37
        if (null === $baseApiUri) {
38
            $this->baseApiUri = new Uri('https://slack.com/api/');
39
        }
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getAuthorizationEndpoint()
46
    {
47
        return new Uri('https://slack.com/oauth/authorize');
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getAccessTokenEndpoint()
54
    {
55
        return new Uri('https://slack.com/api/oauth.access');
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function getAuthorizationMethod()
62
    {
63
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    protected function parseAccessTokenResponse($responseBody)
70
    {
71
        $data = json_decode($responseBody, true);
72
73
        if (null === $data || !is_array($data)) {
74
            throw new TokenResponseException('Unable to parse response.');
75
        } elseif (isset($data['error'])) {
76
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
77
        }
78
79
        $token = new StdOAuth2Token();
80
        $token->setAccessToken($data['access_token']);
81
82
        unset($data['access_token']);
83
84
        $token->setExtraParams($data);
85
86
        return $token;
87
    }
88
}
89