| Total Complexity | 44 |
| Total Lines | 293 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ProfileHandler 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 ProfileHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 31 | class ProfileHandler extends \XoopsPersistableObjectHandler |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * holds reference to {@link ProfileFieldHandler} object |
||
| 35 | */ |
||
| 36 | public $fieldHandler; |
||
| 37 | /** |
||
| 38 | * Array of {@link Suico\Field} objects |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | public array $_fields = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param \XoopsDatabase $db |
||
| 45 | */ |
||
| 46 | public function __construct(\XoopsDatabase $db) |
||
| 47 | { |
||
| 48 | parent::__construct($db, 'suico_profile', Profile::class, 'profile_id'); |
||
| 49 | $this->fieldHandler = Helper::getInstance()->getHandler('Field'); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * create a new {@link ProfileProfile} |
||
| 54 | * |
||
| 55 | * @param bool $isNew Flag the new objects as "new"? |
||
| 56 | * |
||
| 57 | * @return object {@link ProfileProfile} |
||
| 58 | */ |
||
| 59 | public function create($isNew = true) |
||
| 60 | { |
||
| 61 | $obj = new $this->className($this->loadFields()); |
||
| 62 | $obj->handler = $this; |
||
| 63 | if ($isNew) { |
||
| 64 | $obj->setNew(); |
||
| 65 | } |
||
| 66 | |||
| 67 | return $obj; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Get a ProfileProfile object for a user id. |
||
| 72 | * |
||
| 73 | * We will create an empty profile if none exists. This behavior allows user objects |
||
| 74 | * created outside of profile to be edited correctly in the profile module. |
||
| 75 | * |
||
| 76 | * @param int|null $id User ID |
||
| 77 | * @param string[]|null $fields array of field names to fetch, null for all |
||
| 78 | * |
||
| 79 | * @return object {@link ProfileProfile} |
||
| 80 | * |
||
| 81 | * @internal This was get($id, $createOnFailure = true). No callers found using the extra parameter. |
||
| 82 | * @internal Modified to match parent signature. |
||
| 83 | */ |
||
| 84 | public function get($id = null, $fields = null) |
||
| 85 | { |
||
| 86 | $obj = parent::get($id, $fields); |
||
| 87 | if (!\is_object($obj)) { |
||
| 88 | $obj = $this->create(); |
||
| 89 | } |
||
| 90 | |||
| 91 | return $obj; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Create new {@link Suico\Field} object |
||
| 96 | * |
||
| 97 | * @param bool $isNew |
||
| 98 | * |
||
| 99 | * @return \XoopsModules\Suico\Field|\XoopsObject |
||
| 100 | */ |
||
| 101 | public function createField($isNew = true) |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Load field information |
||
| 110 | * |
||
| 111 | * @return array |
||
| 112 | */ |
||
| 113 | public function loadFields() |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Fetch fields |
||
| 124 | * |
||
| 125 | * @param \CriteriaElement $criteria {@link CriteriaElement} object |
||
| 126 | * @param bool $id_as_key return array with field IDs as key? |
||
| 127 | * @param bool $as_object return array of objects? |
||
| 128 | * |
||
| 129 | * @return array |
||
| 130 | */ |
||
| 131 | public function getFields(\CriteriaElement $criteria, $id_as_key = true, $as_object = true) |
||
| 132 | { |
||
| 133 | return $this->fieldHandler->getObjects($criteria, $id_as_key, $as_object); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Insert a field in the database |
||
| 138 | * |
||
| 139 | * @param \XoopsModules\Suico\Field $field |
||
| 140 | * @param bool $force |
||
| 141 | * |
||
| 142 | * @return bool |
||
| 143 | */ |
||
| 144 | public function insertField(Suico\Field $field, $force = false) |
||
| 145 | { |
||
| 146 | return $this->fieldHandler->insert($field, $force); |
||
|
|
|||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Delete a field from the database |
||
| 151 | * |
||
| 152 | * @param \XoopsModules\Suico\Field $field |
||
| 153 | * @param bool $force |
||
| 154 | * |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | public function deleteField(Suico\Field $field, $force = false) |
||
| 158 | { |
||
| 159 | return $this->fieldHandler->delete($field, $force); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Save a new field in the database |
||
| 164 | * |
||
| 165 | * @param array $vars array of variables, taken from $module->loadInfo('profile')['field'] |
||
| 166 | * @param int $weight |
||
| 167 | * |
||
| 168 | * @return string |
||
| 169 | * @internal param int $type valuetype of the field |
||
| 170 | * @internal param int $moduleid ID of the module, this field belongs to |
||
| 171 | * @internal param int $categoryid ID of the category to add it to |
||
| 172 | */ |
||
| 173 | public function saveField($vars, $weight = 0) |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * insert a new object in the database |
||
| 224 | * |
||
| 225 | * @param \XoopsObject $object reference to the object |
||
| 226 | * @param bool $force whether to force the query execution despite security settings |
||
| 227 | * |
||
| 228 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 229 | */ |
||
| 230 | public function insert(\XoopsObject $object, $force = false) |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Get array of standard variable names (user table) |
||
| 248 | * |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | public function getUserVars() |
||
| 252 | { |
||
| 253 | return $this->fieldHandler->getUserVars(); |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Search profiles and users |
||
| 258 | * |
||
| 259 | * @param \CriteriaElement $criteria CriteriaElement |
||
| 260 | * @param array $searchvars Fields to be fetched |
||
| 261 | * @param array|null $groups for Usergroups is selected (only admin!) |
||
| 262 | * |
||
| 263 | * @return array |
||
| 264 | */ |
||
| 265 | public function search(\CriteriaElement $criteria, $searchvars = [], $groups = null) |
||
| 324 | } |
||
| 325 | } |
||
| 326 |