Completed
Pull Request — master (#4)
by Risan Bagja
02:18 queued 51s
created

ProtocolParameter::getSigner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Risan\OAuth1\Request;
4
5
use DateTime;
6
use Risan\OAuth1\Config\ConfigInterface;
7
use Risan\OAuth1\Signature\SignerInterface;
8
use Risan\OAuth1\Credentials\TemporaryCredentials;
9
use Risan\OAuth1\Credentials\ServerIssuedCredentials;
10
11
class ProtocolParameter implements ProtocolParameterInterface
12
{
13
    /**
14
     * The ConfigInterface instance.
15
     *
16
     * @var \Risan\OAuth1\Config\ConfigInterface
17
     */
18
    protected $config;
19
20
    /**
21
     * The SignerInterface instance.
22
     *
23
     * @var \Risan\OAuth1\Signature\SignerInterface
24
     */
25
    protected $signer;
26
27
    /**
28
     * The NonceGeneratorInterface instance.
29
     *
30
     * @var \Risan\OAuth1\Request\NonceGeneratorInterface
31
     */
32
    protected $nonceGenerator;
33
34
    /**
35
     * Create ProtocolParameter instance.
36
     *
37
     * @param \Risan\OAuth1\ConfigInterface $config
38
     * @param \Risan\OAuth1\Signature\SignerInterface $signer
39
     * @param \Risan\OAuth1\Request\NonceGeneratorInterface $nonceGenerator
40
     */
41
    public function __construct(ConfigInterface $config, SignerInterface $signer, NonceGeneratorInterface $nonceGenerator)
42
    {
43
        $this->config = $config;
44
        $this->signer = $signer;
45
        $this->nonceGenerator = $nonceGenerator;
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    public function getConfig()
52
    {
53
        return $this->config;
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59
    public function getSigner()
60
    {
61
        return $this->signer;
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67
    public function getNonceGenerator()
68
    {
69
        return $this->nonceGenerator;
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function getCurrentTimestamp()
76
    {
77
        return (new DateTime)->getTimestamp();
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function getVersion()
84
    {
85
        return '1.0';
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91 View Code Duplication
    public function getBase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
    {
93
        return [
94
            'oauth_consumer_key' => $this->config->getClientCredentialsIdentifier(),
95
            'oauth_nonce' => $this->nonceGenerator->generate(),
96
            'oauth_signature_method' => $this->signer->getMethod(),
97
            'oauth_timestamp' => "{$this->getCurrentTimestamp()}",
98
            'oauth_version' => $this->getVersion(),
99
        ];
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105 View Code Duplication
    public function forTemporaryCredentials()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
    {
107
        $parameters = $this->getBase();
108
109
        if ($this->config->hasCallbackUri()) {
110
            $parameters['oauth_callback'] = $this->config->getCallbackUri();
111
        }
112
113
        $parameters['oauth_signature'] = $this->getSignature($parameters, $this->config->getTemporaryCredentialsUrl());
0 ignored issues
show
Bug introduced by
The method getTemporaryCredentialsUrl() does not exist on Risan\OAuth1\Config\ConfigInterface. Did you maybe mean getTemporaryCredentialsUri()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
114
115
        return $parameters;
116
    }
117
118
    /**
119
     * {@inheritDoc}
120
     */
121
    public function forTokenCredentials(TemporaryCredentials $temporaryCredentials, $verificationCode)
122
    {
123
        $parameters = $this->getBase();
124
125
        $requestOptions = [
126
            'form_params' => ['oauth_verifier' => $verificationCode],
127
        ];
128
129
        $parameters['oauth_signature'] = $this->getSignature(
130
            $parameters,
131
            $this->config->getTokenCredentialsUri(),
132
            $temporaryCredentials,
133
            $requestOptions
134
        );
135
136
        return $parameters;
137
    }
138
139
    /**
140
     * {@inheritDoc}
141
     */
142
    public function getSignature(array $protocolParameters, $uri, ServerIssuedCredentials $serverIssuedCredentials = null, array $requestOptions = [], $httpMethod = 'POST')
143
    {
144
        $signatureParameters = $this->signatureParameters($protocolParameters, $requestOptions);
145
146
        return $this->setupSigner($serverIssuedCredentials)
147
            ->sign($uri, $signatureParameters, $httpMethod);
148
    }
149
150
    /**
151
     * Build the signature parameters to be signed.
152
     *
153
     * @param  array  $protocolParameters
154
     * @param  array  $requestOptions
155
     * @return array
156
     */
157
    public function signatureParameters(array $protocolParameters, array $requestOptions = [])
158
    {
159
        $parameters = $protocolParameters;
160
161
        if ($this->requestOptionsHas($requestOptions, 'query')) {
162
            $parameters = array_merge($parameters, $requestOptions['query']);
163
        }
164
165
        if ($this->requestOptionsHas($requestOptions, 'form_params')) {
166
            $parameters = array_merge($parameters, $requestOptions['form_params']);
167
        }
168
169
        return $parameters;
170
    }
171
172
    /**
173
     * Setup the signer.
174
     *
175
     * @param  \Risan\OAuth1\Credentials\ServerIssuedCredentials|null $serverIssuedCredentials
176
     * @return \Risan\OAuth1\Signature\SignerInterface
177
     */
178
    public function setupSigner(ServerIssuedCredentials $serverIssuedCredentials = null)
179
    {
180
        if ($this->shouldSignWithClientCredentials()) {
181
            $this->signer->setClientCredentials($this->config->getClientCredentials());
182
        }
183
184
        if ($this->shouldSignWithServerIssuedCredentials($serverIssuedCredentials)) {
185
            $this->signer->setServerIssuedCredentials($serverIssuedCredentials);
186
        }
187
188
        return $this->signer;
189
    }
190
191
    /**
192
     * Should sign with the client credentials.
193
     *
194
     * @return boolean
195
     */
196
    public function shouldSignWithClientCredentials()
197
    {
198
        return $this->signer->isKeyBased();
199
    }
200
201
    /**
202
     * Should sign with the server issued credentials.
203
     *
204
     * @param  \Risan\OAuth1\Credentials\ServerIssuedCredentials|null $serverIssuedCredentials
205
     * @return boolean
206
     */
207
    public function shouldSignWithServerIssuedCredentials(ServerIssuedCredentials $serverIssuedCredentials = null)
208
    {
209
        return $this->signer->isKeyBased() && $serverIssuedCredentials !== null;
210
    }
211
212
    /**
213
     * Check if request options has the given key option.
214
     *
215
     * @param  array  $requestOptions
216
     * @param  string $key
217
     * @return boolean
218
     */
219
    public function requestOptionsHas(array $requestOptions, $key)
220
    {
221
        return isset($requestOptions[$key]) &&
222
            is_array($requestOptions[$key]) &&
223
            count($requestOptions[$key]) > 0;
224
    }
225
}
226