Redmine   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 4
dl 0
loc 83
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A getRequestTokenEndpoint() 0 4 1
A getAuthorizationEndpoint() 0 4 1
A getAccessTokenEndpoint() 0 4 1
A parseRequestTokenResponse() 0 12 5
A parseAccessTokenResponse() 0 23 4
1
<?php
2
3
namespace OAuth\OAuth1\Service;
4
5
use Exception;
6
use OAuth\Common\Consumer\CredentialsInterface;
7
use OAuth\Common\Http\Client\ClientInterface;
8
use OAuth\Common\Http\Exception\TokenResponseException;
9
use OAuth\Common\Http\Uri\Uri;
10
use OAuth\Common\Http\Uri\UriInterface;
11
use OAuth\Common\Storage\TokenStorageInterface;
12
use OAuth\OAuth1\Signature\SignatureInterface;
13
use OAuth\OAuth1\Token\StdOAuth1Token;
14
15
class Redmine extends AbstractService
16
{
17
    public function __construct(
18
        CredentialsInterface $credentials,
19
        ClientInterface $httpClient,
20
        TokenStorageInterface $storage,
21
        SignatureInterface $signature,
22
        UriInterface $baseApiUri
23
    ) {
24
        parent::__construct($credentials, $httpClient, $storage, $signature, $baseApiUri);
25
26
        if (null === $baseApiUri) {
27
            throw new Exception('baseApiUri is a required argument.');
28
        }
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function getRequestTokenEndpoint()
35
    {
36
        return new Uri($this->baseApiUri->getAbsoluteUri() . '/request_token');
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getAuthorizationEndpoint()
43
    {
44
        return new Uri($this->baseApiUri->getAbsoluteUri() . '/authorize');
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getAccessTokenEndpoint()
51
    {
52
        return new Uri($this->baseApiUri->getAbsoluteUri() . '/access_token');
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    protected function parseRequestTokenResponse($responseBody)
59
    {
60
        parse_str($responseBody, $data);
61
62
        if (null === $data || !is_array($data)) {
63
            throw new TokenResponseException('Unable to parse response.');
64
        } elseif (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true') {
65
            throw new TokenResponseException('Error in retrieving token.');
66
        }
67
68
        return $this->parseAccessTokenResponse($responseBody);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    protected function parseAccessTokenResponse($responseBody)
75
    {
76
        parse_str($responseBody, $data);
77
78
        if (null === $data || !is_array($data)) {
79
            throw new TokenResponseException('Unable to parse response.');
80
        } elseif (isset($data['error'])) {
81
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
82
        }
83
84
        $token = new StdOAuth1Token();
85
86
        $token->setRequestToken($data['oauth_token']);
87
        $token->setRequestTokenSecret($data['oauth_token_secret']);
88
        $token->setAccessToken($data['oauth_token']);
89
        $token->setAccessTokenSecret($data['oauth_token_secret']);
90
91
        $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
92
        unset($data['oauth_token'], $data['oauth_token_secret']);
93
        $token->setExtraParams($data);
94
95
        return $token;
96
    }
97
}
98