1 | <?php |
||
2 | |||
3 | namespace kalanis\RemoteRequestPsr\Http; |
||
4 | |||
5 | |||
6 | use kalanis\RemoteRequest\Protocols; |
||
7 | use kalanis\RemoteRequest\Protocols\Http; |
||
8 | use Psr\Http\Message\StreamInterface; |
||
9 | |||
10 | |||
11 | /** |
||
12 | * Class Query |
||
13 | * @package kalanis\RemoteRequestPsr\Http |
||
14 | * Message to the remote server compilation - protocol http, uses StreamInterface as body |
||
15 | */ |
||
16 | class Query extends Protocols\Http\Query |
||
17 | { |
||
18 | protected string $path = '/'; |
||
19 | protected ?StreamInterface $streamBody; |
||
20 | |||
21 | 5 | public function setContentStream(StreamInterface $stream): self |
|
22 | { |
||
23 | 5 | $this->streamBody = $stream; |
|
24 | 5 | return $this; |
|
25 | } |
||
26 | |||
27 | /** |
||
28 | * Add HTTP variables |
||
29 | * @param string[] $array |
||
30 | * @return $this |
||
31 | */ |
||
32 | 1 | public function addValues($array): parent |
|
33 | { |
||
34 | 1 | return $this; |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Add HTTP variable |
||
39 | * @param string $key |
||
40 | * @param string|Http\Query\Value $value |
||
41 | * @return $this |
||
42 | */ |
||
43 | 1 | public function addValue(string $key, $value): parent |
|
44 | { |
||
45 | 1 | return $this; |
|
0 ignored issues
–
show
|
|||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Remove HTTP key-value |
||
50 | * @param string $key |
||
51 | * @return $this |
||
52 | */ |
||
53 | 1 | public function removeValue(string $key): parent |
|
54 | { |
||
55 | 1 | return $this; |
|
0 ignored issues
–
show
|
|||
56 | } |
||
57 | |||
58 | 5 | public function getData() |
|
59 | { |
||
60 | 5 | $this->contentStream = Protocols\Helper::getTempStorage(); |
|
61 | 5 | $this->contentLength = 0; |
|
62 | 5 | $this->addHeader('Host', $this->getHostAndPort()); |
|
63 | |||
64 | 5 | $this->checkForMethod(); |
|
65 | 5 | $this->checkForFiles(); |
|
66 | 5 | $this->prepareBoundary(); |
|
67 | 5 | $this->prepareQuery(); |
|
68 | |||
69 | 5 | $this->contentLengthHeader(); |
|
70 | 5 | $this->contentTypeHeader(); |
|
71 | |||
72 | 5 | $storage = Protocols\Helper::getTempStorage(); |
|
73 | 5 | fwrite($storage, $this->renderRequestHeader()); |
|
74 | 5 | rewind($this->contentStream); |
|
75 | 5 | stream_copy_to_stream($this->contentStream, $storage); |
|
76 | 5 | rewind($storage); |
|
77 | 5 | return $storage; |
|
78 | } |
||
79 | |||
80 | 5 | protected function prepareQuery(): parent |
|
81 | { |
||
82 | 5 | $this->contentLength += $this->addStreamBody(); |
|
83 | 5 | return $this; |
|
0 ignored issues
–
show
|
|||
84 | } |
||
85 | |||
86 | 5 | protected function contentTypeHeader(): parent |
|
87 | { |
||
88 | 5 | if (in_array($this->getMethod(), $this->multipartMethods)) { |
|
89 | 2 | $this->addHeader('Content-Type', 'application/x-www-form-urlencoded'); |
|
90 | } else { |
||
91 | 3 | $this->removeHeader('Content-Type'); |
|
92 | } |
||
93 | 5 | return $this; |
|
0 ignored issues
–
show
|
|||
94 | } |
||
95 | |||
96 | 5 | protected function addStreamBody(): int |
|
97 | { |
||
98 | 5 | if (empty($this->streamBody)) { |
|
99 | 1 | return 0; |
|
100 | } |
||
101 | 4 | $this->streamBody->rewind(); |
|
102 | 4 | $size = 0; |
|
103 | 4 | while (!$this->streamBody->eof()) { |
|
104 | 4 | $size += intval(fwrite($this->contentStream, $this->streamBody->read($this->segmentSize()))); |
|
105 | } |
||
106 | 4 | return $size; |
|
107 | } |
||
108 | |||
109 | 4 | protected function segmentSize(): int |
|
110 | { |
||
111 | 4 | return 131072; |
|
112 | } |
||
113 | } |
||
114 |