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 ( 53cd30...da90f7 )
by Gallice
43s
created

Image::validateFile()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.2
cc 4
eloc 6
nc 3
nop 0
crap 4
1
<?php
2
3
namespace Tgallice\FBMessenger\Attachment;
4
5
use Tgallice\FBMessenger\Attachment;
6
use Tgallice\FBMessenger\ExtensionChecker;
7
8
class Image extends Attachment
9
{
10
    /**
11
     * @var bool
12
     */
13
    private $isRemoteFile;
14
15
    /**
16
     * @var string
17
     */
18
    private $path;
19
20
    /**
21
     * @param string $filePath File path of the local/remote file
22
     */
23 13
    public function __construct($filePath)
24
    {
25 13
        $this->path = $filePath;
26 13
        $this->type = Attachment::TYPE_IMAGE;
27
28
        // Local image is sent with a multipart request
29 13
        $this->payload = [];
30
31 13
        if ($this->isRemoteFile()) {
32 2
            $this->payload = [
33 2
                'url' => $this->path,
34
            ];
35 2
        }
36
37 13
        $this->validateFile();
38 11
    }
39
40
    /**
41
     * @return string
42
     */
43 1
    public function getPath()
44
    {
45 1
        return $this->path;
46
    }
47
48
    /**
49
     * @return resource
50
     */
51 2
    public function open()
52
    {
53 2
        if ($this->isRemoteFile) {
54 1
            throw new \RuntimeException('A remote file can not be open');
55
        }
56
57 1
        return fopen($this->path, 'r');
58
    }
59
60
    /**
61
     * @return bool
62
     */
63 13
    public function isRemoteFile()
64
    {
65 13
        if (isset($this->isRemoteFile)) {
66 12
            return $this->isRemoteFile;
67
        }
68
69 13
        return $this->isRemoteFile = preg_match('/^(https?|ftp):\/\/.*/', $this->path) === 1;
70
    }
71
72 13
    private function validateFile()
73
    {
74 13
        if (!ExtensionChecker::check($this->path, ['jpg', 'png'])) {
75 1
            throw new \InvalidArgumentException(
76 1
                sprintf('"%s" this file has not an allowed extension. Only "png" and "jpg" image are supported', $this->path)
77 1
            );
78
        }
79
80 12
        if (!$this->isRemoteFile() && !is_readable($this->path)) {
81 1
            throw new \InvalidArgumentException(sprintf('The file "%s" should be readable', $this->path));
82
        }
83 11
    }
84
}
85