1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\RemoteRequest\Pointers; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\RemoteRequest\Interfaces\IConnectionParams; |
7
|
|
|
use kalanis\RemoteRequest\Interfaces\IQuery; |
8
|
|
|
use kalanis\RemoteRequest\Interfaces\IRRTranslations; |
9
|
|
|
use kalanis\RemoteRequest\Protocols\Helper; |
10
|
|
|
use kalanis\RemoteRequest\RequestException; |
11
|
|
|
use kalanis\RemoteRequest\Traits\TLang; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Processor |
16
|
|
|
* @package kalanis\RemoteRequest\Pointers |
17
|
|
|
* Query to the remote server - read into provided output |
18
|
|
|
*/ |
19
|
|
|
class Processor |
20
|
|
|
{ |
21
|
|
|
use TLang; |
22
|
|
|
|
23
|
|
|
protected ?IQuery $remoteQuery = null; |
24
|
|
|
/** @var resource|null */ |
25
|
|
|
protected $remoteResponse = null; |
26
|
|
|
|
27
|
51 |
|
public function __construct(?IRRTranslations $lang = null) |
28
|
|
|
{ |
29
|
51 |
|
$this->setRRLang($lang); |
30
|
|
|
} |
31
|
|
|
|
32
|
7 |
|
public function setQuery(?IQuery $content): self |
33
|
|
|
{ |
34
|
7 |
|
$this->remoteQuery = $content; |
35
|
7 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param resource|null $filePointer |
40
|
|
|
* @param IConnectionParams $params |
41
|
|
|
* @throws RequestException |
42
|
|
|
* @return $this |
43
|
|
|
* @codeCoverageIgnore because accessing remote resources, similar code is in overwrite |
44
|
|
|
*/ |
45
|
|
|
public function processPointer($filePointer, IConnectionParams $params): self |
46
|
|
|
{ |
47
|
|
|
$this->checkPointer($filePointer); |
48
|
|
|
$this->writeRequest($filePointer, $params); // @phpstan-ignore-line |
49
|
|
|
$this->readResponse($filePointer); // @phpstan-ignore-line |
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param resource $filePointer |
55
|
|
|
* @param IConnectionParams $params |
56
|
|
|
* @throws RequestException |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
5 |
|
protected function writeRequest($filePointer, IConnectionParams $params): self |
60
|
|
|
{ |
61
|
5 |
|
$srcStream = $this->getQuery()->getData(); // always exists - checked |
62
|
4 |
|
rewind($srcStream); |
63
|
4 |
|
stream_copy_to_stream($srcStream, $filePointer); |
64
|
4 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param resource $filePointer |
69
|
|
|
* @throws RequestException |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
4 |
|
protected function readResponse($filePointer): self |
73
|
|
|
{ |
74
|
4 |
|
$this->remoteResponse = null; |
75
|
|
|
|
76
|
|
|
// Read the server response |
77
|
4 |
|
$response = Helper::getTempStorage(); |
78
|
4 |
|
$bytesLeft = $this->getQuery()->getMaxAnswerLength(); // always exists - checked |
79
|
4 |
|
stream_copy_to_stream($filePointer, $response, (is_null($bytesLeft) ? -1 : $bytesLeft), 0); |
80
|
4 |
|
rewind($response); |
81
|
4 |
|
$this->remoteResponse = $response; |
82
|
4 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @throws RequestException |
87
|
|
|
* @return IQuery |
88
|
|
|
*/ |
89
|
5 |
|
protected function getQuery(): IQuery |
90
|
|
|
{ |
91
|
5 |
|
if (empty($this->remoteQuery) |
92
|
5 |
|
|| !($this->remoteQuery instanceof IQuery)) { |
93
|
1 |
|
throw new RequestException($this->getRRLang()->rrPointUnknownTarget()); |
94
|
|
|
} |
95
|
4 |
|
return $this->remoteQuery; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param resource|null $filePointer |
100
|
|
|
* @throws RequestException |
101
|
|
|
*/ |
102
|
6 |
|
protected function checkPointer($filePointer): void |
103
|
|
|
{ |
104
|
6 |
|
if (empty($filePointer) |
105
|
6 |
|
|| !is_resource($filePointer)) { |
106
|
1 |
|
throw new RequestException($this->getRRLang()->rrPointNoStreamPointer()); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return resource|null |
112
|
|
|
*/ |
113
|
4 |
|
public function getContent() |
114
|
|
|
{ |
115
|
4 |
|
return $this->remoteResponse; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|