Completed
Push — develop ( f1f6d4...edda33 )
by Risan Bagja
01:31
created

getTemporaryCredentialsAuthorizationHeader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Risan\OAuth1\Request;
4
5
use DateTime;
6
use GuzzleHttp\Psr7\Uri;
7
use Risan\OAuth1\ConfigInterface;
8
use Risan\OAuth1\Signature\HmacSha1Signer;
9
use Risan\OAuth1\Signature\SignerInterface;
10
use Risan\OAuth1\Credentials\TemporaryCredentials;
11
use Risan\OAuth1\Signature\KeyBasedSignerInterface;
12
13
class RequestConfig implements RequestConfigInterface
14
{
15
    /**
16
     * The ConfigInterface instance.
17
     *
18
     * @var \Risan\OAuth1\ConfigInterface
19
     */
20
    protected $config;
21
22
    /**
23
     * The SignerInterface instance.
24
     *
25
     * @var \Risan\OAuth1\Signature\SignerInterface
26
     */
27
    protected $signer;
28
29
    /**
30
     * The NonceGeneratorInterface instance.
31
     *
32
     * @var \Risan\OAuth1\Request\NonceGeneratorInterface
33
     */
34
    protected $nonceGenerator;
35
36
    /**
37
     * Create RequestBuilder instance.
38
     *
39
     * @param \Risan\OAuth1\ConfigInterface $config
40
     * @param \Risan\OAuth1\Signature\SignerInterface $signer
41
     * @param \Risan\OAuth1\Request\NonceGeneratorInterface $nonceGenerator
42
     */
43 14
    public function __construct(ConfigInterface $config, SignerInterface $signer, NonceGeneratorInterface $nonceGenerator)
44
    {
45 14
        $this->config = $config;
46 14
        $this->signer = $signer;
47 14
        $this->nonceGenerator = $nonceGenerator;
48
49 14
        if ($this->signer instanceof KeyBasedSignerInterface) {
50 2
            $this->signer->setClientCredentials($config->getClientCredentials());
51
        }
52 14
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57 1
    public function getConfig()
58
    {
59 1
        return $this->config;
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65 1
    public function getSigner()
66
    {
67 1
        return $this->signer;
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73 1
    public function getNonceGenerator()
74
    {
75 1
        return $this->nonceGenerator;
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81 2
    public function getCurrentTimestamp()
82
    {
83 2
        return (new DateTime)->getTimestamp();
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     */
89 1
    public function getTemporaryCredentialsUrl()
90
    {
91 1
        return $this->config->getTemporaryCredentialsUrl();
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97 1
    public function getTemporaryCredentialsAuthorizationHeader()
98
    {
99 1
        $parameters = $this->getBaseProtocolParameters();
100
101 1
        if ($this->config->hasCallbackUri()) {
102 1
            $parameters['oauth_callback'] = $this->config->getCallbackUri();
103
        }
104
105 1
        $this->addSignatureParameter($parameters, $this->getTemporaryCredentialsUrl(), 'POST');
106
107 1
        return $this->normalizeProtocolParameters($parameters);
108
    }
109
110
    /**
111
     * {@inheritDoc}
112
     */
113 1
    public function buildAuthorizationUrl(TemporaryCredentials $temporaryCredentials)
114
    {
115 1
        return $this->appendQueryParametersToUri($this->config->getAuthorizationUrl(), [
116 1
            'oauth_token' => $temporaryCredentials->getIdentifier(),
117
        ]);
118
    }
119
120
    /**
121
     * Get base protocol parameters for the authorization header.
122
     *
123
     * @return array
124
     */
125 1
    public function getBaseProtocolParameters()
126
    {
127
        return [
128 1
            'oauth_consumer_key' => $this->config->getClientCredentialsIdentifier(),
129 1
            'oauth_nonce' => $this->nonceGenerator->generate(),
130 1
            'oauth_signature_method' => $this->signer->getMethod(),
131 1
            'oauth_timestamp' => "{$this->getCurrentTimestamp()}",
132 1
            'oauth_version' => '1.0',
133
        ];
134
    }
135
136
    /**
137
     * Add signature parameter to the given protocol parameters.
138
     *
139
     * @param array  &$parameters
140
     * @param string $uri
141
     * @param string $httpMethod
142
     */
143 1
    public function addSignatureParameter(array &$parameters, $uri, $httpMethod = 'POST')
144
    {
145 1
        $parameters['oauth_signature'] = $this->signer->sign($uri, $parameters, $httpMethod);
146
147 1
        return $parameters;
148
    }
149
150
    /**
151
     * Append query parameters to URI.
152
     *
153
     * @param  string $uri
154
     * @param  array  $parameters
155
     * @return string
156
     */
157 2
    public function appendQueryParametersToUri($uri, array $parameters)
158
    {
159 2
        $uri = new Uri($uri);
160
161 2
        parse_str($uri->getQuery(), $queryParameters);
162
163 2
        $mergedParameters = array_merge($queryParameters, $parameters);
164
165 2
        return (string) $uri->withQuery(http_build_query($mergedParameters));
166
    }
167
168
    /**
169
     * Normalize protocol parameters to be used as authorization header.
170
     *
171
     * @param  array  $parameters
172
     * @return string
173
     */
174
    public function normalizeProtocolParameters(array $parameters)
175
    {
176 1
        array_walk($parameters, function (&$value, $key) {
177 1
            $value = rawurlencode($key) . '="' . rawurlencode($value) . '"';
178 1
        });
179
180
        return 'OAuth ' . implode(', ', $parameters);
181
    }
182
}
183