Completed
Push — master ( 85cb1a...6af1ba )
by
unknown
27:56 queued 25:08
created

File::getFormatName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 7
    public function init(string $file, string $format_name)
35
    {
36 7
        $this->file = $file;
37 7
        $this->format_name = $format_name;
38 7
    }
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 7
    public function isLocal() : bool
45
    {
46 7
        $host = parse_url($this->file, PHP_URL_HOST);
47
48
        // If it has not an url host
49 7
        if ($host === null) {
50
            // Is the string a file_id?
51 3
            if (preg_match('/^([\w\d]*[-_]*[\w\d]*)*$/', $this->file)) {
52
                // It is a file_id
53
                return false;
54
            }
55
            // It is local
56 3
            return true;
57
        }
58
            // It is an url
59 4
            return false;
60
    }
61
62
    /**
63
     * \brief (<i>Internal</i>) Get string component of this file.
64
     * @return string File as string (file_id/path to the file).
65
     */
66 4
    public function getString()
67
    {
68 4
        return $this->file;
69
    }
70
71 3
    public function getResource()
72
    {
73 3
        $resource = fopen($this->file, 'r');
74
75 3
        if ($resource !== false) {
76 3
            return $resource;
77
        }
78
79
        return false;
80
    }
81
82 7
    public function getFormatName() : string
83
    {
84 7
        return $this->format_name;
85
    }
86
}
87