Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 12 | |||
| 13 | class SetCORSHeaders extends Command |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * The name and signature of the console command. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $signature = 'ovh:set-cors-headers |
||
| 21 | {--disk=ovh : The disk using your OVH container} |
||
| 22 | {--origins=* : The origins to be allowed on the containers (multiple allowed)} |
||
| 23 | {--max-age=3600 : The maximum cache validity of pre-flight requests} |
||
| 24 | {--force : Forcibly set the new headers}'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The console command description. |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $description = 'Set CORS headers on the container to make Form POST signature work flawlessly'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The Object Storage Container. |
||
| 35 | * |
||
| 36 | * @var Container |
||
| 37 | */ |
||
| 38 | protected $container; |
||
| 39 | |||
| 40 | /** array */ |
||
| 41 | protected $containerMeta = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Execute the console command. |
||
| 45 | * |
||
| 46 | * If the '--force' flag is provided, the specified keys will be set on the container. |
||
| 47 | * This excludes any 'Temp-Url-Key' already present on the container. |
||
| 48 | * |
||
| 49 | * @return void |
||
| 50 | */ |
||
| 51 | public function handle(): void |
||
| 52 | { |
||
| 53 | try { |
||
| 54 | $disk = $this->getDisk(); |
||
| 55 | |||
| 56 | $adapter = Storage::disk($disk)->getAdapter(); |
||
| 57 | |||
| 58 | if ($adapter instanceof CachedAdapter) { |
||
| 59 | $adapter = $adapter->getAdapter(); |
||
| 60 | } |
||
| 61 | |||
| 62 | $this->container = $adapter->getContainer(); |
||
| 63 | } catch (InvalidArgumentException $e) { |
||
| 64 | $this->error($e->getMessage()); |
||
| 65 | |||
| 66 | return; |
||
| 67 | } |
||
| 68 | |||
| 69 | $this->containerMeta = $this->container->getMetadata(); |
||
| 70 | |||
| 71 | if ($this->option('force') || $this->askIfShouldOverrideExistingParams()) { |
||
| 72 | $this->setHeaders(); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * If there's no existing Temp URL Key present in the Container, continue. |
||
| 78 | * |
||
| 79 | * Otherwise, if there's already an existing Temp URL Key present in the |
||
| 80 | * Container, the User will be prompted to choose if we should override it |
||
| 81 | * or not. |
||
| 82 | * |
||
| 83 | * @return bool |
||
| 84 | */ |
||
| 85 | protected function askIfShouldOverrideExistingParams(): bool |
||
| 86 | { |
||
| 87 | $metaKeys = ['Access-Control-Allow-Origin', 'Access-Control-Max-Age']; |
||
| 88 | |||
| 89 | if (count(array_intersect($metaKeys, array_keys($this->containerMeta))) === 0) { |
||
| 90 | return true; |
||
| 91 | } |
||
| 92 | |||
| 93 | return $this->confirm( |
||
| 94 | 'Some CORS Meta keys are already set on the container. Do you want to override them?', |
||
| 95 | false |
||
| 96 | ); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Updates the Temp URL Key for the Container. |
||
| 101 | * |
||
| 102 | * @return void |
||
| 103 | */ |
||
| 104 | protected function setHeaders(): void |
||
| 105 | { |
||
| 106 | $origins = '*'; |
||
| 107 | |||
| 108 | if (count($this->option('origins')) !== 0) { |
||
| 109 | $origins = implode(' ', $this->option('origins')); |
||
| 110 | } |
||
| 111 | |||
| 112 | $maxAge = $this->option('max-age'); |
||
| 113 | $meta = ['Access-Control-Allow-Origin' => $origins, 'Access-Control-Max-Age' => $maxAge]; |
||
| 114 | |||
| 115 | if (array_key_exists('Temp-Url-Key', $this->containerMeta)) { |
||
| 116 | $meta += ['Temp-Url-Key' => $this->containerMeta['Temp-Url-Key']]; |
||
| 117 | } |
||
| 118 | |||
| 119 | try { |
||
| 120 | $this->container->resetMetadata($meta); |
||
| 121 | |||
| 122 | $this->info('CORS meta keys successfully set on the container'); |
||
| 123 | } catch (Exception $e) { |
||
| 124 | $this->error($e->getMessage()); |
||
| 125 | } |
||
| 151 |