Completed
Push — master ( 640da7...eb7508 )
by Danilo
03:54
created

File::is_local()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 0
crap 12
1
<?php
2
3
namespace PhpBotFramework\Entities;
4
5
use PhpBotFramework\Exceptions\BotException;
6
7
class File
8
{
9
    private $file;
10
11
    private $format_name;
12
13
    /**
14
     * \brief Fill this object with another file.
15
     * @param string $file File_id or local/remote path to the file.
16
     * @param string $format_name Format name of the file (audio, document, ...)
17
     */
18
    public function init(string $file, string $format_name)
19
    {
20
        $this->file = $file;
21
        $this->format_name = $format_name;
22
    }
23
24
    /**
25
     * \brief (<i>Internal</i>) Check if the path to the file given is local or a file_id/url.
26
     * @return bool True if the file is a local path.
27
     */
28
    public function is_local() : bool
29
    {
30
        $host = parse_url($this->file, PHP_URL_HOST);
31
32
        // If it has not an url host and it is a file_id
33
        if ($host === null && !is_numeric($this->file)) {
34
            // Then it is a local path
35
            return true;
36
        } else {
37
            return false;
38
        }
39
    }
40
41
    /**
42
     * \brief (<i>Internal</i>) Get string component of this file.
43
     * @return string File as string (file_id/path to the file).
44
     */
45
    public function getString()
46
    {
47
        return $this->file;
48
    }
49
50
    public function getResource()
51
    {
52
        $resource = fopen($this->file, 'r');
53
54
        if ($resource !== false) {
55
            return $resource;
56
        }
57
58
        return false;
59
    }
60
61
    public function getFormatName() : string
62
    {
63
        return $this->format_name;
64
    }
65
}
66