|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace PeeHaa\AsyncTwitter\Http; |
|
4
|
|
|
|
|
5
|
|
|
use Amp\Artax\Client as ArtaxClient; |
|
6
|
|
|
use Amp\Promise; |
|
7
|
|
|
use PeeHaa\AsyncTwitter\Request\Body; |
|
8
|
|
|
use PeeHaa\AsyncTwitter\Request\Parameter; |
|
9
|
|
|
use PeeHaa\AsyncTwitter\Request\Url; |
|
10
|
|
|
use PeeHaa\AsyncTwitter\Oauth\Header; |
|
11
|
|
|
use Amp\Artax\Request; |
|
12
|
|
|
|
|
13
|
|
|
class Artax implements Client |
|
14
|
|
|
{ |
|
15
|
|
|
private $client; |
|
16
|
|
|
|
|
17
|
4 |
|
public function __construct(ArtaxClient $client) |
|
18
|
|
|
{ |
|
19
|
4 |
|
$this->client = $client; |
|
20
|
4 |
|
} |
|
21
|
|
|
|
|
22
|
2 |
View Code Duplication |
public function post(Url $url, Header $header, Body $body, int $flags = 0): Promise |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
2 |
|
$request = (new Request) |
|
25
|
2 |
|
->setMethod('POST') |
|
26
|
2 |
|
->setUri($url->getUrl()) |
|
27
|
2 |
|
->setAllHeaders([ |
|
28
|
2 |
|
'Authorization' => $header->getHeader(), |
|
29
|
2 |
|
'Content-Type' => 'application/x-www-form-urlencoded', |
|
30
|
|
|
]) |
|
31
|
2 |
|
->setBody($this->getBodyString($body)) |
|
32
|
|
|
; |
|
33
|
|
|
|
|
34
|
2 |
|
$options = []; |
|
35
|
2 |
|
if ($flags & Client::OP_STREAM) { |
|
36
|
1 |
|
$options[ArtaxClient::OP_MS_TRANSFER_TIMEOUT] = -1; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
2 |
|
return $this->client->request($request, $options); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
2 |
|
private function getBodyString(Body $body): string |
|
43
|
|
|
{ |
|
44
|
2 |
|
$bodyString = ''; |
|
45
|
2 |
|
$delimiter = ''; |
|
46
|
|
|
|
|
47
|
2 |
|
foreach ($body->getParameters() as $parameter) { |
|
48
|
2 |
|
$bodyString .= $delimiter . $parameter->getKey() . '=' . rawurlencode($parameter->getValue()); |
|
49
|
|
|
|
|
50
|
2 |
|
$delimiter = '&'; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
2 |
|
return $bodyString; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
View Code Duplication |
public function get(Url $url, Header $header, array $parameters, int $flags = 0): Promise |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
2 |
|
$request = (new Request) |
|
59
|
2 |
|
->setMethod('GET') |
|
60
|
2 |
|
->setUri($url->getUrl() . $this->buildQueryString(...$parameters)) |
|
61
|
2 |
|
->setAllHeaders(['Authorization' => $header->getHeader()]) |
|
62
|
|
|
; |
|
63
|
|
|
|
|
64
|
2 |
|
$options = []; |
|
65
|
2 |
|
if ($flags & Client::OP_STREAM) { |
|
66
|
1 |
|
$options[ArtaxClient::OP_MS_TRANSFER_TIMEOUT] = -1; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
return $this->client->request($request, $options); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
2 |
|
private function buildQueryString(Parameter ...$parameters): string |
|
73
|
|
|
{ |
|
74
|
2 |
|
$queryString = []; |
|
75
|
|
|
|
|
76
|
2 |
|
foreach ($parameters as $parameter) { |
|
77
|
2 |
|
$queryString[$parameter->getKey()] = $parameter->getValue(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
return '?' . http_build_query($queryString); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
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.