| Total Complexity | 55 |
| Total Lines | 391 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Members 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 Members, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Members extends DolibarrApi |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var array $FIELDS Mandatory fields, checked when create and update object |
||
| 35 | */ |
||
| 36 | static $FIELDS = array( |
||
| 37 | 'morphy', |
||
| 38 | 'typeid' |
||
| 39 | ); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Constructor |
||
| 43 | */ |
||
| 44 | public function __construct() |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get properties of a member object |
||
| 52 | * |
||
| 53 | * Return an array with member informations |
||
| 54 | * |
||
| 55 | * @param int $id ID of member |
||
| 56 | * @return array|mixed data without useless information |
||
| 57 | * |
||
| 58 | * @throws RestException |
||
| 59 | */ |
||
| 60 | public function get($id) |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * List members |
||
| 81 | * |
||
| 82 | * Get a list of members |
||
| 83 | * |
||
| 84 | * @param string $sortfield Sort field |
||
| 85 | * @param string $sortorder Sort order |
||
| 86 | * @param int $limit Limit for list |
||
| 87 | * @param int $page Page number |
||
| 88 | * @param string $typeid ID of the type of member |
||
| 89 | * @param int $category Use this param to filter list by category |
||
| 90 | * @param string $sqlfilters Other criteria to filter answers separated by a comma. |
||
| 91 | * Example: "(t.ref:like:'SO-%') and ((t.date_creation:<:'20160101') or (t.nature:is:NULL))" |
||
| 92 | * @return array Array of member objects |
||
| 93 | * |
||
| 94 | * @throws RestException |
||
| 95 | */ |
||
| 96 | public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $typeid = '', $category = 0, $sqlfilters = '') |
||
| 97 | { |
||
| 98 | global $db, $conf; |
||
| 99 | |||
| 100 | $obj_ret = array(); |
||
| 101 | |||
| 102 | if (!DolibarrApiAccess::$user->rights->adherent->lire) { |
||
| 103 | throw new RestException(401); |
||
| 104 | } |
||
| 105 | |||
| 106 | $sql = "SELECT t.rowid"; |
||
| 107 | $sql .= " FROM ".MAIN_DB_PREFIX."adherent as t"; |
||
| 108 | if ($category > 0) { |
||
| 109 | $sql .= ", ".MAIN_DB_PREFIX."categorie_member as c"; |
||
| 110 | } |
||
| 111 | $sql .= ' WHERE t.entity IN ('.getEntity('adherent').')'; |
||
| 112 | if (!empty($typeid)) |
||
| 113 | { |
||
| 114 | $sql .= ' AND t.fk_adherent_type='.$typeid; |
||
| 115 | } |
||
| 116 | // Select members of given category |
||
| 117 | if ($category > 0) { |
||
| 118 | $sql .= " AND c.fk_categorie = ".$db->escape($category); |
||
| 119 | $sql .= " AND c.fk_member = t.rowid "; |
||
| 120 | } |
||
| 121 | // Add sql filters |
||
| 122 | if ($sqlfilters) |
||
| 123 | { |
||
| 124 | if (!DolibarrApi::_checkFilters($sqlfilters)) |
||
| 125 | { |
||
| 126 | throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters); |
||
| 127 | } |
||
| 128 | $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; |
||
| 129 | $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; |
||
| 130 | } |
||
| 131 | |||
| 132 | $sql .= $db->order($sortfield, $sortorder); |
||
| 133 | if ($limit) { |
||
| 134 | if ($page < 0) |
||
| 135 | { |
||
| 136 | $page = 0; |
||
| 137 | } |
||
| 138 | $offset = $limit * $page; |
||
| 139 | |||
| 140 | $sql .= $db->plimit($limit + 1, $offset); |
||
| 141 | } |
||
| 142 | |||
| 143 | $result = $db->query($sql); |
||
| 144 | if ($result) |
||
| 145 | { |
||
| 146 | $i = 0; |
||
| 147 | $num = $db->num_rows($result); |
||
| 148 | $min = min($num, ($limit <= 0 ? $num : $limit)); |
||
| 149 | while ($i < $min) |
||
| 150 | { |
||
| 151 | $obj = $db->fetch_object($result); |
||
| 152 | $member = new Adherent($this->db); |
||
| 153 | if ($member->fetch($obj->rowid)) { |
||
| 154 | $obj_ret[] = $this->_cleanObjectDatas($member); |
||
| 155 | } |
||
| 156 | $i++; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | else { |
||
| 160 | throw new RestException(503, 'Error when retrieve member list : '.$db->lasterror()); |
||
| 161 | } |
||
| 162 | if (!count($obj_ret)) { |
||
| 163 | throw new RestException(404, 'No member found'); |
||
| 164 | } |
||
| 165 | |||
| 166 | return $obj_ret; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Create member object |
||
| 171 | * |
||
| 172 | * @param array $request_data Request data |
||
| 173 | * @return int ID of member |
||
| 174 | */ |
||
| 175 | public function post($request_data = null) |
||
| 176 | { |
||
| 177 | if (!DolibarrApiAccess::$user->rights->adherent->creer) { |
||
| 178 | throw new RestException(401); |
||
| 179 | } |
||
| 180 | // Check mandatory fields |
||
| 181 | $result = $this->_validate($request_data); |
||
| 182 | |||
| 183 | $member = new Adherent($this->db); |
||
| 184 | foreach ($request_data as $field => $value) { |
||
| 185 | $member->$field = $value; |
||
| 186 | } |
||
| 187 | if ($member->create(DolibarrApiAccess::$user) < 0) { |
||
| 188 | throw new RestException(500, 'Error creating member', array_merge(array($member->error), $member->errors)); |
||
| 189 | } |
||
| 190 | return $member->id; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Update member |
||
| 195 | * |
||
| 196 | * @param int $id ID of member to update |
||
| 197 | * @param array $request_data Datas |
||
| 198 | * @return int |
||
| 199 | */ |
||
| 200 | public function put($id, $request_data = null) |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Delete member |
||
| 251 | * |
||
| 252 | * @param int $id member ID |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | public function delete($id) |
||
| 256 | { |
||
| 257 | if (!DolibarrApiAccess::$user->rights->adherent->supprimer) { |
||
| 258 | throw new RestException(401); |
||
| 259 | } |
||
| 260 | $member = new Adherent($this->db); |
||
| 261 | $result = $member->fetch($id); |
||
| 262 | if (!$result) { |
||
| 263 | throw new RestException(404, 'member not found'); |
||
| 264 | } |
||
| 265 | |||
| 266 | if (!DolibarrApi::_checkAccessToResource('member', $member->id)) { |
||
| 267 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 268 | } |
||
| 269 | |||
| 270 | if (!$member->delete($member->id, DolibarrApiAccess::$user)) { |
||
| 271 | throw new RestException(401, 'error when deleting member'); |
||
| 272 | } |
||
| 273 | |||
| 274 | return array( |
||
| 275 | 'success' => array( |
||
| 276 | 'code' => 200, |
||
| 277 | 'message' => 'member deleted' |
||
| 278 | ) |
||
| 279 | ); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Validate fields before creating an object |
||
| 284 | * |
||
| 285 | * @param array|null $data Data to validate |
||
| 286 | * @return array |
||
| 287 | * |
||
| 288 | * @throws RestException |
||
| 289 | */ |
||
| 290 | private function _validate($data) |
||
| 299 | } |
||
| 300 | |||
| 301 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore |
||
| 302 | /** |
||
| 303 | * Clean sensible object datas |
||
| 304 | * |
||
| 305 | * @param object $object Object to clean |
||
| 306 | * @return array Array of cleaned object properties |
||
| 307 | */ |
||
| 308 | protected function _cleanObjectDatas($object) |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * List subscriptions of a member |
||
| 332 | * |
||
| 333 | * Get a list of subscriptions |
||
| 334 | * |
||
| 335 | * @param int $id ID of member |
||
| 336 | * @return array Array of subscription objects |
||
| 337 | * |
||
| 338 | * @throws RestException |
||
| 339 | * |
||
| 340 | * @url GET {id}/subscriptions |
||
| 341 | */ |
||
| 342 | public function getSubscriptions($id) |
||
| 343 | { |
||
| 344 | $obj_ret = array(); |
||
| 345 | |||
| 346 | if (!DolibarrApiAccess::$user->rights->adherent->cotisation->lire) { |
||
| 347 | throw new RestException(401); |
||
| 348 | } |
||
| 349 | |||
| 350 | $member = new Adherent($this->db); |
||
| 351 | $result = $member->fetch($id); |
||
| 352 | if (!$result) { |
||
| 353 | throw new RestException(404, 'member not found'); |
||
| 354 | } |
||
| 355 | |||
| 356 | $obj_ret = array(); |
||
| 357 | foreach ($member->subscriptions as $subscription) { |
||
| 358 | $obj_ret[] = $this->_cleanObjectDatas($subscription); |
||
| 359 | } |
||
| 360 | return $obj_ret; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Add a subscription for a member |
||
| 365 | * |
||
| 366 | * @param int $id ID of member |
||
| 367 | * @param int $start_date Start date {@from body} {@type timestamp} |
||
| 368 | * @param int $end_date End date {@from body} {@type timestamp} |
||
| 369 | * @param float $amount Amount (may be 0) {@from body} |
||
| 370 | * @param string $label Label {@from body} |
||
| 371 | * @return int ID of subscription |
||
| 372 | * |
||
| 373 | * @url POST {id}/subscriptions |
||
| 374 | */ |
||
| 375 | public function createSubscription($id, $start_date, $end_date, $amount, $label = '') |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Get categories for a member |
||
| 392 | * |
||
| 393 | * @param int $id ID of member |
||
| 394 | * @param string $sortfield Sort field |
||
| 395 | * @param string $sortorder Sort order |
||
| 396 | * @param int $limit Limit for list |
||
| 397 | * @param int $page Page number |
||
| 398 | * |
||
| 399 | * @return mixed |
||
| 400 | * |
||
| 401 | * @url GET {id}/categories |
||
| 402 | */ |
||
| 403 | public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) |
||
| 422 | } |
||
| 423 | } |
||
| 424 |