1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace PeeHaa\AsyncTwitter\Oauth; |
4
|
|
|
|
5
|
|
|
use PeeHaa\AsyncTwitter\Credentials\Application; |
6
|
|
|
use PeeHaa\AsyncTwitter\Credentials\AccessToken; |
7
|
|
|
use PeeHaa\AsyncTwitter\Request\Parameter; |
8
|
|
|
use PeeHaa\AsyncTwitter\Request\Url; |
9
|
|
|
|
10
|
|
|
class Parameters |
11
|
|
|
{ |
12
|
|
|
private $consumer; |
13
|
|
|
|
14
|
|
|
private $accessToken; |
15
|
|
|
|
16
|
|
|
private $nonce; |
17
|
|
|
|
18
|
|
|
private $timestamp; |
19
|
|
|
|
20
|
|
|
private $signatureMethod; |
21
|
|
|
|
22
|
|
|
private $version; |
23
|
|
|
|
24
|
|
|
private $parameters; |
25
|
|
|
|
26
|
|
|
private $url; |
27
|
|
|
|
28
|
16 |
|
public function __construct(Application $consumer, AccessToken $accessToken, Url $url, Parameter ...$parameters) |
29
|
|
|
{ |
30
|
16 |
|
$this->consumer = $consumer->getKey(); |
31
|
16 |
|
$this->accessToken = $accessToken->getToken(); |
32
|
16 |
|
$this->nonce = bin2hex(random_bytes(16)); |
33
|
16 |
|
$this->timestamp = (new \DateTimeImmutable())->format('U'); |
34
|
16 |
|
$this->signatureMethod = 'HMAC-SHA1'; |
35
|
16 |
|
$this->version = '1.0'; |
36
|
16 |
|
$this->parameters = $parameters; |
37
|
16 |
|
$this->url = $url; |
38
|
|
|
} |
39
|
|
|
|
40
|
6 |
|
public function getConsumerKey(): string |
41
|
|
|
{ |
42
|
6 |
|
return $this->consumer; |
43
|
|
|
} |
44
|
|
|
|
45
|
8 |
|
public function getNonce(): string |
46
|
|
|
{ |
47
|
8 |
|
return $this->nonce; |
48
|
|
|
} |
49
|
|
|
|
50
|
6 |
|
public function getSignatureMethod(): string |
51
|
|
|
{ |
52
|
6 |
|
return $this->signatureMethod; |
53
|
|
|
} |
54
|
|
|
|
55
|
6 |
|
public function getTimestamp(): string |
56
|
|
|
{ |
57
|
6 |
|
return $this->timestamp; |
58
|
|
|
} |
59
|
|
|
|
60
|
6 |
|
public function getToken(): string |
61
|
|
|
{ |
62
|
6 |
|
return $this->accessToken; |
63
|
|
|
} |
64
|
|
|
|
65
|
6 |
|
public function getVersion(): string |
66
|
|
|
{ |
67
|
6 |
|
return $this->version; |
68
|
|
|
} |
69
|
|
|
|
70
|
9 |
|
public function getAll(): array |
71
|
|
|
{ |
72
|
9 |
|
return array_merge($this->getBaseParameters(), $this->getParameters()); |
73
|
|
|
} |
74
|
|
|
|
75
|
9 |
|
private function getBaseParameters(): array |
76
|
|
|
{ |
77
|
|
|
return [ |
78
|
9 |
|
'oauth_consumer_key' => $this->consumer, |
79
|
9 |
|
'oauth_token' => $this->accessToken, |
80
|
9 |
|
'oauth_nonce' => $this->nonce, |
81
|
9 |
|
'oauth_timestamp' => $this->timestamp, |
82
|
9 |
|
'oauth_signature_method' => $this->signatureMethod, |
83
|
9 |
|
'oauth_version' => $this->version, |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
|
87
|
9 |
View Code Duplication |
private function getParameters(): array |
|
|
|
|
88
|
|
|
{ |
89
|
9 |
|
$parameters = []; |
90
|
|
|
|
91
|
9 |
|
foreach ($this->parameters as $parameter) { |
92
|
9 |
|
$parameters[$parameter->getKey()] = $parameter->getValue(); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
9 |
|
return $parameters; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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.