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 SetTempUrlKey extends Command |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * The name and signature of the console command. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $signature = 'ovh:set-temp-url-key |
||
| 21 | {--disk=ovh : The disk using your OVH container} |
||
| 22 | {--key= : The key you want to set up on your container} |
||
| 23 | {--force : Forcibly set a new key on the container}'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The console command description. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $description = 'Set temp url key on the private container, making the use of Storage::temporaryUrl() possible'; |
||
| 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, a new Temp URL Key will be generated and |
||
| 46 | * forcefully set in the Container's metadata, overriding any existing keys. |
||
| 47 | * |
||
| 48 | * If the command is not forced and there's an existing key, the User will be |
||
| 49 | * prompted to override the existing keys. |
||
| 50 | * |
||
| 51 | * @return void |
||
| 52 | */ |
||
| 53 | public function handle(): void |
||
| 54 | { |
||
| 55 | try { |
||
| 56 | $disk = $this->getDisk(); |
||
| 57 | |||
| 58 | $adapter = Storage::disk($disk)->getAdapter(); |
||
| 59 | |||
| 60 | if ($adapter instanceof CachedAdapter) { |
||
| 61 | $adapter = $adapter->getAdapter(); |
||
| 62 | } |
||
| 63 | |||
| 64 | $this->container = $adapter->getContainer(); |
||
| 65 | } catch (InvalidArgumentException $e) { |
||
| 66 | $this->error($e->getMessage()); |
||
| 67 | |||
| 68 | return; |
||
| 69 | } |
||
| 70 | |||
| 71 | $this->containerMeta = $this->container->getMetadata(); |
||
| 72 | |||
| 73 | if ($this->option('force') || $this->askIfShouldOverrideExistingKey()) { |
||
| 74 | $this->setContainerKey(); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * If there's no existing Temp URL Key present in the Container, continue. |
||
| 80 | * |
||
| 81 | * Otherwise, if there's already an existing Temp URL Key present in the |
||
| 82 | * Container, the User will be prompted to choose if we should override it |
||
| 83 | * or not. |
||
| 84 | * |
||
| 85 | * @return bool |
||
| 86 | */ |
||
| 87 | protected function askIfShouldOverrideExistingKey(): bool |
||
| 88 | { |
||
| 89 | if (!array_key_exists('Temp-Url-Key', $this->containerMeta)) { |
||
| 90 | return true; // Yeah, override the non-existing key. |
||
| 91 | } |
||
| 92 | |||
| 93 | return $this->confirm( |
||
| 94 | 'A Temp URL Key already exists in your container, would you like to override it?', |
||
| 95 | false |
||
| 96 | ); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Generates a random Temp URL Key. |
||
| 101 | * |
||
| 102 | * For more details, please refer to: |
||
| 103 | * - https://docs.ovh.com/gb/en/public-cloud/share_an_object_via_a_temporary_url/#generate-the-temporary-address-tempurl |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | protected function getRandomKey(): string |
||
| 108 | { |
||
| 109 | return hash('sha512', time()); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Updates the Temp URL Key for the Container. |
||
| 114 | * |
||
| 115 | * @return void |
||
| 116 | */ |
||
| 117 | protected function setContainerKey(): void |
||
| 118 | { |
||
| 119 | $key = $this->option('key') ?? $this->getRandomKey(); |
||
| 120 | $meta = $this->getMeta(); |
||
| 121 | |||
| 122 | try { |
||
| 123 | $this->container->resetMetadata($meta + ['Temp-Url-Key' => $key]); |
||
| 124 | |||
| 125 | $this->info('Successfully set Temp URL Key to: '.$key); |
||
| 126 | } catch (Exception $e) { |
||
| 127 | $this->error($e->getMessage()); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * If other meta keys exist, get them. |
||
| 133 | * |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | protected function getMeta(): array |
||
| 137 | { |
||
| 138 | $meta = []; |
||
| 139 | $metaKeys = ['Access-Control-Allow-Origin', 'Access-Control-Max-Age']; |
||
| 140 | |||
| 141 | foreach ($metaKeys as $key) { |
||
| 142 | if (array_key_exists($key, $this->containerMeta)) { |
||
| 143 | $meta += [$key => $this->containerMeta[$key]]; |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | return $meta; |
||
| 173 |