Completed
Push — master ( 3845e2...a95f7d )
by Danilo
09:37
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 5
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
/*
4
 * This file is part of the PhpBotFramework.
5
 *
6
 * PhpBotFramework is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, version 3.
9
 *
10
 * PhpBotFramework is distributed in the hope that it will be useful, but
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace PhpBotFramework\Entities;
20
21
use PhpBotFramework\Exceptions\BotException;
22
23
class File
24
{
25
    private $file;
26
27
    private $format_name;
28
29
    /**
30
     * \brief Fill this object with another file.
31
     * @param string $file File_id or local/remote path to the file.
32
     * @param string $format_name Format name of the file (audio, document, ...)
33
     */
34
    public function init(string $file, string $format_name)
35
    {
36
        $this->file = $file;
37
        $this->format_name = $format_name;
38
    }
39
40
    /**
41
     * \brief (<i>Internal</i>) Check if the path to the file given is local or a file_id/url.
42
     * @return bool True if the file is a local path.
43
     */
44
    public function is_local() : bool
45
    {
46
        $host = parse_url($this->file, PHP_URL_HOST);
47
48
        // If it has not an url host and it is a file_id
49
        if ($host === null && !is_numeric($this->file)) {
50
            // Then it is a local path
51
            return true;
52
        } else {
53
            return false;
54
        }
55
    }
56
57
    /**
58
     * \brief (<i>Internal</i>) Get string component of this file.
59
     * @return string File as string (file_id/path to the file).
60
     */
61
    public function getString()
62
    {
63
        return $this->file;
64
    }
65
66
    public function getResource()
67
    {
68
        $resource = fopen($this->file, 'r');
69
70
        if ($resource !== false) {
71
            return $resource;
72
        }
73
74
        return false;
75
    }
76
77
    public function getFormatName() : string
78
    {
79
        return $this->format_name;
80
    }
81
}
82