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.

File::open()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 10
cts 11
cp 0.9091
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 5
nop 0
crap 5.0187
1
<?php
2
3
namespace Tgallice\FBMessenger\Model\Attachment;
4
5
use Tgallice\FBMessenger\ExtensionChecker;
6
use Tgallice\FBMessenger\Model\Attachment;
7
8
class File extends Attachment
9
{
10
    /**
11
     * @var string|null
12
     */
13
    private $path;
14
15
    /**
16
     * @var bool
17
     */
18
    private $isRemoteFile;
19
20
    /**
21
     * @var null|resource
22
     */
23
    private $stream;
24
25
    /**
26
     * @var array
27
     */
28
    protected $allowedExtensions = [];
29
30
    /**
31
     * @param string $type
32
     * @param string $filePath
33
     */
34 19
    public function __construct($filePath, $type = Attachment::TYPE_FILE)
35
    {
36 19
        $this->path = $filePath;
37
38
        // Local image is sent with a multipart request
39 19
        $payload = [];
40
41 19
        if ($this->isRemoteFile()) {
42
            $payload = [
43 2
                'url' => $this->path,
44 2
            ];
45 2
        }
46
47 19
        $this->validateExtension();
48
49 18
        parent::__construct($type, $payload);
50 18
    }
51
52
    /**
53
     * @return string
54
     */
55 1
    public function getPath()
56
    {
57 1
        return $this->path;
58
    }
59
60 3
    public function open()
61
    {
62 3
        if ($this->stream) {
63 1
            return;
64
        }
65
66 3
        if ($this->isRemoteFile()) {
67 1
            throw new \RuntimeException('A remote file can not be open');
68
        }
69
        
70 2
        if (!is_readable($this->path)) {
71 1
            throw new \InvalidArgumentException(sprintf('The file "%s" should be readable', $this->path));
72
        }
73
74 1
        $this->stream = fopen($this->path, 'r');
75
76 1
        if (!$this->stream) {
77
            throw new \InvalidArgumentException(sprintf('Unable to open the media "%s"', $this->path));
78
        }
79 1
    }
80
81
    /**
82
     * @return null|resource
83
     */
84 1
    public function getStream()
85
    {
86 1
        $this->open();
87
88 1
        return $this->stream;
89
    }
90
91
    /**
92
     * @return bool
93
     */
94 19
    public function isRemoteFile()
95
    {
96 19
        if (isset($this->isRemoteFile)) {
97 5
            return $this->isRemoteFile;
98
        }
99
100 19
        return $this->isRemoteFile = preg_match('/^(https?|ftp):\/\/.*/', $this->path) === 1;
101
    }
102
103 19
    private function validateExtension()
104
    {
105 19
        if (!empty($this->allowedExtensions) && !ExtensionChecker::check($this->path, $this->allowedExtensions)) {
106 1
            throw new \InvalidArgumentException(
107 1
                sprintf('"%s" this file has not an allowed extension. Only %s extensions are supported', $this->path, '["'. implode('", "', $this->allowedExtensions) .'"]')
108 1
            );
109
        }
110 18
    }
111
}
112