Total Complexity | 56 |
Total Lines | 205 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | 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' => isset($options['apollo-namespaces']) && $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 = []) |
||
115 | { |
||
116 | $configs = []; |
||
117 | $uri = sprintf('%s/configs/%s/%s/', $this->server, $this->appId, $this->cluster); |
||
118 | foreach ($namespaces as $namespace) { |
||
119 | $url = $uri . $namespace . '?' . http_build_query([ |
||
120 | 'releaseKey' => $withReleaseKey && isset($this->releaseKeys[$namespace]) ? $this->releaseKeys[$namespace] : null, |
||
121 | 'ip' => $this->clientIp, |
||
122 | ]); |
||
123 | $timeout = isset($options['timeout']) ? $options['timeout'] : $this->pullTimeout; |
||
124 | $response = $this->httpGet($url, compact('timeout')); |
||
125 | if ($response['statusCode'] === 200) { |
||
126 | $json = json_decode($response['body'], true); |
||
127 | if (is_array($json)) { |
||
128 | $configs[$namespace] = $json; |
||
129 | $this->releaseKeys[$namespace] = $configs[$namespace]['releaseKey']; |
||
130 | } |
||
131 | } elseif ($response['statusCode'] === 304) { |
||
132 | // ignore 304 |
||
133 | } |
||
134 | |||
135 | } |
||
136 | return $configs; |
||
137 | } |
||
138 | |||
139 | public function pullAll($withReleaseKey = false, array $options = []) |
||
140 | { |
||
141 | return $this->pullBatch($this->namespaces, $withReleaseKey, $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 | $configs[] = sprintf('%s=%s', $key, $value); |
||
157 | } |
||
158 | } |
||
159 | if (empty($configs)) { |
||
160 | throw new \RuntimeException('Empty Apollo configuration list'); |
||
161 | } |
||
162 | if ($this->backupOldEnv && file_exists($filepath)) { |
||
163 | rename($filepath, $filepath . '.' . date('YmdHis')); |
||
164 | } |
||
165 | $fileContent = implode(PHP_EOL, $configs); |
||
166 | if (Context::inCoroutine()) { |
||
167 | Coroutine::writeFile($filepath, $fileContent); |
||
168 | } else { |
||
169 | file_put_contents($filepath, $fileContent); |
||
170 | } |
||
171 | return $configs; |
||
172 | } |
||
173 | |||
174 | public function startWatchNotification(callable $callback, array $options = []) |
||
208 | // ignore 304 |
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | |||
213 | public function stopWatchNotification() |
||
216 | } |
||
217 | } |
||
218 |