GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 64a210...2d6991 )
by Stan
02:42
created

File::download()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 1
Metric Value
c 3
b 2
f 1
dl 0
loc 22
rs 8.6737
cc 5
eloc 12
nc 5
nop 1
1
<?php
2
3
namespace Teebot\Entity;
4
5
use Teebot\Command\Executor;
6
use Teebot\Exception\Critical;
7
use Teebot\Exception\Output;
8
9
class File extends AbstractEntity
10
{
11
    const ENTITY_TYPE = 'File';
12
13
    protected $file_id;
14
15
    protected $file_size;
16
17
    protected $file_path;
18
19
    /**
20
     * @return mixed
21
     */
22
    public function getFileId()
23
    {
24
        return $this->file_id;
25
    }
26
27
    /**
28
     * @param mixed $file_id
29
     *
30
     * @return $this
31
     */
32
    public function setFileId($file_id)
33
    {
34
        $this->file_id = $file_id;
35
36
        return $this;
37
    }
38
39
    /**
40
     * @return mixed
41
     */
42
    public function getFileSize()
43
    {
44
        return $this->file_size;
45
    }
46
47
    /**
48
     * @param mixed $file_size
49
     *
50
     * @return $this
51
     */
52
    public function setFileSize($file_size)
53
    {
54
        $this->file_size = $file_size;
55
56
        return $this;
57
    }
58
59
    /**
60
     * @return mixed
61
     */
62
    public function getFilePath()
63
    {
64
        return $this->file_path;
65
    }
66
67
    /**
68
     * @param mixed $file_path
69
     *
70
     * @return $this
71
     */
72
    public function setFilePath($file_path)
73
    {
74
        $this->file_path = $file_path;
75
76
        return $this;
77
    }
78
79
    public function getFullPath()
80
    {
81
        if (!$this->file_path) {
82
            return null;
83
        }
84
85
        $basePath = Executor::getInstance()
86
            ->getConfig()
87
            ->getFileBasePath();
88
89
        if (!$basePath) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $basePath of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
90
            return null;
91
        }
92
93
        return $basePath . $this->file_path;
94
    }
95
96
    public function download($storePath)
97
    {
98
        if (file_exists($storePath) && !is_writable($storePath)) {
99
            throw new Critical('File "' . $storePath . '" is already exist!"');
100
        }
101
102
        $filePath = $this->getFullPath();
103
104
        if (!$filePath) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filePath of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
105
            throw new Critical('Unable to get download path to file!');
106
        }
107
108
        try {
109
            $contents = file_get_contents($filePath);
110
111
            file_put_contents($storePath, $contents);
112
        } catch (\Exception $e) {
113
            Output::log(new Critical($e->getMessage()));
114
        }
115
116
        return is_readable($storePath);
117
    }
118
}