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