Completed
Push — master ( df0c28...f24cf1 )
by Дмитрий
01:28
created

Slack::getIdentity()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 43
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.2118

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 15
cts 26
cp 0.5769
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 23
nc 4
nop 1
crap 5.2118
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\OAuth2\Provider;
8
9
use SocialConnect\Provider\AccessTokenInterface;
10
use SocialConnect\Provider\Exception\InvalidAccessToken;
11
use SocialConnect\Provider\Exception\InvalidResponse;
12
use SocialConnect\OAuth2\AccessToken;
13
use SocialConnect\Common\Entity\User;
14
use SocialConnect\Common\Hydrator\ObjectMap;
15
16
class Slack extends \SocialConnect\OAuth2\AbstractProvider
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 3
    public function getBaseUri()
22
    {
23 3
        return 'https://slack.com/';
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 2
    public function getAuthorizeUri()
30
    {
31 2
        return 'https://slack.com/oauth/authorize';
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 2
    public function getRequestTokenUri()
38
    {
39 2
        return 'https://slack.com/api/oauth.access';
40
    }
41
42
    /**
43
     * @return string
44
     */
45 3
    public function getName()
46
    {
47 3
        return 'slack';
48
    }
49
50
    /**
51
     * @param $body
52
     * @return AccessToken
53
     * @throws InvalidAccessToken
54
     */
55 3
    public function parseToken($body)
56
    {
57 3
        $response = json_decode($body, true);
58 3
        if ($response) {
59 1
            return new AccessToken($response);
60
        }
61
62 2
        throw new InvalidAccessToken('AccessToken is not a valid JSON');
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 2
    public function getIdentity(AccessTokenInterface $accessToken)
69
    {
70 2
        $response = $this->httpClient->request(
71 2
            $this->getBaseUri() . 'api/users.identity',
72
            [
73 2
                'token' => $accessToken->getToken()
74 2
            ]
75 2
        );
76
77 2
        if (!$response->isSuccess()) {
78 1
            throw new InvalidResponse(
79 1
                'API response with error code',
80
                $response
81 1
            );
82
        }
83
84 1
        $result = $response->json();
85 1
        if (!$result) {
86 1
            throw new InvalidResponse(
87 1
                'API response is not a valid JSON object',
88
                $response
89 1
            );
90
        }
91
92
        if (!$result->ok) {
93
            throw new InvalidResponse(
94
                'API response->ok is false',
95
                $result
96
            );
97
        }
98
99
        $hydrator = new ObjectMap(
100
            [
101
                'id' => 'id',
102
                'name' => 'name',
103
            ]
104
        );
105
106
        $user = $hydrator->hydrate(new User(), $result->user);
107
        $user->team = $result->team;
108
109
        return $user;
110
    }
111
}
112