| Total Complexity | 55 |
| Total Lines | 206 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Client |
||
| 12 | { |
||
| 13 | use SimpleHttpTrait; |
||
| 14 | |||
| 15 | protected $server; |
||
| 16 | protected $appId; |
||
| 17 | protected $cluster = 'default'; |
||
| 18 | protected $namespaces = ['application']; |
||
| 19 | protected $clientIp; |
||
| 20 | protected $pullTimeout = 5; |
||
| 21 | protected $backupOldEnv = false; |
||
| 22 | |||
| 23 | protected $releaseKeys = []; |
||
| 24 | protected $notifications = []; |
||
| 25 | |||
| 26 | protected $watching = true; |
||
| 27 | |||
| 28 | public function __construct(array $settings) |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | public static function putCommandOptionsToEnv(array $options) |
||
| 52 | { |
||
| 53 | $envs = [ |
||
| 54 | 'ENABLE_APOLLO' => !empty($options['enable-apollo']), |
||
| 55 | 'APOLLO_SERVER' => $options['apollo-server'], |
||
| 56 | 'APOLLO_APP_ID' => $options['apollo-app-id'], |
||
| 57 | 'APOLLO_CLUSTER' => $options['apollo-cluster'], |
||
| 58 | 'APOLLO_NAMESPACES' => implode(',', $options['apollo-namespaces']), |
||
| 59 | 'APOLLO_CLIENT_IP' => $options['apollo-client-ip'], |
||
| 60 | 'APOLLO_PULL_TIMEOUT' => $options['apollo-pull-timeout'], |
||
| 61 | 'APOLLO_BACKUP_OLD_ENV' => $options['apollo-backup-old-env'], |
||
| 62 | ]; |
||
| 63 | foreach ($envs as $key => $value) { |
||
| 64 | putenv("{$key}={$value}"); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | public static function createFromEnv() |
||
| 69 | { |
||
| 70 | if (!getenv('APOLLO_SERVER') || !getenv('APOLLO_APP_ID')) { |
||
| 71 | throw new \InvalidArgumentException('Missing environment variable APOLLO_SERVER or APOLLO_APP_ID'); |
||
| 72 | } |
||
| 73 | $settings = [ |
||
| 74 | 'server' => getenv('APOLLO_SERVER'), |
||
| 75 | 'app_id' => getenv('APOLLO_APP_ID'), |
||
| 76 | 'cluster' => ($cluster = (string)getenv('APOLLO_CLUSTER')) !== '' ? $cluster : null, |
||
| 77 | 'namespaces' => ($namespaces = (string)getenv('APOLLO_NAMESPACES')) !== '' ? explode(',', $namespaces) : null, |
||
| 78 | 'client_ip' => ($clientIp = (string)getenv('APOLLO_CLIENT_IP')) !== '' ? $clientIp : null, |
||
| 79 | 'pull_timeout' => ($pullTimeout = (int)getenv('APOLLO_PULL_TIMEOUT')) > 0 ? $pullTimeout : null, |
||
| 80 | 'backup_old_env' => ($backupOldEnv = (bool)getenv('APOLLO_BACKUP_OLD_ENV')) ? $backupOldEnv : null, |
||
| 81 | ]; |
||
| 82 | return new static($settings); |
||
| 83 | } |
||
| 84 | |||
| 85 | public static function createFromCommandOptions(array $options) |
||
| 86 | { |
||
| 87 | if (!isset($options['apollo-server'], $options['apollo-app-id'])) { |
||
| 88 | throw new \InvalidArgumentException('Missing command option apollo-server or apollo-app-id'); |
||
| 89 | } |
||
| 90 | $settings = [ |
||
| 91 | 'server' => $options['apollo-server'], |
||
| 92 | 'app_id' => $options['apollo-app-id'], |
||
| 93 | 'cluster' => isset($options['apollo-cluster']) && $options['apollo-cluster'] !== '' ? $options['apollo-cluster'] : null, |
||
| 94 | 'namespaces' => !empty($options['apollo-namespaces']) ? $options['apollo-namespaces'] : null, |
||
| 95 | 'client_ip' => isset($options['apollo-client-ip']) && $options['apollo-client-ip'] !== '' ? $options['apollo-client-ip'] : null, |
||
| 96 | 'pull_timeout' => isset($options['apollo-pull-timeout']) ? (int)$options['apollo-pull-timeout'] : null, |
||
| 97 | 'backup_old_env' => isset($options['apollo-backup-old-env']) ? (bool)$options['apollo-backup-old-env'] : null, |
||
| 98 | ]; |
||
| 99 | return new static($settings); |
||
| 100 | } |
||
| 101 | |||
| 102 | public static function attachCommandOptions(Command $command) |
||
| 112 | } |
||
| 113 | |||
| 114 | public function pullBatch(array $namespaces, $withReleaseKey = false, array $options = []) |
||
| 137 | } |
||
| 138 | |||
| 139 | public function pullAll($withReleaseKey = false, array $options = []) |
||
| 142 | } |
||
| 143 | |||
| 144 | public function pullAllAndSave($filepath, array $options = []) |
||
| 145 | { |
||
| 146 | $all = $this->pullAll(false, $options); |
||
| 147 | if (count($all) !== count($this->namespaces)) { |
||
| 148 | $lackNamespaces = array_diff($this->namespaces, array_keys($all)); |
||
| 149 | throw new \RuntimeException('Missing Apollo configurations for namespaces ' . implode(',', $lackNamespaces)); |
||
| 150 | } |
||
| 151 | $configs = []; |
||
| 152 | foreach ($all as $namespace => $config) { |
||
| 153 | $configs[] = '# Namespace: ' . $config['namespaceName']; |
||
| 154 | ksort($config['configurations']); |
||
| 155 | foreach ($config['configurations'] as $key => $value) { |
||
| 156 | $key = preg_replace('/[^a-zA-Z0-9_.]/', '_', $key); |
||
| 157 | $configs[] = sprintf('%s=%s', $key, $value); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | if (empty($configs)) { |
||
| 161 | throw new \RuntimeException('Empty Apollo configuration list'); |
||
| 162 | } |
||
| 163 | if ($this->backupOldEnv && file_exists($filepath)) { |
||
| 164 | rename($filepath, $filepath . '.' . date('YmdHis')); |
||
| 165 | } |
||
| 166 | $fileContent = implode(PHP_EOL, $configs); |
||
| 167 | if (Context::inCoroutine()) { |
||
| 168 | Coroutine::writeFile($filepath, $fileContent); |
||
| 169 | } else { |
||
| 170 | file_put_contents($filepath, $fileContent); |
||
| 171 | } |
||
| 172 | return $configs; |
||
| 173 | } |
||
| 174 | |||
| 175 | public function startWatchNotification(callable $callback, array $options = []) |
||
| 209 | // ignore 304 |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | public function stopWatchNotification() |
||
| 217 | } |
||
| 218 | } |
||
| 219 |