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

Processor::getContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace kalanis\RemoteRequest\Pointers;
4
5
6
use kalanis\RemoteRequest\Interfaces\IConnectionParams;
7
use kalanis\RemoteRequest\Interfaces\IQuery;
8
use kalanis\RemoteRequest\Interfaces\IRRTranslations;
9
use kalanis\RemoteRequest\Protocols\Helper;
10
use kalanis\RemoteRequest\RequestException;
11
12
13
/**
14
 * Class Processor
15
 * @package kalanis\RemoteRequest\Pointers
16
 * Query to the remote server - read into provided output
17
 */
18
class Processor
19
{
20
    /** @var int how many bytes for load split */
21
    const PART_SPLIT = 1024;
22
23
    /** @var IRRTranslations */
24
    protected $lang = null;
25
    /** @var IQuery|null */
26
    protected $remoteQuery = null;
27
    /** @var resource|null */
28
    protected $remoteResponse = null;
29
30 50
    public function __construct(IRRTranslations $lang)
31
    {
32 50
        $this->lang = $lang;
33 50
    }
34
35 7
    public function setQuery(?IQuery $content): self
36
    {
37 7
        $this->remoteQuery = $content;
38 7
        return $this;
39
    }
40
41
    /**
42
     * @param resource|null $filePointer
43
     * @param IConnectionParams $params
44
     * @throws RequestException
45
     * @return $this
46
     * @codeCoverageIgnore because accessing remote resources, similar code is in overwrite
47
     */
48 1
    public function processPointer($filePointer, IConnectionParams $params): self
49
    {
50 1
        $this->checkQuery();
51 1
        $this->checkPointer($filePointer);
52
        $this->writeRequest($filePointer, $params); // @phpstan-ignore-line
53
        $this->readResponse($filePointer); // @phpstan-ignore-line
54
        return $this;
55
    }
56
57
    /**
58
     * @param resource $filePointer
59
     * @param IConnectionParams $params
60
     * @throws RequestException
61
     * @return $this
62
     */
63 4
    protected function writeRequest($filePointer, IConnectionParams $params): self
64
    {
65 4
        $this->checkQuery();
66
        // @phpstan-ignore-next-line
67 4
        $srcStream = $this->remoteQuery->getData(); // always exists - checked
68 4
        rewind($srcStream);
69 4
        stream_copy_to_stream($srcStream, $filePointer);
70 4
        return $this;
71
    }
72
73
    /**
74
     * @param resource $filePointer
75
     * @throws RequestException
76
     * @return $this
77
     */
78 4
    protected function readResponse($filePointer): self
79
    {
80 4
        $this->checkQuery();
81 4
        $this->remoteResponse = null;
82
83
        // Read the server response
84 4
        $response = Helper::getTempStorage();
85
        // @phpstan-ignore-next-line
86 4
        $bytesLeft = $this->remoteQuery->getMaxAnswerLength(); // always exists - checked
87 4
        stream_copy_to_stream($filePointer, $response, (is_null($bytesLeft) ? -1 : $bytesLeft), 0);
88 4
        rewind($response);
89 4
        $this->remoteResponse = $response;
90 4
        return $this;
91
    }
92
93
    /**
94
     * @throws RequestException
95
     */
96 6
    protected function checkQuery(): void
97
    {
98 6
        if (empty($this->remoteQuery)
99 6
            || !($this->remoteQuery instanceof IQuery)) {
100 1
            throw new RequestException($this->lang->rrPointUnknownTarget());
101
        }
102 5
    }
103
104
    /**
105
     * @param resource|null $filePointer
106
     * @throws RequestException
107
     */
108 5
    protected function checkPointer($filePointer): void
109
    {
110 5
        if (empty($filePointer)
111 5
            || !is_resource($filePointer)) {
112 1
            throw new RequestException($this->lang->rrPointNoStreamPointer());
113
        }
114 4
    }
115
116
    /**
117
     * @return resource|null
118
     */
119 4
    public function getContent()
120
    {
121 4
        return $this->remoteResponse;
122
    }
123
}
124