| Total Complexity | 40 | 
| Total Lines | 250 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like User 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 User, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 43 | class User extends EntityWithDBProperties  | 
            ||
| 44 | { | 
            ||
| 45 | |||
| 46 | /**  | 
            ||
| 47 | *  | 
            ||
| 48 | * @var string  | 
            ||
| 49 | */  | 
            ||
| 50 | public $userName;  | 
            ||
| 51 | |||
| 52 | /**  | 
            ||
| 53 | * Class constructor. The required argument is a user's persistent identifier as was returned by the authentication source.  | 
            ||
| 54 | *  | 
            ||
| 55 | * @param string $userId User Identifier as per authentication source  | 
            ||
| 56 | */  | 
            ||
| 57 | public function __construct($userId)  | 
            ||
| 92 | }  | 
            ||
| 93 | }  | 
            ||
| 94 | |||
| 95 | /**  | 
            ||
| 96 | * This function checks whether a user is a federation administrator. When called without argument, it only checks if the  | 
            ||
| 97 | * user is a federation administrator of *any* federation. When given a parameter (ISO shortname of federation), it checks  | 
            ||
| 98 | * if the user administers this particular federation.  | 
            ||
| 99 | *  | 
            ||
| 100 | * @param string $federation optional: federation to be checked  | 
            ||
| 101 | * @return boolean TRUE if the user is federation admin, FALSE if not  | 
            ||
| 102 | */  | 
            ||
| 103 | public function isFederationAdmin($federation = 0)  | 
            ||
| 104 |     { | 
            ||
| 105 |         $feds = $this->getAttributes("user:fedadmin"); | 
            ||
| 106 |         if (count($feds) == 0) { // not a fedadmin at all | 
            ||
| 107 | return FALSE;  | 
            ||
| 108 | }  | 
            ||
| 109 |         if ($federation === 0) { // fedadmin for one; that's all we want to know | 
            ||
| 110 | return TRUE;  | 
            ||
| 111 | }  | 
            ||
| 112 |         foreach ($feds as $fed) { // check if authz is for requested federation | 
            ||
| 113 |             if (strtoupper($fed['value']) == strtoupper($federation)) { | 
            ||
| 114 | return TRUE;  | 
            ||
| 115 | }  | 
            ||
| 116 | }  | 
            ||
| 117 | return FALSE; // no luck so far? Not the admin we are looking for.  | 
            ||
| 118 | }  | 
            ||
| 119 | |||
| 120 | /**  | 
            ||
| 121 | * This function tests if the current user has been configured as the system superadmin, i.e. if the user is allowed  | 
            ||
| 122 | * to execute the 112365365321.php script  | 
            ||
| 123 | *  | 
            ||
| 124 | * @return boolean TRUE if the user is a superadmin, FALSE if not  | 
            ||
| 125 | */  | 
            ||
| 126 | public function isSuperadmin()  | 
            ||
| 127 |     { | 
            ||
| 128 | return in_array($this->userName, \config\Master::SUPERADMINS);  | 
            ||
| 129 | }  | 
            ||
| 130 | |||
| 131 | /**  | 
            ||
| 132 | * This function tests if the current user is an ovner of a given IdP  | 
            ||
| 133 | *  | 
            ||
| 134 | * @param int $idp integer identifier of the IdP  | 
            ||
| 135 | * @return boolean TRUE if the user is an owner, FALSE if not  | 
            ||
| 136 | */  | 
            ||
| 137 | public function isIdPOwner($idp)  | 
            ||
| 146 | }  | 
            ||
| 147 | |||
| 148 | /**  | 
            ||
| 149 | * This function lists all institution ids for which the user appears as admin  | 
            ||
| 150 | *  | 
            ||
| 151 | * @return array if institution ids.  | 
            ||
| 152 | */  | 
            ||
| 153 |     public function listOwnerships() { | 
            ||
| 154 |         $dbHandle = \core\DBConnection::handle("INST"); | 
            ||
| 155 |         $query = $dbHandle->exec("SELECT institution_id FROM ownership WHERE user_id='".$this->userName."'"); | 
            ||
| 156 | return array_column($query->fetch_all(), 0);  | 
            ||
| 157 | }  | 
            ||
| 158 | |||
| 159 | /**  | 
            ||
| 160 | * shorthand function for email sending to the user  | 
            ||
| 161 | *  | 
            ||
| 162 | * @param string $subject addressee of the mail  | 
            ||
| 163 | * @param string $content content of the mail  | 
            ||
| 164 | * @return boolean did it work?  | 
            ||
| 165 | */  | 
            ||
| 166 | public function sendMailToUser($subject, $content)  | 
            ||
| 167 |     { | 
            ||
| 168 | |||
| 169 |         $mailaddr = $this->getAttributes("user:email"); | 
            ||
| 170 |         if (count($mailaddr) == 0) { // we don't know user's mail address | 
            ||
| 171 | return FALSE;  | 
            ||
| 172 | }  | 
            ||
| 173 | common\Entity::intoThePotatoes();  | 
            ||
| 174 | $mail = \core\common\OutsideComm::mailHandle();  | 
            ||
| 175 | // who to whom?  | 
            ||
| 176 | $mail->FromName = \config\Master::APPEARANCE['productname'] . " Notification System";  | 
            ||
| 177 |         $mail->addReplyTo(\config\Master::APPEARANCE['support-contact']['developer-mail'], \config\Master::APPEARANCE['productname'] . " " . _("Feedback")); | 
            ||
| 178 | $mail->addAddress($mailaddr[0]["value"]);  | 
            ||
| 179 | // what do we want to say?  | 
            ||
| 180 | $mail->Subject = $subject;  | 
            ||
| 181 | $mail->Body = $content;  | 
            ||
| 182 | |||
| 183 | $sent = $mail->send();  | 
            ||
| 184 | common\Entity::outOfThePotatoes();  | 
            ||
| 185 | return $sent;  | 
            ||
| 186 | }  | 
            ||
| 187 | |||
| 188 | /**  | 
            ||
| 189 | * NOOP in this class, only need to override abstract base class  | 
            ||
| 190 | *  | 
            ||
| 191 | * @return void  | 
            ||
| 192 | */  | 
            ||
| 193 | public function updateFreshness()  | 
            ||
| 195 | // User is always fresh  | 
            ||
| 196 | }  | 
            ||
| 197 | |||
| 198 | const PROVIDER_STRINGS = [  | 
            ||
| 199 | "eduPersonTargetedID" => "eduGAIN",  | 
            ||
| 200 | "facebook_targetedID" => "Facebook",  | 
            ||
| 201 | "google_eppn" => "Google",  | 
            ||
| 202 | "linkedin_targetedID" => "LinkedIn",  | 
            ||
| 203 | "twitter_targetedID" => "Twitter",  | 
            ||
| 204 | "openid" => "Google (defunct)",  | 
            ||
| 205 | ];  | 
            ||
| 206 | |||
| 207 | /**  | 
            ||
| 208 | * Some users apparently forget which eduGAIN/social ID they originally used  | 
            ||
| 209 | * to log into CAT. We can try to help them: if they tell us the email  | 
            ||
| 210 | * address by which they received the invitation token, then we can see if  | 
            ||
| 211 | * any CAT IdPs are associated to an account which originally came in via  | 
            ||
| 212 | * that email address. We then see which pretty-print auth provider name  | 
            ||
| 213 | * was used  | 
            ||
| 214 | *  | 
            ||
| 215 | * @param string $mail mail address to search with  | 
            ||
| 216 | * @param string $lang language for the eduGAIN request  | 
            ||
| 217 | * @return boolean|array the list of auth source IdPs we found for the mail, or FALSE if none found or invalid input  | 
            ||
| 218 | */  | 
            ||
| 219 | public static function findLoginIdPByEmail($mail, $lang)  | 
            ||
| 293 | }  | 
            ||
| 294 | }  |