DownloadCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 68
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 14 1
A getFileNameWithPath() 0 4 1
A getFilePath() 0 4 2
A getFileName() 0 4 2
1
<?php
2
3
namespace Ngtfkx\Laradeck\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\Storage;
7
8
class DownloadCommand extends Command
9
{
10
    protected $signature = 'laradeck:download 
11
                            {url} 
12
                            {--name= : Save file with name} 
13
                            {--path= : Path relative storage/app}';
14
15
    protected $description = 'Download file';
16
17
    /**
18
     * @var string
19
     */
20
    protected $url;
21
22
    /**
23
     * @var string
24
     */
25
    protected $file;
26
27
    /**
28
     * @var string
29
     */
30
    protected $path;
31
32
    public function __construct()
33
    {
34
        parent::__construct();
35
    }
36
37
    public function handle()
38
    {
39
        $this->url = $this->argument('url');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->argument('url') can also be of type array. However, the property $url 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...
40
41
        $this->file = $this->getFileName();
42
43
        $this->path = $this->getFilePath();
44
45
        $resource = fopen($this->url, 'r');
46
47
        Storage::put($this->getFileNameWithPath(), $resource);
48
49
        fclose($resource);
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    protected function getFileNameWithPath(): string
56
    {
57
        return $this->path . DIRECTORY_SEPARATOR . $this->file;
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    protected function getFilePath(): string
64
    {
65
        return $this->option('path') ?: '';
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    protected function getFileName(): string
72
    {
73
        return $this->option('name') ?: array_slice(explode('/', $this->url), -1)[0];
74
    }
75
}
76