Passed
Pull Request — master (#190)
by AD
05:46 queued 03:08
created

Slack   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 68
ccs 18
cts 19
cp 0.9474
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseUri() 0 3 1
A getName() 0 3 1
A getAuthorizeUri() 0 3 1
A getRequestTokenUri() 0 3 1
A prepareRequest() 0 4 2
A getIdentity() 0 19 2
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
declare(strict_types=1);
7
8
namespace SocialConnect\OAuth2\Provider;
9
10
use SocialConnect\Common\ArrayHydrator;
11
use SocialConnect\Provider\AccessTokenInterface;
12
use SocialConnect\Provider\Exception\InvalidResponse;
13
use SocialConnect\Common\Entity\User;
14
15
class Slack extends \SocialConnect\OAuth2\AbstractProvider
16
{
17
    const NAME = 'slack';
18
19
    /**
20
     * {@inheritdoc}
21
     */
22 4
    public function getBaseUri()
23
    {
24 4
        return 'https://slack.com/';
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 2
    public function getAuthorizeUri()
31
    {
32 2
        return 'https://slack.com/oauth/authorize';
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 2
    public function getRequestTokenUri()
39
    {
40 2
        return 'https://slack.com/api/oauth.access';
41
    }
42
43
    /**
44
     * @return string
45
     */
46 3
    public function getName()
47
    {
48 3
        return self::NAME;
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54 3
    public function prepareRequest(string $method, string $uri, array &$headers, array &$query, ?AccessTokenInterface $accessToken = null): void
55
    {
56 3
        if ($accessToken) {
57 3
            $query['token'] = $accessToken->getToken();
58
        }
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 3
    public function getIdentity(AccessTokenInterface $accessToken)
65
    {
66 3
        $response = $this->request('GET', 'api/users.identity', [], $accessToken);
67
68 1
        if (!$response['ok']) {
69
            throw new InvalidResponse(
70
                'API response->ok is false'
71
            );
72
        }
73
74 1
        $hydrator = new ArrayHydrator([
75
            'id' => 'id',
76
            'name' => 'name',
77
        ]);
78
79 1
        $user = $hydrator->hydrate(new User(), $response['user']);
80 1
        $user->team = $response['team'];
81
82 1
        return $user;
83
    }
84
}
85