ExternalResource   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 103
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setSource() 0 5 1
A getName() 0 4 1
A getSize() 0 4 1
A getMime() 0 4 1
A getTemp() 0 4 1
A copyTempResource() 0 15 3
B checkAvailable() 0 29 3
1
<?php
2
/**
3
 * ExternalResource.php
4
 * @author Revin Roman http://phptime.ru
5
 */
6
7
namespace rmrevin\yii\module\File\resources;
8
9
/**
10
 * Class ExternalResource
11
 * @package rmrevin\yii\module\File\resources
12
 */
13
class ExternalResource extends AbstractResource implements ResourceInterface
14
{
15
16
    /** @var string */
17
    private $temp;
18
19
    /** @var string */
20
    private $source;
21
22
    /**
23
     * @param string $source
24
     */
25 2
    public function setSource($source)
26
    {
27 2
        $this->source = $source;
28 2
        $this->temp = $this->copyTempResource();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->copyTempResource() can also be of type false. However, the property $temp is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
29 1
    }
30
31
    /**
32
     * @return string
33
     */
34 1
    public function getName()
35
    {
36 1
        return basename($this->source);
37
    }
38
39
    /**
40
     * @return int
41
     */
42 1
    public function getSize()
43
    {
44 1
        return filesize($this->temp);
45
    }
46
47
    /**
48
     * @return string
49
     * @throws \yii\base\InvalidConfigException
50
     */
51 1
    public function getMime()
52
    {
53 1
        return \yii\helpers\FileHelper::getMimeType($this->temp);
54
    }
55
56
    /**
57
     * @return string
58
     */
59 1
    public function getTemp()
60
    {
61 1
        return $this->temp;
62
    }
63
64
    /**
65
     * @return string|false
66
     */
67 2
    private function copyTempResource()
68
    {
69 2
        if (false === $this->checkAvailable()) {
70 1
            throw new \RuntimeException('External resource not available');
71
        }
72
73 1
        $url = parse_url($this->source);
74 1
        $ext = pathinfo($url['path'], PATHINFO_EXTENSION);
75 1
        $temp = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('file-') . '.' . $ext;
76
77 1
        $result = copy($this->source, $temp);
78 1
        @chmod($temp, 0664);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
79
80 1
        return true === $result ? $temp : false;
81
    }
82
83
    /**
84
     * @return bool
85
     */
86 2
    private function checkAvailable()
87
    {
88 2
        $result = false;
89
90 2
        $ch = curl_init($this->source);
91 2
        curl_setopt_array($ch, [
92 2
            CURLOPT_HEADER => true,
93 2
            CURLOPT_FOLLOWLOCATION => true,
94 2
            CURLOPT_RETURNTRANSFER => true,
95 2
            CURLOPT_NOBODY => true,
96 2
            CURLOPT_TIMEOUT => 30,
97 2
            CURLOPT_SSL_VERIFYHOST => false,
98 2
            CURLOPT_SSL_VERIFYPEER => false,
99 2
        ]);
100
101 2
        curl_exec($ch);
102
103 2
        if (!curl_errno($ch)) {
104 1
            $info = curl_getinfo($ch);
105
106 1
            if ($info['http_code'] === 200) {
107 1
                $result = true;
108 1
            }
109 1
        }
110
111 2
        curl_close($ch);
112
113 2
        return $result;
114
    }
115
}