Processor::setData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
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
use kalanis\RemoteRequest\Traits\TLang;
11
12
13
/**
14
 * Class Processor
15
 * @package kalanis\RemoteRequest\Connection
16
 * Query to the remote server - completing and simple processing
17
 * This is the network layer, what lies elsewhere and what has been sent through is not important for processing
18
 * It only needs to know destination, message to the destination and how to get response from remote; not how to make
19
 * something sane from response. That is not responsibility of these classes.
20
 */
21
class Processor
22
{
23
    use TLang;
24
25
    protected ?Interfaces\IQuery $data = null;
26
    protected Pointers\Processor $processor;
27
    protected ?Interfaces\IConnectionParams $params = null;
28
    protected Sockets\ASocket $socket;
29
30 51
    public function __construct(?Sockets\ASocket $method = null, ?Interfaces\IRRTranslations $lang = null)
31
    {
32 51
        $this->setRRLang($lang);
33 51
        $this->socket = $method ?: new Sockets\FSocket($lang);
34 51
        $this->processor = ($this->socket instanceof Sockets\Socket)
35 1
            ? new Pointers\SocketProcessor($lang)
36 51
            : new Pointers\Processor($lang)
37 51
        ;
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 9
    public function setData(?Interfaces\IQuery $request): self
53
    {
54 9
        $this->data = $request;
55 9
        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 8
    public function process(): self
65
    {
66 8
        if (!$this->params) {
67 1
            throw new RequestException($this->getRRLang()->rrPointUnknownTarget());
68
        }
69 7
        $this->processor
70 7
            ->setQuery($this->data)
71 7
            ->processPointer($this->socket->getResourcePointer($this->params), $this->params)
72 7
        ;
73 4
        return $this;
74
    }
75
76
    /**
77
     * What come back
78
     * @return resource|null
79
     */
80 4
    public function getResponse()
81
    {
82 4
        return $this->processor->getContent();
83
    }
84
}
85