Completed
Push — master ( 6cfdea...faed68 )
by Denis
04:07
created

DownloadCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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');
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