Completed
Push — master ( 3b23d7...f7166a )
by Camilo
11s
created

TelegramDocument   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 47
ccs 8
cts 9
cp 0.8889
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A __toString() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace unreal4u\TelegramAPI\InternalFunctionality;
6
7
/**
8
 * Used when we download a file from Telegram, contains some important headers information that else would be lost
9
 */
10
class TelegramDocument
11
{
12
    /**
13
     * The mime type of this file
14
     * @var string
15
     */
16
    public $mime_type = '';
17
18
    /**
19
     * The actual contents of this file
20
     * @var string
21
     */
22
    public $contents = '';
23
24
    /**
25
     * The file size
26
     * @var int
27
     */
28
    public $file_size = 0;
29
30
    /**
31
     * Constructs a representable document
32
     *
33
     * @param TelegramResponse $response
34
     */
35 1
    public function __construct(TelegramResponse $response)
36
    {
37 1
        $headers = (array) $response->getHeaders();
38
39
        // What better to get the mime type than what the Telegram servers already send us?
40 1
        $this->mime_type = !empty($headers['Content-Type']) ? $headers['Content-Type'] : 'application/octet-stream';
41
42
        // Same with file length
43 1
        $this->file_size = !empty($headers['Content-Length']) ? $headers['Content-Length']
44
            : strlen($response->getRawData());
45 1
        $this->contents = $response->getRawData();
46 1
    }
47
48
    /**
49
     * When called with string-ish functions, assume we want the actual contents of the file
50
     * @return string
51
     */
52 1
    public function __toString()
53
    {
54 1
        return $this->contents;
55
    }
56
}
57