Passed
Push — master ( c8983c...06f2b4 )
by Petr
08:23
created

Runner   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 113
rs 10
c 0
b 0
f 0
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getConnectParams() 0 3 1
A getTimeout() 0 3 1
A getActionQuery() 0 6 2
A process() 0 25 3
A sendBye() 0 9 2
A __construct() 0 8 1
A __destruct() 0 4 2
A setActionQuery() 0 4 1
A getQuery() 0 3 1
1
<?php
2
3
namespace kalanis\RemoteRequest\Wrappers\Fsp;
4
5
6
use kalanis\RemoteRequest\Connection;
7
use kalanis\RemoteRequest\Interfaces\IRRTranslations;
8
use kalanis\RemoteRequest\Interfaces\ISchema;
9
use kalanis\RemoteRequest\Protocols\Fsp as Protocol;
10
use kalanis\RemoteRequest\RequestException;
11
use kalanis\RemoteRequest\Sockets;
12
use kalanis\RemoteRequest\Traits\TLang;
13
14
15
/**
16
 * Class Runner
17
 * @package kalanis\RemoteRequest\Wrappers\Fsp
18
 * Runner for FSP under RemoteRequest
19
 */
20
class Runner
21
{
22
    use TLang;
23
24
    protected Connection\Params\AParams $params;
25
    protected Connection\Processor $processor;
26
    protected Protocol\Query $query;
27
    protected Protocol\Answer $answer;
28
    protected Protocol\Session $session;
29
    protected ?Protocol\Query\AQuery $actionQuery = null;
30
31
    /**
32
     * @param IRRTranslations $lang
33
     * @throws RequestException
34
     */
35
    public function __construct(IRRTranslations $lang)
36
    {
37
        $this->setRRLang($lang);
38
        $this->params = Connection\Params\Factory::getForSchema(ISchema::SCHEMA_UDP, $lang);
39
        $this->processor = new Connection\Processor(new Sockets\Socket($lang), $lang);
40
        $this->query = new Protocol\Query();
41
        $this->answer = new Protocol\Answer($lang);
42
        $this->session = new Protocol\Session($lang);
43
    }
44
45
    /**
46
     * @throws RequestException
47
     */
48
    public function __destruct()
49
    {
50
        if ($this->session->hasKey()) {
51
            $this->sendBye();
52
        }
53
    }
54
55
    public function setActionQuery(Protocol\Query\AQuery $query): self
56
    {
57
        $this->actionQuery = $query;
58
        return $this;
59
    }
60
61
    /**
62
     * @throws RequestException
63
     * @return Protocol\Query\AQuery
64
     */
65
    protected function getActionQuery(): Protocol\Query\AQuery
66
    {
67
        if (!$this->actionQuery) {
68
            throw new RequestException($this->getRRLang()->rrFspNoAction());
69
        }
70
        return $this->actionQuery;
71
    }
72
73
    public function getQuery(): Protocol\Query
74
    {
75
        return $this->query;
76
    }
77
78
    public function getConnectParams(): Connection\Params\AParams
79
    {
80
        return $this->params;
81
    }
82
83
    public function getTimeout(/** @scrutinizer ignore-unused */ string $host): int
84
    {
85
        return 10;
86
        // this one will be based on info from server session
87
    }
88
89
    /**
90
     * Process queries to remote machine
91
     * @throws RequestException
92
     * @return Protocol\Answer\AAnswer
93
     */
94
    public function process(): Protocol\Answer\AAnswer
95
    {
96
        if (empty($this->params->getHost())) {
97
            throw new RequestException($this->getRRLang()->rrFspNoTarget());
98
        }
99
        $this->session->setHost($this->params->getHost());
100
        $this->getActionQuery()
101
            ->setKey($this->session->getKey())
102
            ->setSequence($this->session->getSequence())
103
            ->compile()
104
        ;
105
        $response = $this->processor->setConnectionParams($this->params)->setData($this->query)->process()->getResponse();
106
        $answer = Protocol\Answer\AnswerFactory::getObject(
107
            $this->answer->setResponse(
108
                $response
109
            )->process()
110
        );
111
        if ($answer instanceof Protocol\Answer\Error) {
112
            throw $answer->getError();
113
        }
114
        $this->session
115
            ->setKey($answer->getDataClass()->getKey())
116
            ->updateSequence($answer->getDataClass()->getSequence())
117
        ;
118
        return $answer;
119
    }
120
121
    /**
122
     * @throws RequestException
123
     */
124
    protected function sendBye(): void
125
    {
126
        $answer = $this->setActionQuery(new Protocol\Query\Bye($this->getQuery()))
127
            ->process()
128
        ;
129
        if (!$answer instanceof Protocol\Answer\Nothing) {
130
            throw new RequestException($this->getRRLang()->rrFspBadResponseClose(get_class($answer)));
131
        }
132
        $this->session->clear();
133
    }
134
}
135