Completed
Pull Request — master (#5)
by Risan Bagja
01:34
created

OAuth1::getTokenCredentials()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 3
crap 6
1
<?php
2
3
namespace Risan\OAuth1;
4
5
use InvalidArgumentException;
6
use Risan\OAuth1\Credentials\ClientCredentials;
7
use Risan\OAuth1\Request\RequestConfigInterface;
8
use Risan\OAuth1\Credentials\TemporaryCredentials;
9
use Risan\OAuth1\Credentials\CredentialsFactoryInterface;
10
11
class OAuth1 implements OAuth1Interface
12
{
13
    /**
14
     * The RequestConfigInterface instance.
15
     *
16
     * @var \Risan\OAuth1\Request\RequestConfigInterface
17
     */
18
    protected $requestConfig;
19
20
    /**
21
     * The HttpClientInterface instance.
22
     *
23
     * @var \Risan\OAuth1\HttpClientInterface
24
     */
25
    protected $httpClient;
26
27
    /**
28
     * The CredentialsFactoryInterface instance.
29
     *
30
     * @var \Risan\OAuth1\Credentials\CredentialsFactoryInterface
31
     */
32
    protected $credentialsFactory;
33
34
    /**
35
     * Create a new OAuth1 instance.
36
     *
37
     * @param \Risan\OAuth1\Request\RequestConfigInterface $requestConfig
38
     * @param \Risan\OAuth1\HttpClientInterface $httpClient
39
     * @param \Risan\OAuth1\Credentials\CredentialsFactoryInterface $credentialsFactory
40
     */
41 7
    public function __construct(RequestConfigInterface $requestConfig, HttpClientInterface $httpClient, CredentialsFactoryInterface $credentialsFactory)
42
    {
43 7
        $this->requestConfig = $requestConfig;
44 7
        $this->httpClient = $httpClient;
45 7
        $this->credentialsFactory = $credentialsFactory;
46 7
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51 1
    public function getRequestConfig()
52
    {
53 1
        return $this->requestConfig;
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59 1
    public function getHttpClient()
60
    {
61 1
        return $this->httpClient;
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67 1
    public function getCredentialsFactory()
68
    {
69 1
        return $this->credentialsFactory;
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75 1
    public function getTemporaryCredentials()
76
    {
77 1
        $response = $this->httpClient->post($this->requestConfig->getTemporaryCredentialsUrl(), [
78
            'headers' => [
79 1
                'Authorization' => $this->requestConfig->getTemporaryCredentialsAuthorizationHeader(),
80
            ],
81
        ]);
82
83 1
        return $this->credentialsFactory->createTemporaryCredentialsFromResponse($response);
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     */
89 1
    public function buildAuthorizationUrl(TemporaryCredentials $temporaryCredentials)
90
    {
91 1
        return $this->requestConfig->buildAuthorizationUrl($temporaryCredentials);
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97
    public function getTokenCredentials(TemporaryCredentials $temporaryCredentials, $temporaryIdentifier, $verificationCode)
98
    {
99
        if ($temporaryCredentials->getIdentifier() !== $temporaryIdentifier) {
100
            throw new InvalidArgumentException('The given temporary identifier does not match the temporary credentials.');
101
        }
102
103
        $response = $this->httpClient->post($this->requestConfig->getTokenCredentialsUrl(), [
104
            'headers' => [
105
                'Authorization' => $this->requestConfig->getTokenCredentialsAuthorizationHeader($temporaryCredentials, $verificationCode),
106
            ],
107
            'form_params' => [
108
                'oauth_verifier' => $verificationCode,
109
            ],
110
        ]);
111
112
        return $response;
113
    }
114
}
115