| Total Complexity | 57 |
| Total Lines | 196 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ClusterTopClient 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 ClusterTopClient, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 2 | class ClusterTopClient extends TopClient { |
||
| 3 | |||
| 4 | private static $dnsconfig; |
||
| 5 | private static $syncDate = 0; |
||
| 6 | private static $applicationVar ; |
||
| 7 | private static $cfgDuration = 10; |
||
| 8 | |||
| 9 | public function __construct($appkey = "",$secretKey = ""){ |
||
| 10 | ClusterTopClient::$applicationVar = new ApplicationVar; |
||
| 11 | $this->appkey = $appkey; |
||
| 12 | $this->secretKey = $secretKey ; |
||
| 13 | $saveConfig = ClusterTopClient::$applicationVar->getValue(); |
||
| 14 | |||
| 15 | if($saveConfig){ |
||
| 16 | $tmpConfig = $saveConfig['dnsconfig']; |
||
| 17 | ClusterTopClient::$dnsconfig = $this->object_to_array($tmpConfig); |
||
| 18 | unset($tmpConfig); |
||
| 19 | |||
| 20 | ClusterTopClient::$syncDate = $saveConfig['syncDate']; |
||
| 21 | if(!ClusterTopClient::$syncDate) |
||
| 22 | ClusterTopClient::$syncDate = 0; |
||
| 23 | } |
||
| 24 | } |
||
| 25 | |||
| 26 | public function __destruct(){ |
||
| 27 | if(ClusterTopClient::$dnsconfig && ClusterTopClient::$syncDate){ |
||
| 28 | ClusterTopClient::$applicationVar->setValue("dnsconfig",ClusterTopClient::$dnsconfig); |
||
| 29 | ClusterTopClient::$applicationVar->setValue("syncDate",ClusterTopClient::$syncDate); |
||
| 30 | ClusterTopClient::$applicationVar->write(); |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | public function execute($request = null, $session = null,$bestUrl = null){ |
||
| 35 | $currentDate = date('U'); |
||
| 36 | $syncDuration = $this->getDnsConfigSyncDuration(); |
||
| 37 | $bestUrl = $this->getBestVipUrl($this->gatewayUrl,$request->getApiMethodName(),$session); |
||
| 38 | if($currentDate - ClusterTopClient::$syncDate > $syncDuration * 60){ |
||
| 39 | $httpdns = new HttpdnsGetRequest; |
||
| 40 | ClusterTopClient::$dnsconfig = json_decode(parent::execute($httpdns,null,$bestUrl)->result,true); |
||
|
|
|||
| 41 | $syncDate = date('U'); |
||
| 42 | ClusterTopClient::$syncDate = $syncDate ; |
||
| 43 | } |
||
| 44 | return parent::execute($request,$session,$bestUrl); |
||
| 45 | } |
||
| 46 | |||
| 47 | private function getDnsConfigSyncDuration(){ |
||
| 48 | if(ClusterTopClient::$cfgDuration){ |
||
| 49 | return ClusterTopClient::$cfgDuration; |
||
| 50 | } |
||
| 51 | if(!ClusterTopClient::$dnsconfig){ |
||
| 52 | return ClusterTopClient::$cfgDuration; |
||
| 53 | } |
||
| 54 | $config = json_encode(ClusterTopClient::$dnsconfig); |
||
| 55 | if(!$config){ |
||
| 56 | return ClusterTopClient::$cfgDuration; |
||
| 57 | } |
||
| 58 | $config = ClusterTopClient::$dnsconfig['config']; |
||
| 59 | $duration = $config['interval']; |
||
| 60 | ClusterTopClient::$cfgDuration = $duration; |
||
| 61 | |||
| 62 | return ClusterTopClient::$cfgDuration; |
||
| 63 | } |
||
| 64 | |||
| 65 | private function getBestVipUrl($url,$apiname = null,$session = null){ |
||
| 66 | $config = ClusterTopClient::$dnsconfig['config']; |
||
| 67 | $degrade = $config['degrade']; |
||
| 68 | if(strcmp($degrade,'true') == 0){ |
||
| 69 | return $url; |
||
| 70 | } |
||
| 71 | $currentEnv = $this->getEnvByApiName($apiname,$session); |
||
| 72 | $vip = $this->getVipByEnv($url,$currentEnv); |
||
| 73 | if($vip) |
||
| 74 | return $vip; |
||
| 75 | return $url; |
||
| 76 | } |
||
| 77 | |||
| 78 | private function getVipByEnv($comUrl,$currentEnv){ |
||
| 79 | $urlSchema = parse_url($comUrl); |
||
| 80 | if(!$urlSchema) |
||
| 81 | return null; |
||
| 82 | if(!ClusterTopClient::$dnsconfig['env']) |
||
| 83 | return null; |
||
| 84 | |||
| 85 | if(!array_key_exists($currentEnv,ClusterTopClient::$dnsconfig['env'])) |
||
| 86 | return null; |
||
| 87 | |||
| 88 | $hostList = ClusterTopClient::$dnsconfig['env'][$currentEnv]; |
||
| 89 | if(!$hostList) |
||
| 90 | return null ; |
||
| 91 | |||
| 92 | $vipList = null; |
||
| 93 | foreach ($hostList as $key => $value) { |
||
| 94 | if(strcmp($key,$urlSchema['host']) == 0 && strcmp($value['proto'],$urlSchema['scheme']) == 0){ |
||
| 95 | $vipList = $value; |
||
| 96 | break; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | $vip = $this->getRandomWeightElement($vipList['vip']); |
||
| 100 | |||
| 101 | if($vip){ |
||
| 102 | return $urlSchema['scheme']."://".$vip.$urlSchema['path']; |
||
| 103 | } |
||
| 104 | return null; |
||
| 105 | } |
||
| 106 | |||
| 107 | private function getEnvByApiName($apiName,$session=""){ |
||
| 108 | $apiCfgArray = ClusterTopClient::$dnsconfig['api']; |
||
| 109 | if($apiCfgArray){ |
||
| 110 | if(array_key_exists($apiName,$apiCfgArray)){ |
||
| 111 | $apiCfg = $apiCfgArray[$apiName]; |
||
| 112 | if(array_key_exists('user',$apiCfg)){ |
||
| 113 | $userFlag = $apiCfg['user']; |
||
| 114 | $flag = $this->getUserFlag($session); |
||
| 115 | if($userFlag && $flag ){ |
||
| 116 | return $this->getEnvBySessionFlag($userFlag,$flag); |
||
| 117 | }else{ |
||
| 118 | return $this->getRandomWeightElement($apiCfg['rule']); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | return $this->getDeafultEnv(); |
||
| 124 | } |
||
| 125 | |||
| 126 | private function getUserFlag($session){ |
||
| 127 | if($session && strlen($session) > 5){ |
||
| 128 | if($session[0] == '6' || $session[0] == '7'){ |
||
| 129 | return $session[strlen($session) -1]; |
||
| 130 | }else if($session[0] == '5' || $session[0] == '8'){ |
||
| 131 | return $session[5]; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | return null; |
||
| 135 | } |
||
| 136 | |||
| 137 | private function getEnvBySessionFlag($targetConfig,$flag){ |
||
| 138 | if($flag){ |
||
| 139 | $userConf = ClusterTopClient::$dnsconfig['user']; |
||
| 140 | $cfgArry = $userConf[$targetConfig]; |
||
| 141 | foreach ($cfgArry as $key => $value) { |
||
| 142 | if(in_array($flag,$value)) |
||
| 143 | return $key; |
||
| 144 | } |
||
| 145 | }else{ |
||
| 146 | return null; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | private function getRandomWeightElement($elements){ |
||
| 166 | |||
| 167 | } |
||
| 168 | |||
| 169 | private function getElementWeight($ele){ |
||
| 170 | $params = explode('|', $ele); |
||
| 171 | return floatval($params[1]); |
||
| 172 | } |
||
| 173 | private function getElementValue($ele){ |
||
| 174 | $params = explode('|', $ele); |
||
| 175 | return $params[0]; |
||
| 176 | } |
||
| 177 | |||
| 178 | private function getDeafultEnv(){ |
||
| 179 | return ClusterTopClient::$dnsconfig['config']['def_env']; |
||
| 180 | } |
||
| 181 | |||
| 182 | private static function startsWith($haystack, $needle) { |
||
| 184 | } |
||
| 185 | |||
| 186 | private function object_to_array($obj) |
||
| 187 | { |
||
| 188 | $_arr= is_object($obj) ? get_object_vars($obj) : $obj; |
||
| 189 | foreach($_arr as $key=> $val) |
||
| 190 | { |
||
| 191 | $val= (is_array($val) || is_object($val))? $this->object_to_array($val) : $val; |
||
| 192 | $arr[$key] = $val; |
||
| 193 | } |
||
| 194 | return$arr; |
||
| 195 | } |
||
| 196 | |||
| 197 | private function randomFloat($min = 0, $max = 1) { return $min + mt_rand() / mt_getrandmax() * ($max - $min); } |
||
| 198 | } |
||
| 199 | ?> |
||