Completed
Push — master ( 1b7481...344f58 )
by Saurabh
01:12
created

SetTempUrlKey::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace Sausin\LaravelOvh\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\Storage;
7
8
class SetTempUrlKey extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'ovh:set-temp-url-key
16
                            {--key= : The key you want to setup on your container}
17
                            {--force : Forcibly set a new key on the container}';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Set temp url key on private container, making the use of Storage::temporaryUrl() possible';
25
26
    /**
27
     * Create a new command instance.
28
     *
29
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
30
     */
31
    public function __construct()
32
    {
33
        parent::__construct();
34
    }
35
36
    /**
37
     * Execute the console command.
38
     *
39
     * @return mixed
40
     */
41
    public function handle()
42
    {
43
        if ($this->option('force')) {
44
            $this->setContainerKey();
45
46
            return 0;
47
        }
48
49
        if ($this->checkContainerHasKey()) {
50
            $this->info('Container already has a key');
51
52
            return 1;
53
        }
54
55
        $this->setContainerKey();
56
    }
57
58
    protected function setContainerKey()
59
    {
60
        $key = $this->option('key') ?? $this->getRandomKey();
61
62
        try {
63
            Storage::disk('ovh')
0 ignored issues
show
Bug introduced by
The method getAdapter() does not seem to exist on object<Illuminate\Contra...\Filesystem\Filesystem>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
            ->getAdapter()
65
            ->getContainer()
66
            ->resetMetadata(['Temp-Url-Key' => $key]);
67
68
            $this->info('Success! The key has been set as: ' . $key);
69
            return 0;
70
        } catch (\Exception $e) {
71
            $this->info($e->getMessage());
72
73
            return 1;
74
        }
75
    }
76
77
    protected function getRandomKey()
78
    {
79
        return hash('sha512', time());
80
    }
81
82
    protected function checkContainerHasKey()
83
    {
84
        $data = Storage::disk('ovh')->getAdapter()->getContainer()->getMetaData();
0 ignored issues
show
Bug introduced by
The method getAdapter() does not seem to exist on object<Illuminate\Contra...\Filesystem\Filesystem>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
86
        return array_key_exists('Temp-Url-Key', $data);
87
    }
88
}
89