Completed
Push — master ( e9be36...9488e8 )
by Camilo
04:57
created

TelegramDocument::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 9.6666
cc 1
eloc 5
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace unreal4u\TelegramAPI\InternalFunctionality;
6
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * Used when we download a file from Telegram, contains some important headers information that else would be lost
11
 */
12
class TelegramDocument
13
{
14
    /**
15
     * The mime type of this file
16
     * @var string
17
     */
18
    public $mime_type = '';
19
20
    /**
21
     * The actual contents of this file
22
     * @var string
23
     */
24
    public $contents = '';
25
26
    /**
27
     * The file size
28
     * @var int
29
     */
30
    public $file_size = 0;
31
32
    /**
33
     * Constructs a representable document
34
     *
35
     * @param ResponseInterface $response
36
     */
37
    public function __construct(ResponseInterface $response)
38
    {
39
        $headers = $response->getHeaders();
40
        // What better to get the mime type than what the Telegram servers already send us?
41
        $this->mime_type = $headers['Content-Type'][0];
42
        // Same with file length
43
        $this->file_size = $headers['Content-Length'][0];
44
        $this->contents = (string)$response->getBody();
45
    }
46
47
    /**
48
     * When called with string-ish functions, assume we want the actual contents of the file
49
     * @return string
50
     */
51
    public function __toString()
52
    {
53
        return $this->contents;
54
    }
55
}
56