File::getContent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
crap 2
1
<?php
2
3
namespace kalanis\RemoteRequest\Protocols\Http\Query;
4
5
6
use kalanis\RemoteRequest\Protocols\Helper;
7
use kalanis\RemoteRequest\RequestException;
8
9
10
/**
11
 * Class File
12
 * @package kalanis\RemoteRequest\Protocols\Http\Query
13
 * Single item for query - file
14
 * Beware: The content must be already loaded into memory, not stay on volume; there is no additional loading (for reasons)
15
 */
16
class File extends Value
17
{
18
    /** @var string|resource */
19
    protected $content = '';
20
    public string $filename = 'binary';
21
    public string $mime = 'octet/stream';
22
23 4
    public function getFilename(): string
24
    {
25 4
        return strval($this->filename);
26
    }
27
28 4
    public function getMimeType(): string
29
    {
30 4
        return strval($this->mime);
31
    }
32
33 3
    public function getContent(): string
34
    {
35 3
        return is_resource($this->content)
36 1
            ? strval(stream_get_contents($this->content, -1, 0))
37 3
            : strval($this->content)
38 3
        ;
39
    }
40
41
    /**
42
     * @throws RequestException
43
     * @return resource
44
     */
45 3
    public function getStream()
46
    {
47 3
        if (is_resource($this->content)) {
48 1
            return $this->content;
49
        }
50 3
        $stream = Helper::getMemStorage();
51 3
        fwrite($stream, strval($this->content));
52 3
        rewind($stream);
53 3
        return $stream;
54
    }
55
}
56