Completed
Push — master ( 483eda...cb59ac )
by Дмитрий
03:04
created

Slack::getRequestTokenUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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