Completed
Pull Request — master (#47)
by
unknown
01:05
created

SetTempUrlKey::handle()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 0
1
<?php
2
3
namespace Sausin\LaravelOvh\Commands;
4
5
use Exception;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Facades\Storage;
8
use OpenStack\ObjectStore\v1\Models\Container;
9
10
class SetTempUrlKey extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'ovh:set-temp-url-key
18
                            {--disk=ovh : The disk using your OVH container}
19
                            {--key= : The key you want to set up on your container}
20
                            {--force : Forcibly set a new key on the container}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Set temp url key on the private container, making the use of Storage::temporaryUrl() possible';
28
29
    /**
30
     * The Object Storage Container.
31
     *
32
     * @var Container
33
     */
34
    protected $container;
35
36
    /**
37
     * Execute the console command.
38
     *
39
     * If the '--force' flag is provided, a new Temp URL Key will be generated and
40
     * forcefully set in the Container's metadata, overriding any existing keys.
41
     *
42
     * If the command is not forced and there's an existing key, the User will be
43
     * prompted to override the existing keys.
44
     *
45
     * @return void
46
     */
47
    public function handle(): void
48
    {
49
        $this->container = Storage::disk($this->option('disk'))->getAdapter()->getContainer();
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...
50
51
        if ($this->hasOption('force') || $this->askIfShouldOverrideExistingKey()) {
52
            $this->setContainerKey();
53
        }
54
    }
55
56
    /**
57
     * If there's no existing Temp URL Key present in the Container, continue.
58
     *
59
     * Otherwise, if there's already an existing Temp URL Key present in the
60
     * Container, the User will be prompted to choose if we should override it
61
     * or not.
62
     *
63
     * @return bool
64
     */
65
    protected function askIfShouldOverrideExistingKey(): bool
66
    {
67
        if (!array_key_exists('Temp-Url-Key', $this->container->getMetadata())) {
68
            return true; // Yeah, override the non-existing key.
69
        }
70
71
        return $this->confirm(
72
            'A Temp URL Key already exists in your container, would you like to override it?',
73
            false
74
        );
75
    }
76
77
    /**
78
     * Generates a random Temp URL Key.
79
     *
80
     * For more details, please refer to:
81
     *  - https://docs.ovh.com/gb/en/public-cloud/share_an_object_via_a_temporary_url/#generate-the-temporary-address-tempurl
82
     *
83
     * @return string
84
     */
85
    protected function getRandomKey(): string
86
    {
87
        return hash('sha512', time());
88
    }
89
90
    /**
91
     * Updates the Temp URL Key for the Container.
92
     *
93
     * @return void
94
     */
95
    protected function setContainerKey(): void
96
    {
97
        $key = $this->option('key') ?? $this->getRandomKey();
98
99
        try {
100
            $this->container->resetMetadata(['Temp-Url-Key' => $key]);
101
        } catch (Exception $e) {
102
            $this->error($e->getMessage());
103
        }
104
105
        $this->info('Successfully set Temp URL Key to: '.$key);
106
    }
107
}
108