Completed
Push — master ( deb54b...06ec1a )
by Дмитрий
01:56
created

SmashCast   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 15

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 20
lcom 3
cbo 15
dl 0
loc 196
ccs 0
cts 119
cp 0
rs 9.1666
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseUri() 0 4 1
A getAuthorizeUri() 0 4 1
A getRequestTokenUri() 0 4 1
A getName() 0 4 1
A getAuthUrlParameters() 0 8 1
A parseToken() 0 13 3
A makeAccessTokenRequest() 0 17 1
B getAccessTokenByRequestParameters() 0 21 5
B getUserNameByToken() 0 24 3
A getIdentity() 0 50 3
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\Common\Http\Client\Client;
10
use SocialConnect\OAuth2\Exception\InvalidState;
11
use SocialConnect\OAuth2\Exception\UnknownAuthorization;
12
use SocialConnect\OAuth2\Exception\UnknownState;
13
use SocialConnect\Provider\AccessTokenInterface;
14
use SocialConnect\Provider\Exception\InvalidAccessToken;
15
use SocialConnect\Provider\Exception\InvalidResponse;
16
use SocialConnect\OAuth2\AccessToken;
17
use SocialConnect\Common\Entity\User;
18
use SocialConnect\Common\Hydrator\ObjectMap;
19
20
class SmashCast extends \SocialConnect\OAuth2\AbstractProvider
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function getBaseUri()
26
    {
27
        return 'https://api.smashcast.tv/';
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getAuthorizeUri()
34
    {
35
        return 'https://api.smashcast.tv/oauth/login';
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getRequestTokenUri()
42
    {
43
        return 'https://api.smashcast.tv/oauth/exchange';
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getName()
50
    {
51
        return 'smashcast';
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getAuthUrlParameters()
58
    {
59
        return [
60
            'app_token' => $this->consumer->getKey(),
61
            'redirect_uri' => $this->getRedirectUrl(),
62
            'response_type' => 'code',
63
        ];
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function parseToken($body)
70
    {
71
        if (empty($body)) {
72
            throw new InvalidAccessToken('Provider response with empty body');
73
        }
74
75
        $result = json_decode($body, true);
76
        if ($result) {
77
            return new AccessToken($result);
78
        }
79
80
        throw new InvalidAccessToken('Provider response with not valid JSON');
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    protected function makeAccessTokenRequest($code)
87
    {
88
        $parameters = [
89
            'request_token' => $code,
90
            'app_token' => $this->consumer->getKey(),
91
            'hash' => base64_encode($this->consumer->getKey() . $this->consumer->getSecret()),
92
        ];
93
94
        return new \SocialConnect\Common\Http\Request(
95
            $this->getRequestTokenUri(),
96
            $parameters,
97
            $this->requestHttpMethod,
98
            [
99
                'Content-Type' => 'application/x-www-form-urlencoded'
100
            ]
101
        );
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function getAccessTokenByRequestParameters(array $parameters)
108
    {
109
        $state = $this->session->get('oauth2_state');
110
        if (!$state) {
111
            throw new UnknownAuthorization();
112
        }
113
114
        if (!isset($parameters['state'])) {
115
            throw new UnknownState();
116
        }
117
118
        if ($state !== $parameters['state']) {
119
            throw new InvalidState();
120
        }
121
122
        if (isset($parameters['authToken'])) {
123
            return new AccessToken(['access_token' => $parameters['authToken']]);
124
        }
125
126
        return $this->getAccessToken($parameters['request_token']);
127
    }
128
129
    /**
130
     * This method it needed, because I cannot fix auth/login with accessToken
131
     * BTW: Yes, I known that it's unneeded round trip to the server
132
     *
133
     * @param AccessTokenInterface $accessToken
134
     * @return mixed
135
     * @throws InvalidResponse
136
     */
137
    protected function getUserNameByToken(AccessTokenInterface $accessToken)
138
    {
139
        $response = $this->httpClient->request(
140
            $this->getBaseUri() . 'userfromtoken/' . $accessToken->getToken()
141
        );
142
143
        if (!$response->isSuccess()) {
144
            throw new InvalidResponse(
145
                'API response with error code',
146
                $response
147
            );
148
        }
149
150
        $result = $response->json();
151
152
        if (!$result) {
153
            throw new InvalidResponse(
154
                'API response is not a valid JSON object',
155
                $response->getBody()
156
            );
157
        }
158
159
        return $result->user_name;
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function getIdentity(AccessTokenInterface $accessToken)
166
    {
167
        // @todo Find a problem with this code
168
        /*$response = $this->httpClient->request(
169
            $this->getBaseUri() . 'auth/login',
170
            [
171
                'app' => 'desktop', // @any app name, not working, I was using JSON and not working..
172
                'authToken' => $accessToken->getToken()
173
            ],
174
            Client::POST,
175
            [
176
                'Content-Type' => 'application/x-www-form-urlencoded'
177
            ]
178
        );*/
179
180
        $username = $this->getUserNameByToken($accessToken);
181
182
        $response = $this->httpClient->request(
183
            $this->getBaseUri() . 'user/' . $username,
184
            [
185
                'authToken' => $accessToken->getToken()
186
            ]
187
        );
188
189
        if (!$response->isSuccess()) {
190
            throw new InvalidResponse(
191
                'API response with error code',
192
                $response
193
            );
194
        }
195
196
        $result = $response->json();
197
198
        if (!$result) {
199
            throw new InvalidResponse(
200
                'API response is not a valid JSON object',
201
                $response->getBody()
202
            );
203
        }
204
205
        $hydrator = new ObjectMap(
206
            [
207
                'user_id' => 'id',
208
                'user_name' => 'username',
209
                'user_email' => 'email',
210
            ]
211
        );
212
213
        return $hydrator->hydrate(new User(), $result);
214
    }
215
}
216