Passed
Push — master ( 06f2b4...a60608 )
by Petr
09:01
created

AProtocol   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 63
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A getParams() 0 3 1
A getQuery() 0 3 1
A getAnswer() 0 16 3
1
<?php
2
3
namespace kalanis\RemoteRequest\Protocols;
4
5
6
use kalanis\RemoteRequest;
7
8
9
/**
10
 * Class AProtocol
11
 * @package kalanis\RemoteRequest\Protocols
12
 * Properties for query to remote server - raw UDP
13
 */
14
abstract class AProtocol
15
{
16
    use RemoteRequest\Traits\TLang;
17
18
    protected RemoteRequest\Connection\Processor $processor;
19
    protected RemoteRequest\Connection\Params\AParams $params;
20
    protected Dummy\Query $query;
21
    protected Dummy\Answer $answer;
22
23
    /**
24
     * @param array<string, array<string, string>|string> $contextOptions
25
     * @param bool $long
26
     * @param RemoteRequest\Interfaces\IRRTranslations|null $lang
27
     */
28 8
    public function __construct(array $contextOptions = [], bool $long = false, ?RemoteRequest\Interfaces\IRRTranslations $lang = null)
29
    {
30 8
        $this->setRRLang($lang);
31 8
        $pointer = empty($contextOptions)
32 7
            ? ($long ? new RemoteRequest\Sockets\PfSocket($lang) : new RemoteRequest\Sockets\FSocket($lang))
33 8
            : (new RemoteRequest\Sockets\Stream($lang))->setContextOptions($contextOptions) ;
34 8
        $this->processor = new RemoteRequest\Connection\Processor($pointer, $lang);
35 8
        $this->params = $this->loadParams();
36 8
        $this->query = $this->loadQuery();
37 8
        $this->answer = $this->loadAnswer();
38
    }
39
40
    abstract protected function loadParams(): RemoteRequest\Connection\Params\AParams;
41
42
    abstract protected function loadQuery(): Dummy\Query;
43
44
    abstract protected function loadAnswer(): Dummy\Answer;
45
46 7
    public function getParams(): RemoteRequest\Connection\Params\AParams
47
    {
48 7
        return $this->params;
49
    }
50
51 7
    public function getQuery(): Dummy\Query
52
    {
53 7
        return $this->query;
54
    }
55
56
    /**
57
     * @throws RemoteRequest\RequestException
58
     * @return Dummy\Answer
59
     * @codeCoverageIgnore because it's about querying remote machine
60
     */
61
    public function getAnswer(): Dummy\Answer
62
    {
63
        $target = $this->query;
64
        if (empty($this->params->getHost())
65
            && ($target instanceof RemoteRequest\Interfaces\ITarget)) {
66
            $this->params->setTarget($target->getHost(), $target->getPort());
67
        }
68
69
        $this->answer->setResponse(
70
            $this->processor
71
                ->setConnectionParams($this->params)
72
                ->setData($this->query)
73
                ->process()
74
                ->getResponse()
75
        );
76
        return $this->answer;
77
    }
78
}
79