Completed
Push — master ( a94344...0cb987 )
by Danilo
04:54
created

File   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 64
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A getString() 0 4 1
A getResource() 0 10 2
A getFormatName() 0 4 1
A isLocal() 0 17 3
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 isLocal() : bool
45
    {
46
        $host = parse_url($this->file, PHP_URL_HOST);
47
48
        // If it has not an url host
49
        if ($host === null) {
50
            // Is the string a file_id?
51
            if (preg_match('/^([\w\d]*[-_]*[\w\d]*)*$/', $this->file)) {
52
                // It is a file_id
53
                return false;
54
            }
55
            // It is local
56
            return true;
57
        }
58
            // It is an url
59
            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
    public function getString()
67
    {
68
        return $this->file;
69
    }
70
71
    public function getResource()
72
    {
73
        $resource = fopen($this->file, 'r');
74
75
        if ($resource !== false) {
76
            return $resource;
77
        }
78
79
        return false;
80
    }
81
82
    public function getFormatName() : string
83
    {
84
        return $this->format_name;
85
    }
86
}
87