Completed
Push — master ( b7db91...9b4aa4 )
by Дмитрий
03:25
created

Provider::getOpenIdUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\Steam;
8
9
use SocialConnect\Auth\Exception\InvalidResponse;
10
use SocialConnect\Auth\Provider\OpenID\AccessToken;
11
use SocialConnect\Common\Entity\User;
12
use SocialConnect\Common\Hydrator\ObjectMap;
13
14
class Provider extends \SocialConnect\Auth\Provider\OpenID\AbstractProvider
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function getOpenIdUrl()
20
    {
21
        return 'http://steamcommunity.com/openid/id';
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function getBaseUri()
28
    {
29
        return 'http://api.steampowered.com/';
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getName()
36
    {
37
        return 'steam';
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     * @throws \SocialConnect\Auth\Exception\InvalidResponse
43
     */
44
    public function getIdentity(AccessToken $accessToken)
45
    {
46
        preg_match(
47
            "/^http:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/",
48
            $accessToken->getIdentity(),
49
            $matches
50
        );
51
52
        $userId = $matches[0];
53
54
        $response = $this->service->getHttpClient()->request(
55
            $this->getBaseUri() . 'ISteamUser/GetPlayerSummaries/v0002/',
56
            [
57
                'key' => $this->consumer->getKey(),
58
                'steamids' => $userId
59
            ]
60
        );
61
62
        if (!$response->isSuccess()) {
63
            throw new InvalidResponse(
64
                'API response with error code',
65
                $response
66
            );
67
        }
68
69
        $result = $response->json();
70
        if (!$result) {
71
            throw new InvalidResponse(
72
                'API response is not a valid JSON object',
73
                $response->getBody()
74
            );
75
        }
76
77
        $hydrator = new ObjectMap(array(
78
            'steamid' => 'id',
79
            'personaname' => 'username',
80
            'realname' => 'fullname'
81
        ));
82
83
        return $hydrator->hydrate(new User(), $result->response->players[0]);
84
    }
85
}
86