| Total Complexity | 54 |
| Total Lines | 362 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| Bugs | 0 | Features | 0 |
Complex classes like IdPlist 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 IdPlist, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class IdPlist extends common\Entity |
||
| 26 | { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Order active identity providers according to their distance and name |
||
| 30 | * @param string $country the country from which to list IdPs |
||
| 31 | * @param array $currentLocation current location |
||
| 32 | * @return array $IdPs - list of arrays ('id', 'name'); |
||
| 33 | */ |
||
| 34 | public static function orderIdentityProviders($country, $currentLocation) |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Lists all identity providers in the database |
||
| 55 | * adding information required by DiscoJuice. |
||
| 56 | * |
||
| 57 | * @param int $activeOnly if set to non-zero will cause listing of only those institutions which have some valid profiles defined. |
||
| 58 | * @param string $country if set, only list IdPs in a specific country |
||
| 59 | * @return array the list of identity providers |
||
| 60 | * |
||
| 61 | */ |
||
| 62 | public static function listAllIdentityProviders($activeOnly = 0, $country = "") |
||
| 63 | { |
||
| 64 | common\Entity::intoThePotatoes(); |
||
| 65 | $handle = DBConnection::handle("INST"); |
||
| 66 | $handle->exec("SET SESSION group_concat_max_len=10000"); |
||
| 67 | $query = IdPlist::setAllIdentyProvidersQuery($activeOnly, $country); |
||
| 68 | $allIDPs = ($country != "" ? $handle->exec($query, "s", $country) : $handle->exec($query)); |
||
| 69 | $idpArray = []; |
||
| 70 | // SELECTs never return a booleans, always an object |
||
| 71 | while ($queryResult = mysqli_fetch_object(/** @scrutinizer ignore-type */ $allIDPs)) { |
||
| 72 | $options = IdPlist::setIdentityProviderAttributes($queryResult); |
||
| 73 | $oneInstitutionResult = []; |
||
| 74 | $oneInstitutionResult['entityID'] = $queryResult->inst_id; |
||
| 75 | $oneInstitutionResult['country'] = strtoupper($queryResult->country); |
||
| 76 | if ($options['icon'] > 0) { |
||
| 77 | $oneInstitutionResult['icon'] = $options['icon']; |
||
| 78 | } |
||
| 79 | $name = _("Unnamed Entity"); |
||
| 80 | if (count($options['names']) != 0) { |
||
| 81 | $langObject = new \core\common\Language(); |
||
| 82 | $name = $langObject->getLocalisedValue($options['names']); |
||
| 83 | } |
||
| 84 | $oneInstitutionResult['title'] = $name; |
||
| 85 | $keywords = []; |
||
| 86 | foreach ($options['names'] as $keyword) { |
||
| 87 | $value = $keyword['value']; |
||
| 88 | $keywords[$keyword['lang']] = $keyword['value']; |
||
| 89 | $keywords[$keyword['lang'].'_7'] = |
||
| 90 | iconv('UTF-8', 'ASCII//TRANSLIT', $value); |
||
| 91 | } |
||
| 92 | |||
| 93 | if (\config\ConfAssistant::USE_KEYWORDS) { |
||
| 94 | $keywords_final = array_unique($keywords); |
||
| 95 | |||
| 96 | if (!empty($keywords_final)) { |
||
| 97 | $oneInstitutionResult['keywords'] = []; |
||
| 98 | foreach (array_keys($keywords_final) as $key) { |
||
| 99 | $oneInstitutionResult['keywords'][] = [$keywords_final[$key]]; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | } |
||
| 103 | if (count($options['geo']) > 0) { |
||
| 104 | $oneInstitutionResult['geo'] = $options['geo']; |
||
| 105 | } |
||
| 106 | $idpArray[] = $oneInstitutionResult; |
||
| 107 | } |
||
| 108 | common\Entity::outOfThePotatoes(); |
||
| 109 | return $idpArray; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * outputs a full list of IdPs containing the fllowing data: |
||
| 114 | * institution_is, institution name in all available languages, |
||
| 115 | * list of production profiles. |
||
| 116 | * For eache profile the profile identifier, profile name in all languages |
||
| 117 | * and redirect values (empty rediret value means that no redirect has been |
||
| 118 | * set). |
||
| 119 | * |
||
| 120 | * @return array of identity providers with attributes |
||
| 121 | */ |
||
| 122 | public static function listIdentityProvidersWithProfiles() { |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * sets the current location |
||
| 174 | * |
||
| 175 | * @param array $currentLocation the location to set |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | private static function setCurrentLocation($currentLocation) |
||
| 179 | { |
||
| 180 | if (is_null($currentLocation)) { |
||
| 181 | $currentLocation = ['lat' => "90", 'lon' => "0"]; |
||
| 182 | $userLocation = DeviceLocation::locateDevice(); |
||
| 183 | if ($userLocation['status'] == 'ok') { |
||
| 184 | $currentLocation = $userLocation['geo']; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | return $currentLocation; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * calculate surface distance from user location to IdP location |
||
| 192 | * @param array $idp the IdP in question |
||
| 193 | * @param array $location user location |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | private static function getIdpDistance($idp, $location) |
||
| 197 | { |
||
| 198 | $dist = 10000; |
||
| 199 | if (isset($idp['geo'])) { |
||
| 200 | $G = $idp['geo']; |
||
| 201 | if (isset($G['lon'])) { |
||
| 202 | $d1 = self::geoDistance($location, $G); |
||
| 203 | if ($d1 < $dist) { |
||
| 204 | $dist = $d1; |
||
| 205 | } |
||
| 206 | } else { |
||
| 207 | foreach ($G as $g) { |
||
| 208 | $d1 = self::geoDistance($location, $g); |
||
| 209 | if ($d1 < $dist) { |
||
| 210 | $dist = $d1; |
||
| 211 | } |
||
| 212 | } |
||
| 213 | } |
||
| 214 | } |
||
| 215 | if ($dist > 100) { |
||
| 216 | $dist = 10000; |
||
| 217 | } |
||
| 218 | return(sprintf("%06d", $dist)); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * set the IdP query string for listAllIdentityProviders and |
||
| 223 | * listIdentityProvidersWithProfiles |
||
| 224 | * @param int $activeOnly if set to non-zero will cause listing of only those institutions which have some valid profiles defined. |
||
| 225 | * @param string $country if set, only list IdPs in a specific country |
||
| 226 | * |
||
| 227 | * @return string the query |
||
| 228 | */ |
||
| 229 | private static function setAllIdentyProvidersQuery($activeOnly = 0, $country = "") |
||
| 230 | { |
||
| 231 | $query = "SELECT distinct institution.inst_id AS inst_id, |
||
| 232 | institution.country AS country, |
||
| 233 | group_concat(concat_ws('===',institution_option.option_name, |
||
| 234 | LEFT(institution_option.option_value,200), |
||
| 235 | institution_option.option_lang) separator '---') AS options |
||
| 236 | FROM institution "; |
||
| 237 | if ($activeOnly == 1) { |
||
| 238 | $query .= "JOIN v_active_inst ON institution.inst_id = v_active_inst.inst_id "; |
||
| 239 | } |
||
| 240 | $query .= |
||
| 241 | "JOIN institution_option ON institution.inst_id = institution_option.institution_id |
||
| 242 | WHERE (institution_option.option_name = 'general:instname' |
||
| 243 | OR institution_option.option_name = 'general:geo_coordinates' |
||
| 244 | OR institution_option.option_name = 'general:logo_file') "; |
||
| 245 | |||
| 246 | $query .= ($country != "" ? "AND institution.country = ? " : ""); |
||
| 247 | |||
| 248 | $query .= "GROUP BY institution.inst_id ORDER BY inst_id"; |
||
| 249 | return $query; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * set the Profile query string for listIdentityProvidersWithProfiles |
||
| 254 | * |
||
| 255 | * @return string query |
||
| 256 | */ |
||
| 257 | private static function setAllProfileQuery() { |
||
| 258 | $query = "SELECT profile.inst_id AS inst_id, |
||
| 259 | profile.profile_id, |
||
| 260 | group_concat(concat_ws('===',profile_option.option_name, |
||
| 261 | LEFT(profile_option.option_value, 200), |
||
| 262 | profile_option.option_lang) separator '---') AS profile_options |
||
| 263 | FROM profile |
||
| 264 | JOIN profile_option ON profile.profile_id = profile_option.profile_id |
||
| 265 | WHERE profile.sufficient_config = 1 |
||
| 266 | AND profile_option.eap_method_id = 0 |
||
| 267 | AND (profile_option.option_name = 'profile:name' |
||
| 268 | OR (profile_option.option_name = 'device-specific:redirect' |
||
| 269 | AND isnull(profile_option.device_id)) |
||
| 270 | OR profile_option.option_name = 'profile:production') |
||
| 271 | GROUP BY profile.profile_id ORDER BY inst_id"; |
||
| 272 | return $query; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Extract IdP attributes for listAllIdentityProviders and |
||
| 277 | * listIdentityProvidersWithProfiles |
||
| 278 | * @param $idp object - the row object returned by the IdP search |
||
| 279 | * @return array the IdP attributes |
||
| 280 | */ |
||
| 281 | private static function setIdentityProviderAttributes($idp) { |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Extract Profie attributes for listIdentityProvidersWithProfiles |
||
| 311 | * |
||
| 312 | * @param object $profile - the row object returned by the profile search |
||
| 313 | * @return array the profile attributes |
||
| 314 | */ |
||
| 315 | private static function setProfileAttributes($profile) |
||
| 349 | } |
||
| 350 | |||
| 351 | private static function setKeywords($names) |
||
|
|
|||
| 352 | { |
||
| 353 | if (!\config\ConfAssistant::USE_KEYWORDS) { |
||
| 354 | return null; |
||
| 355 | } |
||
| 356 | $keywords = []; |
||
| 357 | $returnArray = []; |
||
| 358 | foreach ($names as $keyword) { |
||
| 359 | $value = $keyword['value']; |
||
| 360 | $keywords[$keyword['lang']] = $keyword['value']; |
||
| 361 | $keywords[$keyword['lang'].'_7'] = |
||
| 362 | iconv('UTF-8', 'ASCII//TRANSLIT', $value); |
||
| 363 | } |
||
| 364 | $keywords_final = array_unique($keywords); |
||
| 365 | if (!empty($keywords_final)) { |
||
| 366 | foreach (array_keys($keywords_final) as $key) { |
||
| 367 | $returnArray[] = [$keywords_final[$key]]; |
||
| 368 | } |
||
| 369 | } |
||
| 370 | return $returnArray; |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Calculate the distance in km between two points given their |
||
| 375 | * geo coordinates. |
||
| 376 | * @param array $point1 first point as an 'lat', 'lon' array |
||
| 377 | * @param array $profile1 second point as an 'lat', 'lon' array |
||
| 378 | * @return float distance in km |
||
| 379 | */ |
||
| 380 | public static function geoDistance($point1, $profile1) |
||
| 387 | } |
||
| 388 | } |
This check looks for private methods that have been defined, but are not used inside the class.