Completed
Pull Request — master (#498)
by Dragonqos
02:30
created

ScoopIt::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
namespace OAuth\OAuth1\Service;
4
5
use OAuth\OAuth1\Token\StdOAuth1Token;
6
use OAuth\Common\Http\Exception\TokenResponseException;
7
use OAuth\Common\Http\Uri\Uri;
8
9
class ScoopIt extends AbstractService
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    public function init()
15
    {
16
        if (null === $this->baseApiUri) {
17
            $this->baseApiUri = new Uri('https://www.scoop.it/api/1/');
18
        }
19
    }
20
21
    /**
22
     * {@inheritDoc}
23
     */
24
    public function getRequestTokenEndpoint()
25
    {
26
        return new Uri('https://www.scoop.it/oauth/request');
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getAuthorizationEndpoint()
33
    {
34
        return new Uri('https://www.scoop.it/oauth/authorize');
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getAccessTokenEndpoint()
41
    {
42
        return new Uri('https://www.scoop.it/oauth/access');
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function parseRequestTokenResponse($responseBody)
49
    {
50
        parse_str($responseBody, $data);
51
52
        if (null === $data || !is_array($data)) {
53
            throw new TokenResponseException('Unable to parse response.');
54
        } elseif (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true') {
55
            throw new TokenResponseException('Error in retrieving token.');
56
        }
57
58
        return $this->parseAccessTokenResponse($responseBody);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    protected function parseAccessTokenResponse($responseBody)
65
    {
66
        parse_str($responseBody, $data);
67
68
        if (null === $data || !is_array($data)) {
69
            throw new TokenResponseException('Unable to parse response.');
70
        } elseif (isset($data['error'])) {
71
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
72
        }
73
74
        $token = new StdOAuth1Token();
75
76
        $token->setRequestToken($data['oauth_token']);
77
        $token->setRequestTokenSecret($data['oauth_token_secret']);
78
        $token->setAccessToken($data['oauth_token']);
79
        $token->setAccessTokenSecret($data['oauth_token_secret']);
80
81
        $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
82
        unset($data['oauth_token'], $data['oauth_token_secret']);
83
        $token->setExtraParams($data);
84
85
        return $token;
86
    }
87
}
88