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