Completed
Push — master ( 59e915...d83aef )
by Saurabh
05:04 queued 02:02
created

SetCORSHeaders::handle()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 22

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 9.2568
c 0
b 0
f 0
cc 5
nc 8
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 InvalidArgumentException;
9
use League\Flysystem\Cached\CachedAdapter;
10
use OpenStack\ObjectStore\v1\Models\Container;
11
12
class SetCORSHeaders extends Command
13
{
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'ovh:set-cors-headers
20
                            {--disk=ovh : The disk using your OVH container}
21
                            {--origins=* : The origins to be allowed on the containers (multiple allowed)}
22
                            {--max-age=3600 : The maximum cache validity of pre-flight requests}
23
                            {--force : Forcibly set the new headers}';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Set CORS headers on the container to make Form POST signaure work flawlessly';
31
32
    /**
33
     * The Object Storage Container.
34
     *
35
     * @var Container
36
     */
37
    protected $container;
38
39
    /** array */
40
    protected $containerMeta = [];
41
42
    /**
43
     * Execute the console command.
44
     *
45
     * If the '--force' flag is provided, the specified keys will be set on the container.
46
     * This excludes any 'Temp-Url-Key' already present on the container.
47
     *
48
     * @return void
49
     */
50 View Code Duplication
    public function handle(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52
        try {
53
            $adapter = Storage::disk($this->option('disk'))->getAdapter();
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...
54
55
            if ($adapter instanceof CachedAdapter) {
56
                $adapter = $adapter->getAdapter();
57
            }
58
59
            $this->container = $adapter->getContainer();
60
        } catch (InvalidArgumentException $e) {
61
            $this->error($e->getMessage());
62
63
            return;
64
        }
65
66
        $this->containerMeta = $this->container->getMetadata();
67
68
        if ($this->option('force') || $this->askIfShouldOverrideExistingParams()) {
69
            $this->setHeaders();
70
        }
71
    }
72
73
    /**
74
     * If there's no existing Temp URL Key present in the Container, continue.
75
     *
76
     * Otherwise, if there's already an existing Temp URL Key present in the
77
     * Container, the User will be prompted to choose if we should override it
78
     * or not.
79
     *
80
     * @return bool
81
     */
82
    protected function askIfShouldOverrideExistingParams(): bool
83
    {
84
        $metaKeys = ['Access-Control-Allow-Origin', 'Access-Control-Max-Age'];
85
86
        if (count(array_intersect($metaKeys, array_keys($this->containerMeta))) === 0) {
87
            return true;
88
        }
89
90
        return $this->confirm(
91
            'Some CORS Meta keys are already set on the container. Do you want to override them?',
92
            false
93
        );
94
    }
95
96
    /**
97
     * Updates the Temp URL Key for the Container.
98
     *
99
     * @return void
100
     */
101
    protected function setHeaders(): void
102
    {
103
        $origins = '*';
104
105
        if (count($this->option('origins')) !== 0) {
106
            $origins = implode(' ', $this->option('origins'));
107
        }
108
109
        $maxAge = $this->option('max-age');
110
        $meta = ['Access-Control-Allow-Origin' => $origins, 'Access-Control-Max-Age' => $maxAge];
111
112
        if (array_key_exists('Temp-Url-Key', $this->containerMeta)) {
113
            $meta += ['Temp-Url-Key' => $this->containerMeta['Temp-Url-Key']];
114
        }
115
116
        try {
117
            $this->container->resetMetadata($meta);
118
119
            $this->info('CORS meta keys successfully set on the container');
120
        } catch (Exception $e) {
121
            $this->error($e->getMessage());
122
        }
123
    }
124
}
125