| Conditions | 14 |
| Paths | 1153 |
| Total Lines | 66 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 91 | public function getConnection() { |
||
| 92 | if (!is_null($this->connection)) { |
||
| 93 | return $this->connection; |
||
| 94 | } |
||
| 95 | |||
| 96 | $scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https'; |
||
| 97 | $base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/'; |
||
| 98 | |||
| 99 | // Adding explicit credential provider to the beginning chain. |
||
| 100 | // Including environment variables and IAM instance profiles. |
||
| 101 | $provider = CredentialProvider::memoize( |
||
| 102 | CredentialProvider::chain( |
||
| 103 | $this->paramCredentialProvider(), |
||
| 104 | CredentialProvider::env(), |
||
| 105 | CredentialProvider::instanceProfile() |
||
| 106 | ) |
||
| 107 | ); |
||
| 108 | |||
| 109 | $options = [ |
||
| 110 | 'version' => isset($this->params['version']) ? $this->params['version'] : 'latest', |
||
| 111 | 'credentials' => $provider, |
||
| 112 | 'endpoint' => $base_url, |
||
| 113 | 'region' => $this->params['region'], |
||
| 114 | 'use_path_style_endpoint' => isset($this->params['use_path_style']) ? $this->params['use_path_style'] : false, |
||
| 115 | 'signature_provider' => \Aws\or_chain([self::class, 'legacySignatureProvider'], ClientResolver::_default_signature_provider()), |
||
| 116 | 'csm' => false, |
||
| 117 | ]; |
||
| 118 | if (isset($this->params['proxy'])) { |
||
| 119 | $options['request.options'] = ['proxy' => $this->params['proxy']]; |
||
| 120 | } |
||
| 121 | if (isset($this->params['legacy_auth']) && $this->params['legacy_auth']) { |
||
| 122 | $options['signature_version'] = 'v2'; |
||
| 123 | } |
||
| 124 | $this->connection = new S3Client($options); |
||
| 125 | |||
| 126 | if (!$this->connection::isBucketDnsCompatible($this->bucket)) { |
||
| 127 | $logger = \OC::$server->getLogger(); |
||
|
|
|||
| 128 | $logger->debug('Bucket "' . $this->bucket . '" This bucket name is not dns compatible, it may contain invalid characters.', |
||
| 129 | ['app' => 'objectstore']); |
||
| 130 | } |
||
| 131 | |||
| 132 | if (!$this->connection->doesBucketExist($this->bucket)) { |
||
| 133 | $logger = \OC::$server->getLogger(); |
||
| 134 | try { |
||
| 135 | $logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']); |
||
| 136 | if (!$this->connection::isBucketDnsCompatible($this->bucket)) { |
||
| 137 | throw new \Exception("The bucket will not be created because the name is not dns compatible, please correct it: " . $this->bucket); |
||
| 138 | } |
||
| 139 | $this->connection->createBucket(['Bucket' => $this->bucket]); |
||
| 140 | $this->testTimeout(); |
||
| 141 | } catch (S3Exception $e) { |
||
| 142 | $logger->logException($e, [ |
||
| 143 | 'message' => 'Invalid remote storage.', |
||
| 144 | 'level' => ILogger::DEBUG, |
||
| 145 | 'app' => 'objectstore', |
||
| 146 | ]); |
||
| 147 | throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage()); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | // google cloud's s3 compatibility doesn't like the EncodingType parameter |
||
| 152 | if (strpos($base_url, 'storage.googleapis.com')) { |
||
| 153 | $this->connection->getHandlerList()->remove('s3.auto_encode'); |
||
| 154 | } |
||
| 155 | |||
| 156 | return $this->connection; |
||
| 157 | } |
||
| 197 |