| Total Complexity | 50 |
| Total Lines | 291 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like EntityWithDBProperties 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 EntityWithDBProperties, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | abstract class EntityWithDBProperties extends \core\common\Entity { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * This variable gets initialised with the known IdP attributes in the constructor. It never gets updated until the object |
||
| 49 | * is destroyed. So if attributes change in the database, and IdP attributes are to be queried afterwards, the object |
||
| 50 | * needs to be re-instantiated to have current values in this variable. |
||
| 51 | * |
||
| 52 | * @var array of entity's attributes |
||
| 53 | */ |
||
| 54 | protected $attributes; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The database to query for attributes regarding this entity |
||
| 58 | * |
||
| 59 | * @var string DB type |
||
| 60 | */ |
||
| 61 | protected $databaseType; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * This variable contains the name of the table that stores the entity's options |
||
| 65 | * |
||
| 66 | * @var string DB table name |
||
| 67 | */ |
||
| 68 | protected $entityOptionTable; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * column name to find entity in that table |
||
| 72 | * |
||
| 73 | * @var string DB column name of entity |
||
| 74 | */ |
||
| 75 | protected $entityIdColumn; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * We need database access. Be sure to instantiate the singleton, and then |
||
| 79 | * use its instance (rather than always accessing everything statically) |
||
| 80 | * |
||
| 81 | * @var DBConnection the instance of the default database we talk to usually |
||
| 82 | */ |
||
| 83 | protected $databaseHandle; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * the unique identifier of this entity instance |
||
| 87 | * refers to the integer row name in the DB -> int; Federation has no own |
||
| 88 | * DB, so the identifier is of no use there -> use Fedearation->$tld |
||
| 89 | * |
||
| 90 | * @var int identifier of the entity instance |
||
| 91 | */ |
||
| 92 | public $identifier; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * the name of the entity in the current locale |
||
| 96 | */ |
||
| 97 | public $name; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * The constructor initialises the entity. Since it has DB properties, |
||
| 101 | * this means the DB connection is set up for it. |
||
| 102 | */ |
||
| 103 | public function __construct() { |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * How is the object identified in the database? |
||
| 113 | * @return string|int |
||
| 114 | * @throws Exception |
||
| 115 | */ |
||
| 116 | private function getRelevantIdentifier() { |
||
| 117 | switch (get_class($this)) { |
||
| 118 | case "core\ProfileRADIUS": |
||
| 119 | case "core\ProfileSilverbullet": |
||
| 120 | case "core\IdP": |
||
| 121 | case "core\DeploymentManaged": |
||
| 122 | return $this->identifier; |
||
| 123 | case "core\Federation": |
||
| 124 | return $this->tld; |
||
| 125 | case "core\User": |
||
| 126 | return $this->userName; |
||
| 127 | default: |
||
| 128 | throw new Exception("Operating on a class where we don't know the relevant identifier in the DB - ".get_class($this)."!"); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * This function retrieves the entity's attributes. |
||
| 134 | * |
||
| 135 | * If called with the optional parameter, only attribute values for the attribute |
||
| 136 | * name in $optionName are retrieved; otherwise, all attributes are retrieved. |
||
| 137 | * The retrieval is in-memory from the internal attributes class member - no |
||
| 138 | * DB callback, so changes in the database during the class instance lifetime |
||
| 139 | * are not considered. |
||
| 140 | * |
||
| 141 | * @param string $optionName optionally, the name of the attribute that is to be retrieved |
||
| 142 | * @return array of arrays of attributes which were set for this IdP |
||
| 143 | */ |
||
| 144 | public function getAttributes(string $optionName = NULL) { |
||
| 145 | if ($optionName !== NULL) { |
||
| 146 | $returnarray = []; |
||
| 147 | foreach ($this->attributes as $theAttr) { |
||
| 148 | if ($theAttr['name'] == $optionName) { |
||
| 149 | $returnarray[] = $theAttr; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | return $returnarray; |
||
| 153 | } |
||
| 154 | return $this->attributes; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * deletes all attributes in this profile except the _file ones, these are reported as array |
||
| 159 | * |
||
| 160 | * @param string $extracondition a condition to append to the deletion query. RADIUS Profiles have eap-level or device-level options which shouldn't be purged; this can be steered in the overriding function. |
||
| 161 | * @return array list of row id's of file-based attributes which weren't deleted |
||
| 162 | */ |
||
| 163 | public function beginFlushAttributes($extracondition = "") { |
||
| 164 | $quotedIdentifier = (!is_int($this->getRelevantIdentifier()) ? "\"" : "") . $this->getRelevantIdentifier() . (!is_int($this->getRelevantIdentifier()) ? "\"" : ""); |
||
| 165 | $this->databaseHandle->exec("DELETE FROM $this->entityOptionTable WHERE $this->entityIdColumn = $quotedIdentifier AND option_name NOT LIKE '%_file' $extracondition"); |
||
| 166 | $this->updateFreshness(); |
||
| 167 | $execFlush = $this->databaseHandle->exec("SELECT row FROM $this->entityOptionTable WHERE $this->entityIdColumn = $quotedIdentifier $extracondition"); |
||
| 168 | $returnArray = []; |
||
| 169 | // SELECT always returns a resourse, never a boolean |
||
| 170 | while ($queryResult = mysqli_fetch_object(/** @scrutinizer ignore-type */ $execFlush)) { |
||
| 171 | $returnArray[$queryResult->row] = "KILLME"; |
||
| 172 | } |
||
| 173 | return $returnArray; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * after a beginFlushAttributes, deletes all attributes which are in the tobedeleted array. |
||
| 178 | * |
||
| 179 | * @param array $tobedeleted array of database rows which are to be deleted |
||
| 180 | * @return void |
||
| 181 | */ |
||
| 182 | public function commitFlushAttributes(array $tobedeleted) { |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * deletes all attributes of this entity from the database |
||
| 192 | * |
||
| 193 | * @return void |
||
| 194 | */ |
||
| 195 | public function flushAttributes() { |
||
| 196 | $this->commitFlushAttributes($this->beginFlushAttributes()); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Adds an attribute for the entity instance into the database. Multiple instances of the same attribute are supported. |
||
| 201 | * |
||
| 202 | * @param string $attrName Name of the attribute. This must be a well-known value from the profile_option_dict table in the DB. |
||
| 203 | * @param string $attrLang language of the attribute. Can be NULL. |
||
| 204 | * @param mixed $attrValue Value of the attribute. Can be anything; will be stored in the DB as-is. |
||
| 205 | * @return void |
||
| 206 | */ |
||
| 207 | public function addAttribute($attrName, $attrLang, $attrValue) { |
||
| 208 | $relevantId = $this->getRelevantIdentifier(); |
||
| 209 | $identifierType = (is_int($relevantId) ? "i" : "s"); |
||
| 210 | $this->databaseHandle->exec("INSERT INTO $this->entityOptionTable ($this->entityIdColumn, option_name, option_lang, option_value) VALUES(?,?,?,?)", $identifierType . "sss", $relevantId, $attrName, $attrLang, $attrValue); |
||
| 211 | $this->updateFreshness(); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * retrieve attributes from a database. Only does SELECT queries. |
||
| 216 | * @param string $query sub-classes set the query to execute to get to the options |
||
| 217 | * @param string $level the retrieved options get flagged with this "level" identifier |
||
| 218 | * @return array the attributes in one array |
||
| 219 | */ |
||
| 220 | protected function retrieveOptionsFromDatabase($query, $level) { |
||
| 221 | if (substr($query, 0, 6) != "SELECT") { |
||
| 222 | throw new Exception("This function only operates with SELECT queries!"); |
||
| 223 | } |
||
| 224 | $optioninstance = Options::instance(); |
||
| 225 | $tempAttributes = []; |
||
| 226 | $relevantId = $this->getRelevantIdentifier(); |
||
| 227 | $attributeDbExec = $this->databaseHandle->exec($query, is_int($relevantId) ? "i" : "s", $relevantId); |
||
| 228 | if (empty($attributeDbExec)) { |
||
| 229 | return $tempAttributes; |
||
| 230 | } |
||
| 231 | // with SELECTs, we always operate on a resource, not a boolean |
||
| 232 | while ($attributeQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $attributeDbExec)) { |
||
| 233 | $optinfo = $optioninstance->optionType($attributeQuery->option_name); |
||
| 234 | $flag = $optinfo['flag']; |
||
| 235 | $decoded = $attributeQuery->option_value; |
||
| 236 | // file attributes always get base64-decoded. |
||
| 237 | if ($optinfo['type'] == 'file') { |
||
| 238 | $decoded = base64_decode($decoded); |
||
| 239 | } |
||
| 240 | $tempAttributes[] = ["name" => $attributeQuery->option_name, "lang" => $attributeQuery->option_lang, "value" => $decoded, "level" => $level, "row" => $attributeQuery->row, "flag" => $flag]; |
||
| 241 | } |
||
| 242 | return $tempAttributes; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Retrieves data from the underlying tables, for situations where instantiating the IdP or Profile object is inappropriate |
||
| 247 | * |
||
| 248 | * @param string $table institution_option or profile_option |
||
| 249 | * @param string $row rowindex |
||
| 250 | * @return string|boolean the data, or FALSE if something went wrong |
||
| 251 | */ |
||
| 252 | public static function fetchRawDataByIndex($table, $row) { |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Checks if a raw data pointer is public data (return value FALSE) or if |
||
| 275 | * yes who the authorised admins to view it are (return array of user IDs) |
||
| 276 | * |
||
| 277 | * @param string $table which database table is this about |
||
| 278 | * @param int $row row index of the table |
||
| 279 | * @return mixed FALSE if the data is public, an array of owners of the data if it is NOT public |
||
| 280 | */ |
||
| 281 | public static function isDataRestricted($table, $row) { |
||
| 327 | } |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * when options in the DB change, this can mean generated installers become stale. sub-classes must define whether this is the case for them |
||
| 332 | * |
||
| 333 | * @return void |
||
| 334 | */ |
||
| 335 | abstract public function updateFreshness(); |
||
| 336 | } |
||
| 337 |