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
Pull Request — master (#9)
by Gallice
02:33
created

File   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.22%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 104
ccs 35
cts 36
cp 0.9722
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 2
A getPath() 0 4 1
B open() 0 20 5
A getStream() 0 6 1
A isRemoteFile() 0 8 2
A validateExtension() 0 8 3
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