| Total Complexity | 43 |
| Total Lines | 253 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Model 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 Model, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Model |
||
| 11 | { |
||
| 12 | public $table; |
||
| 13 | public $columns; |
||
| 14 | public $required; |
||
| 15 | public $is_course_model = false; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Constructor. |
||
| 19 | */ |
||
| 20 | public function __construct() |
||
| 21 | { |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Useful finder - experimental akelos like only use in notification.lib.php send function. |
||
| 26 | * |
||
| 27 | * @param string $type |
||
| 28 | * @param array $options |
||
| 29 | * |
||
| 30 | * @return array |
||
| 31 | */ |
||
| 32 | public function find($type, $options = null) |
||
| 33 | { |
||
| 34 | switch ($type) { |
||
| 35 | case 'all': |
||
| 36 | return self::get_all($options); |
||
|
|
|||
| 37 | break; |
||
| 38 | default: |
||
| 39 | if (is_numeric($type)) { |
||
| 40 | return self::get($type); |
||
| 41 | } |
||
| 42 | break; |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Deletes an item. |
||
| 48 | * |
||
| 49 | * @param int $id |
||
| 50 | * |
||
| 51 | * @return bool |
||
| 52 | */ |
||
| 53 | public function delete($id) |
||
| 54 | { |
||
| 55 | if (empty($id) || $id != strval(intval($id))) { |
||
| 56 | return false; |
||
| 57 | } |
||
| 58 | $params = ['id = ?' => $id]; |
||
| 59 | if ($this->is_course_model) { |
||
| 60 | $courseId = api_get_course_int_id(); |
||
| 61 | $params = ['id = ? AND c_id = ?' => [$id, $courseId]]; |
||
| 62 | } |
||
| 63 | |||
| 64 | // Database table definition |
||
| 65 | $result = Database::delete($this->table, $params); |
||
| 66 | if (1 != $result) { |
||
| 67 | return false; |
||
| 68 | } |
||
| 69 | |||
| 70 | return true; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Displays the title + grid. |
||
| 75 | */ |
||
| 76 | public function display() |
||
| 77 | { |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Gets an element. |
||
| 82 | * |
||
| 83 | * @param int $id |
||
| 84 | * |
||
| 85 | * @return array|mixed |
||
| 86 | */ |
||
| 87 | public function get($id) |
||
| 88 | { |
||
| 89 | if (empty($id)) { |
||
| 90 | return []; |
||
| 91 | } |
||
| 92 | $params = ['id = ?' => intval($id)]; |
||
| 93 | if ($this->is_course_model) { |
||
| 94 | $course_id = api_get_course_int_id(); |
||
| 95 | $params = ['id = ? AND c_id = ?' => [$id, $course_id]]; |
||
| 96 | } |
||
| 97 | $result = Database::select( |
||
| 98 | '*', |
||
| 99 | $this->table, |
||
| 100 | ['where' => $params], |
||
| 101 | 'first' |
||
| 102 | ); |
||
| 103 | |||
| 104 | return $result; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param array $options |
||
| 109 | * |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | public function get_all($options = null) |
||
| 113 | { |
||
| 114 | return Database::select('*', $this->table, $options); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param array $options |
||
| 119 | * |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | public function getDataToExport($options = []) |
||
| 123 | { |
||
| 124 | return Database::select('name, description', $this->table, $options); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get the count of elements. |
||
| 129 | * |
||
| 130 | * @return int |
||
| 131 | */ |
||
| 132 | public function get_count() |
||
| 133 | { |
||
| 134 | $row = Database::select( |
||
| 135 | 'count(*) as count', |
||
| 136 | $this->table, |
||
| 137 | ['where' => ['parent_id = ?' => '0']], |
||
| 138 | 'first' |
||
| 139 | ); |
||
| 140 | |||
| 141 | return $row['count']; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * a little bit of javascript to display. |
||
| 146 | */ |
||
| 147 | public function javascript() |
||
| 148 | { |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Saves an element into the DB. |
||
| 153 | * |
||
| 154 | * @param array $params |
||
| 155 | * @param bool $show_query Whether to show the query in logs or not (passed to Database::insert()) |
||
| 156 | * |
||
| 157 | * @return bool|int |
||
| 158 | */ |
||
| 159 | public function save($params, $show_query = false) |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Updates the obj in the database. The $params['id'] must exist in order to update a record. |
||
| 202 | * |
||
| 203 | * @param array $params |
||
| 204 | * @param bool $showQuery |
||
| 205 | * |
||
| 206 | * @return bool |
||
| 207 | */ |
||
| 208 | public function update($params, $showQuery = false) |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param array $params |
||
| 248 | * |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | private function clean_parameters($params) |
||
| 263 | } |
||
| 264 | } |
||
| 265 |