1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\RemoteRequest\Connection; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\RemoteRequest\Interfaces; |
7
|
|
|
use kalanis\RemoteRequest\Pointers; |
8
|
|
|
use kalanis\RemoteRequest\RequestException; |
9
|
|
|
use kalanis\RemoteRequest\Sockets; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Processor |
14
|
|
|
* @package kalanis\RemoteRequest\Connection |
15
|
|
|
* Query to the remote server - completing and simple processing |
16
|
|
|
* This is the network layer, what lies elsewhere and what has been sent through is not important for processing |
17
|
|
|
* It only needs to know destination, message to the destination and how to get response from remote; not how to make |
18
|
|
|
* something sane from response. That is not responsibility of these classes. |
19
|
|
|
*/ |
20
|
|
|
class Processor |
21
|
|
|
{ |
22
|
|
|
/** @var Interfaces\IQuery|null */ |
23
|
|
|
protected $data = null; |
24
|
|
|
/** @var Pointers\Processor */ |
25
|
|
|
protected $processor = null; |
26
|
|
|
/** @var Interfaces\IConnectionParams */ |
27
|
|
|
protected $params = null; |
28
|
|
|
/** @var Sockets\ASocket */ |
29
|
|
|
protected $socket = null; |
30
|
|
|
|
31
|
50 |
|
public function __construct(Interfaces\IRRTranslations $lang, Sockets\ASocket $method = null) |
32
|
|
|
{ |
33
|
50 |
|
$this->socket = (empty($method)) ? new Sockets\FSocket($lang) : $method ; |
34
|
50 |
|
$this->processor = ($this->socket instanceof Sockets\Socket) |
35
|
1 |
|
? new Pointers\SocketProcessor($lang) |
36
|
50 |
|
: new Pointers\Processor($lang) |
37
|
|
|
; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Interfaces\IConnectionParams $params |
42
|
|
|
* @return Processor |
43
|
|
|
* Necessary to set only once - when you say where you need to connect |
44
|
|
|
*/ |
45
|
9 |
|
public function setConnectionParams(Interfaces\IConnectionParams $params): self |
46
|
|
|
{ |
47
|
9 |
|
$this->socket->close(); // close previously used connection |
48
|
9 |
|
$this->params = $params; |
49
|
9 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
8 |
|
public function setData(?Interfaces\IQuery $request): self |
53
|
|
|
{ |
54
|
8 |
|
$this->data = $request; |
55
|
8 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Process query itself |
60
|
|
|
* @throws RequestException |
61
|
|
|
* @return $this |
62
|
|
|
* processPointer will make a new connection when the current one is not available |
63
|
|
|
*/ |
64
|
7 |
|
public function process(): self |
65
|
|
|
{ |
66
|
7 |
|
$this->processor |
67
|
7 |
|
->setQuery($this->data) |
68
|
7 |
|
->processPointer($this->socket->getResourcePointer($this->params), $this->params) |
69
|
|
|
; |
70
|
4 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* What come back |
75
|
|
|
* @return resource|null |
76
|
|
|
*/ |
77
|
4 |
|
public function getResponse() |
78
|
|
|
{ |
79
|
4 |
|
return $this->processor->getContent(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|