Passed
Push — master ( edb799...f409da )
by Petr
07:56
created

AProtocol::getAnswer()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 2
nop 0
dl 0
loc 16
ccs 0
cts 12
cp 0
crap 12
rs 9.9
c 0
b 0
f 0
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
    /** @var RemoteRequest\Connection\Processor */
17
    protected $processor = null;
18
    /** @var RemoteRequest\Connection\Params\AParams */
19
    protected $params = null;
20
    /** @var Dummy\Query */
21
    protected $query = null;
22
    /** @var Dummy\Answer */
23
    protected $answer = null;
24
    /** @var RemoteRequest\Interfaces\IRRTranslations */
25
    protected $lang = null;
26
27
    /**
28
     * @param RemoteRequest\Interfaces\IRRTranslations $lang
29
     * @param array<string, array<string, string>|string> $contextOptions
30
     * @param bool $long
31
     */
32 8
    public function __construct(RemoteRequest\Interfaces\IRRTranslations $lang, array $contextOptions = [], bool $long = false)
33
    {
34 8
        $this->lang = $lang;
35 8
        $pointer = empty($contextOptions)
36 8
            ? ($long ? new RemoteRequest\Sockets\PfSocket($lang) : new RemoteRequest\Sockets\FSocket($lang))
37 8
            : (new RemoteRequest\Sockets\Stream($lang))->setContextOptions($contextOptions) ;
38 8
        $this->processor = new RemoteRequest\Connection\Processor($lang, $pointer);
39 8
        $this->params = $this->loadParams();
40 8
        $this->query = $this->loadQuery();
41 8
        $this->answer = $this->loadAnswer();
42 8
    }
43
44
    abstract protected function loadParams(): RemoteRequest\Connection\Params\AParams;
45
46
    abstract protected function loadQuery(): Dummy\Query;
47
48
    abstract protected function loadAnswer(): Dummy\Answer;
49
50 7
    public function getParams(): RemoteRequest\Connection\Params\AParams
51
    {
52 7
        return $this->params;
53
    }
54
55 7
    public function getQuery(): Dummy\Query
56
    {
57 7
        return $this->query;
58
    }
59
60
    /**
61
     * @throws RemoteRequest\RequestException
62
     * @return Dummy\Answer
63
     * @codeCoverageIgnore because it's about querying remote machine
64
     */
65
    public function getAnswer(): Dummy\Answer
66
    {
67
        $target = $this->query;
68
        if (empty($this->params->getHost())
69
            && ($target instanceof RemoteRequest\Interfaces\ITarget)) {
70
            $this->params->setTarget($target->getHost(), $target->getPort());
71
        }
72
73
        $this->answer->setResponse(
74
            $this->processor
75
                ->setConnectionParams($this->params)
76
                ->setData($this->query)
77
                ->process()
78
                ->getResponse()
79
        );
80
        return $this->answer;
81
    }
82
}
83