Total Complexity | 44 |
Total Lines | 361 |
Duplicated Lines | 0 % |
Changes | 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 |
||
37 | class ProfileHandler extends \XoopsPersistableObjectHandler |
||
38 | { |
||
39 | /** |
||
40 | * holds reference to {@link profileFieldHandler} object |
||
41 | */ |
||
42 | |||
43 | public $_fHandler; |
||
44 | |||
45 | /** |
||
46 | * Array of {@link XoopsYogurt\Field} objects |
||
47 | * @var array |
||
48 | */ |
||
49 | |||
50 | public $_fields = []; |
||
51 | |||
52 | /** |
||
53 | * @param \XoopsDatabase $db |
||
54 | */ |
||
55 | |||
56 | public function __construct(\XoopsDatabase $db) |
||
57 | { |
||
58 | parent::__construct($db, 'yogurt_profile', Profile::class, 'profile_id'); |
||
59 | |||
60 | $this->_fHandler = \XoopsModules\Yogurt\Helper::getInstance()->getHandler('Field'); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * create a new {@link ProfileProfile} |
||
65 | * |
||
66 | * @param bool $isNew Flag the new objects as "new"? |
||
67 | * |
||
68 | * @return object {@link ProfileProfile} |
||
69 | */ |
||
70 | |||
71 | public function create($isNew = true) |
||
72 | { |
||
73 | $obj = new $this->className($this->loadFields()); |
||
74 | |||
75 | $obj->handler = $this; |
||
76 | |||
77 | if ($isNew) { |
||
78 | $obj->setNew(); |
||
79 | } |
||
80 | |||
81 | return $obj; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Get a ProfileProfile object for a user id. |
||
86 | * |
||
87 | * We will create an empty profile if none exists. This behavior allows user objects |
||
88 | * created outside of profile to be edited correctly in the profile module. |
||
89 | * |
||
90 | * @param int|null $uid |
||
91 | * @param string[]|null $fields array of field names to fetch, null for all |
||
92 | * |
||
93 | * @return object {@link ProfileProfile} |
||
94 | * |
||
95 | * @internal This was get($uid, $createOnFailure = true). No callers found using the extra parameter. |
||
96 | * @internal Modified to match parent signature. |
||
97 | */ |
||
98 | |||
99 | public function get($uid = null, $fields = null) |
||
100 | { |
||
101 | $obj = parent::get($uid, $fields); |
||
102 | |||
103 | if (!\is_object($obj)) { |
||
104 | $obj = $this->create(); |
||
105 | } |
||
106 | |||
107 | return $obj; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Create new {@link Yogurt\Field} object |
||
112 | * |
||
113 | * @param bool $isNew |
||
114 | * |
||
115 | * @return Yogurt\Field |
||
116 | */ |
||
117 | |||
118 | public function createField($isNew = true) |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Load field information |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | |||
131 | public function loadFields() |
||
132 | { |
||
133 | if (0 == \count($this->_fields)) { |
||
134 | $this->_fields = $this->_fHandler->loadFields(); |
||
135 | } |
||
136 | |||
137 | return $this->_fields; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Fetch fields |
||
142 | * |
||
143 | * @param \CriteriaElement $criteria {@link CriteriaElement} object |
||
144 | * @param bool $id_as_key return array with field IDs as key? |
||
145 | * @param bool $as_object return array of objects? |
||
146 | * |
||
147 | * @return array |
||
148 | */ |
||
149 | |||
150 | public function getFields(\CriteriaElement $criteria, $id_as_key = true, $as_object = true) |
||
151 | { |
||
152 | return $this->_fHandler->getObjects($criteria, $id_as_key, $as_object); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Insert a field in the database |
||
157 | * |
||
158 | * @param \XoopsModules\Yogurt\Field $field |
||
159 | * @param bool $force |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | |||
164 | public function insertField(Yogurt\Field $field, $force = false) |
||
165 | { |
||
166 | return $this->_fHandler->insert($field, $force); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Delete a field from the database |
||
171 | * |
||
172 | * @param \XoopsModules\Yogurt\Field $field |
||
173 | * @param bool $force |
||
174 | * |
||
175 | * @return bool |
||
176 | */ |
||
177 | |||
178 | public function deleteField(Yogurt\Field $field, $force = false) |
||
179 | { |
||
180 | return $this->_fHandler->delete($field, $force); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Save a new field in the database |
||
185 | * |
||
186 | * @param array $vars array of variables, taken from $module->loadInfo('profile')['field'] |
||
187 | * @param int $weight |
||
188 | * |
||
189 | * @return string |
||
190 | * @internal param int $type valuetype of the field |
||
191 | * @internal param int $moduleid ID of the module, this field belongs to |
||
192 | * @internal param int $categoryid ID of the category to add it to |
||
193 | */ |
||
194 | |||
195 | public function saveField($vars, $weight = 0) |
||
196 | { |
||
197 | $field = $this->createField(); |
||
198 | |||
199 | $field->setVar('field_name', $vars['name']); |
||
200 | |||
201 | $field->setVar('field_valuetype', $vars['valuetype']); |
||
202 | |||
203 | $field->setVar('field_type', $vars['type']); |
||
204 | |||
205 | $field->setVar('field_weight', $weight); |
||
206 | |||
207 | if (isset($vars['title'])) { |
||
208 | $field->setVar('field_title', $vars['title']); |
||
209 | } |
||
210 | |||
211 | if (isset($vars['description'])) { |
||
212 | $field->setVar('field_description', $vars['description']); |
||
213 | } |
||
214 | |||
215 | if (isset($vars['required'])) { |
||
216 | $field->setVar('field_required', $vars['required']); //0 = no, 1 = yes |
||
217 | } |
||
218 | |||
219 | if (isset($vars['maxlength'])) { |
||
220 | $field->setVar('field_maxlength', $vars['maxlength']); |
||
221 | } |
||
222 | |||
223 | if (isset($vars['default'])) { |
||
224 | $field->setVar('field_default', $vars['default']); |
||
225 | } |
||
226 | |||
227 | if (isset($vars['notnull'])) { |
||
228 | $field->setVar('field_notnull', $vars['notnull']); |
||
229 | } |
||
230 | |||
231 | if (isset($vars['show'])) { |
||
232 | $field->setVar('field_show', $vars['show']); |
||
233 | } |
||
234 | |||
235 | if (isset($vars['edit'])) { |
||
236 | $field->setVar('field_edit', $vars['edit']); |
||
237 | } |
||
238 | |||
239 | if (isset($vars['config'])) { |
||
240 | $field->setVar('field_config', $vars['config']); |
||
241 | } |
||
242 | |||
243 | if (isset($vars['options'])) { |
||
244 | $field->setVar('field_options', $vars['options']); |
||
245 | } else { |
||
246 | $field->setVar('field_options', []); |
||
247 | } |
||
248 | |||
249 | if ($this->insertField($field)) { |
||
250 | $msg = ' Field <strong>' . $vars['name'] . '</strong> added to the database'; |
||
251 | } else { |
||
252 | $msg = ' <span class="red">ERROR: Could not insert field <strong>' . $vars['name'] . '</strong> into the database. ' . \implode(' ', $field->getErrors()) . $this->db->error() . '</span>'; |
||
253 | } |
||
254 | |||
255 | unset($field); |
||
256 | |||
257 | return $msg; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * insert a new object in the database |
||
262 | * |
||
263 | * @param \XoopsObject $obj reference to the object |
||
264 | * @param bool $force whether to force the query execution despite security settings |
||
265 | * |
||
266 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
267 | */ |
||
268 | |||
269 | public function insert(\XoopsObject $obj, $force = false) |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Get array of standard variable names (user table) |
||
290 | * |
||
291 | * @return array |
||
292 | */ |
||
293 | |||
294 | public function getUserVars() |
||
295 | { |
||
296 | return $this->_fHandler->getUserVars(); |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Search profiles and users |
||
301 | * |
||
302 | * @param \CriteriaElement $criteria CriteriaElement |
||
303 | * @param array $searchvars Fields to be fetched |
||
304 | * @param array $groups for Usergroups is selected (only admin!) |
||
305 | * |
||
306 | * @return array |
||
307 | */ |
||
308 | |||
309 | public function search(\CriteriaElement $criteria, $searchvars = [], $groups = null) |
||
398 | } |
||
399 | } |
||
400 |