| Total Complexity | 45 |
| Total Lines | 216 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like S3ConnectionTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use S3ConnectionTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | trait S3ConnectionTrait { |
||
| 47 | /** @var array */ |
||
| 48 | protected $params; |
||
| 49 | |||
| 50 | /** @var S3Client */ |
||
| 51 | protected $connection; |
||
| 52 | |||
| 53 | /** @var string */ |
||
| 54 | protected $id; |
||
| 55 | |||
| 56 | /** @var string */ |
||
| 57 | protected $bucket; |
||
| 58 | |||
| 59 | /** @var int */ |
||
| 60 | protected $timeout; |
||
| 61 | |||
| 62 | /** @var string */ |
||
| 63 | protected $proxy; |
||
| 64 | |||
| 65 | /** @var string */ |
||
| 66 | protected $storageClass; |
||
| 67 | |||
| 68 | /** @var int */ |
||
| 69 | protected $uploadPartSize; |
||
| 70 | |||
| 71 | /** @var int */ |
||
| 72 | private $putSizeLimit; |
||
| 73 | |||
| 74 | protected $test; |
||
| 75 | |||
| 76 | protected function parseParams($params) { |
||
| 77 | if (empty($params['bucket'])) { |
||
| 78 | throw new \Exception("Bucket has to be configured."); |
||
| 79 | } |
||
| 80 | |||
| 81 | $this->id = 'amazon::' . $params['bucket']; |
||
| 82 | |||
| 83 | $this->test = isset($params['test']); |
||
| 84 | $this->bucket = $params['bucket']; |
||
| 85 | $this->proxy = $params['proxy'] ?? false; |
||
|
|
|||
| 86 | $this->timeout = $params['timeout'] ?? 15; |
||
| 87 | $this->storageClass = !empty($params['storageClass']) ? $params['storageClass'] : 'STANDARD'; |
||
| 88 | $this->uploadPartSize = $params['uploadPartSize'] ?? 524288000; |
||
| 89 | $this->putSizeLimit = $params['putSizeLimit'] ?? 104857600; |
||
| 90 | $params['region'] = empty($params['region']) ? 'eu-west-1' : $params['region']; |
||
| 91 | $params['hostname'] = empty($params['hostname']) ? 's3.' . $params['region'] . '.amazonaws.com' : $params['hostname']; |
||
| 92 | if (!isset($params['port']) || $params['port'] === '') { |
||
| 93 | $params['port'] = (isset($params['use_ssl']) && $params['use_ssl'] === false) ? 80 : 443; |
||
| 94 | } |
||
| 95 | $params['verify_bucket_exists'] = empty($params['verify_bucket_exists']) ? true : $params['verify_bucket_exists']; |
||
| 96 | $this->params = $params; |
||
| 97 | } |
||
| 98 | |||
| 99 | public function getBucket() { |
||
| 100 | return $this->bucket; |
||
| 101 | } |
||
| 102 | |||
| 103 | public function getProxy() { |
||
| 104 | return $this->proxy; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Returns the connection |
||
| 109 | * |
||
| 110 | * @return S3Client connected client |
||
| 111 | * @throws \Exception if connection could not be made |
||
| 112 | */ |
||
| 113 | public function getConnection() { |
||
| 114 | if (!is_null($this->connection)) { |
||
| 115 | return $this->connection; |
||
| 116 | } |
||
| 117 | |||
| 118 | $scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https'; |
||
| 119 | $base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/'; |
||
| 120 | |||
| 121 | // Adding explicit credential provider to the beginning chain. |
||
| 122 | // Including default credential provider (skipping AWS shared config files). |
||
| 123 | $provider = CredentialProvider::memoize( |
||
| 124 | CredentialProvider::chain( |
||
| 125 | $this->paramCredentialProvider(), |
||
| 126 | CredentialProvider::defaultProvider(['use_aws_shared_config_files' => false]) |
||
| 127 | ) |
||
| 128 | ); |
||
| 129 | |||
| 130 | $options = [ |
||
| 131 | 'version' => isset($this->params['version']) ? $this->params['version'] : 'latest', |
||
| 132 | 'credentials' => $provider, |
||
| 133 | 'endpoint' => $base_url, |
||
| 134 | 'region' => $this->params['region'], |
||
| 135 | 'use_path_style_endpoint' => isset($this->params['use_path_style']) ? $this->params['use_path_style'] : false, |
||
| 136 | 'signature_provider' => \Aws\or_chain([self::class, 'legacySignatureProvider'], ClientResolver::_default_signature_provider()), |
||
| 137 | 'csm' => false, |
||
| 138 | 'use_arn_region' => false, |
||
| 139 | 'http' => ['verify' => $this->getCertificateBundlePath()], |
||
| 140 | 'use_aws_shared_config_files' => false, |
||
| 141 | ]; |
||
| 142 | if ($this->getProxy()) { |
||
| 143 | $options['http']['proxy'] = $this->getProxy(); |
||
| 144 | } |
||
| 145 | if (isset($this->params['legacy_auth']) && $this->params['legacy_auth']) { |
||
| 146 | $options['signature_version'] = 'v2'; |
||
| 147 | } |
||
| 148 | $this->connection = new S3Client($options); |
||
| 149 | |||
| 150 | if (!$this->connection::isBucketDnsCompatible($this->bucket)) { |
||
| 151 | $logger = \OC::$server->get(LoggerInterface::class); |
||
| 152 | $logger->debug('Bucket "' . $this->bucket . '" This bucket name is not dns compatible, it may contain invalid characters.', |
||
| 153 | ['app' => 'objectstore']); |
||
| 154 | } |
||
| 155 | |||
| 156 | if ($this->params['verify_bucket_exists'] && !$this->connection->doesBucketExist($this->bucket)) { |
||
| 157 | $logger = \OC::$server->get(LoggerInterface::class); |
||
| 158 | try { |
||
| 159 | $logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']); |
||
| 160 | if (!$this->connection::isBucketDnsCompatible($this->bucket)) { |
||
| 161 | throw new \Exception("The bucket will not be created because the name is not dns compatible, please correct it: " . $this->bucket); |
||
| 162 | } |
||
| 163 | $this->connection->createBucket(['Bucket' => $this->bucket]); |
||
| 164 | $this->testTimeout(); |
||
| 165 | } catch (S3Exception $e) { |
||
| 166 | $logger->debug('Invalid remote storage.', [ |
||
| 167 | 'exception' => $e, |
||
| 168 | 'app' => 'objectstore', |
||
| 169 | ]); |
||
| 170 | throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage()); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | // google cloud's s3 compatibility doesn't like the EncodingType parameter |
||
| 175 | if (strpos($base_url, 'storage.googleapis.com')) { |
||
| 176 | $this->connection->getHandlerList()->remove('s3.auto_encode'); |
||
| 177 | } |
||
| 178 | |||
| 179 | return $this->connection; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * when running the tests wait to let the buckets catch up |
||
| 184 | */ |
||
| 185 | private function testTimeout() { |
||
| 186 | if ($this->test) { |
||
| 187 | sleep($this->timeout); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | public static function legacySignatureProvider($version, $service, $region) { |
||
| 192 | switch ($version) { |
||
| 193 | case 'v2': |
||
| 194 | case 's3': |
||
| 195 | return new S3Signature(); |
||
| 196 | default: |
||
| 197 | return null; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * This function creates a credential provider based on user parameter file |
||
| 203 | */ |
||
| 204 | protected function paramCredentialProvider(): callable { |
||
| 205 | return function () { |
||
| 206 | $key = empty($this->params['key']) ? null : $this->params['key']; |
||
| 207 | $secret = empty($this->params['secret']) ? null : $this->params['secret']; |
||
| 208 | |||
| 209 | if ($key && $secret) { |
||
| 210 | return Promise\promise_for( |
||
| 211 | new Credentials($key, $secret) |
||
| 212 | ); |
||
| 213 | } |
||
| 214 | |||
| 215 | $msg = 'Could not find parameters set for credentials in config file.'; |
||
| 216 | return new RejectedPromise(new CredentialsException($msg)); |
||
| 217 | }; |
||
| 218 | } |
||
| 219 | |||
| 220 | protected function getCertificateBundlePath(): ?string { |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | protected function getSSECKey(): ?string { |
||
| 236 | if (isset($this->params['sse_c_key'])) { |
||
| 237 | return $this->params['sse_c_key']; |
||
| 238 | } |
||
| 239 | |||
| 241 | } |
||
| 242 | |||
| 243 | protected function getSSECParameters(bool $copy = false): array { |
||
| 265 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.