Total Complexity | 58 |
Total Lines | 250 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like TableModel 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 TableModel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | abstract class TableModel extends Crudites |
||
12 | { |
||
13 | |||
14 | //check all primary keys are set (FIXME that doesn't work unles AIPK.. nice try) |
||
15 | public function is_new(): bool |
||
16 | { |
||
17 | $match = static::table()->primary_keys_match(get_object_vars($this)); |
||
18 | return empty($match); |
||
19 | } |
||
20 | |||
21 | public function get_id($mode = null) |
||
29 | } |
||
30 | |||
31 | public function get($prop_name) |
||
32 | { |
||
33 | if (property_exists($this, $prop_name) === true) { |
||
34 | return $this->$prop_name; |
||
35 | } |
||
36 | |||
37 | return null; |
||
38 | } |
||
39 | |||
40 | public function set($prop_name, $value) |
||
43 | } |
||
44 | |||
45 | public function import($assoc_data) |
||
46 | { |
||
47 | if (!is_array($assoc_data)) { |
||
48 | throw new \Exception(__FUNCTION__ . '(assoc_data) parm is not an array'); |
||
49 | } |
||
50 | |||
51 | // shove it all up in model, god will sort them out |
||
52 | foreach ($assoc_data as $field => $value) { |
||
53 | $this->set($field, $value); |
||
54 | } |
||
55 | |||
56 | return $this; |
||
57 | } |
||
58 | |||
59 | public static function table(): TableManipulationInterface |
||
65 | } |
||
66 | |||
67 | public static function table_name(): string |
||
83 | } |
||
84 | |||
85 | |||
86 | public function to_table_row($operator_id = null) |
||
105 | } |
||
106 | |||
107 | // DEPRECATED, only exist for unit testing vis-a-vis TightModelSelector |
||
108 | public static function query_retrieve($filters = [], $options = []): SelectInterface |
||
109 | { |
||
110 | $class = get_called_class(); |
||
111 | $table = $class::table(); |
||
112 | |||
113 | $Query = $table->select(null, $class::table_alias()); |
||
114 | |||
115 | |||
116 | if(!isset($options['eager']) || $options['eager'] !== false) |
||
117 | { |
||
118 | $Query->eager(); |
||
119 | } |
||
120 | |||
121 | |||
122 | foreach($table->columns() as $column_name => $column) |
||
123 | { |
||
124 | if(isset($filters[$column_name]) && is_string($filters[$column_name])) |
||
125 | $Query->aw_eq($column_name, $filters[$column_name]); |
||
126 | } |
||
127 | |||
128 | if(is_subclass_of($event = new $class(), '\HexMakina\kadro\Models\Interfaces\EventInterface')) |
||
129 | { |
||
130 | if(!empty($filters['date_start'])) |
||
131 | $Query->aw_gte($event->event_field(), $filters['date_start'], $Query->table_label(), ':filter_date_start'); |
||
132 | |||
133 | if(!empty($filters['date_stop'])) |
||
134 | $Query->aw_lte($event->event_field(), $filters['date_stop'], $Query->table_label(), ':filter_date_stop'); |
||
135 | |||
136 | if(empty($options['order_by'])) |
||
137 | $Query->order_by([$event->event_field(), 'DESC']); |
||
138 | } |
||
139 | |||
140 | if(isset($filters['content'])) $Query->aw_filter_content($filters['content']); |
||
141 | |||
142 | if(isset($filters['ids'])) |
||
143 | { |
||
144 | if(empty($filters['ids'])) |
||
145 | $Query->and_where('1=0'); // TODO: this is a new low.. find another way to cancel query |
||
146 | else $Query->aw_numeric_in('id', $filters['ids'], $Query->table_label()); |
||
147 | } |
||
148 | |||
149 | if(isset($options['order_by'])) // TODO commenting required about the array situation |
||
150 | { |
||
151 | $order_by = $options['order_by']; |
||
152 | |||
153 | if(is_string($order_by)) |
||
154 | $Query->order_by($order_by); |
||
155 | |||
156 | elseif(is_array($order_by)) |
||
157 | foreach($options['order_by'] as $order_by) |
||
158 | { |
||
159 | if(!isset($order_by[2])) |
||
160 | array_unshift($order_by, ''); |
||
161 | |||
162 | list($order_table, $order_field, $order_direction) = $order_by; |
||
163 | $Query->order_by([$order_table ?? '', $order_field, $order_direction]); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | if(isset($options['limit']) && is_array($options['limit'])) |
||
168 | $Query->limit($options['limit'][1], $options['limit'][0]); |
||
169 | |||
170 | return $Query; |
||
171 | } |
||
172 | |||
173 | |||
174 | |||
175 | // success: return PK-indexed array of results (associative array or object) |
||
176 | public static function retrieve(SelectInterface $Query): array |
||
199 | } |
||
200 | |||
201 | /* USAGE |
||
202 | * one($primary_key_value) |
||
203 | * one($unique_column, $value) |
||
204 | */ |
||
205 | public static function one($arg1, $arg2 = null) |
||
223 | } |
||
224 | } |
||
225 | |||
226 | public static function exists($arg1, $arg2 = null) |
||
227 | { |
||
228 | try { |
||
229 | return self::one($arg1, $arg2); |
||
230 | } catch (CruditesException $e) { |
||
231 | return null; |
||
232 | } |
||
233 | } |
||
234 | |||
235 | |||
236 | public static function any($field_exact_values, $options = []) |
||
237 | { |
||
238 | $Query = static::query_retrieve([], $options)->aw_fields_eq($field_exact_values); |
||
239 | return static::retrieve($Query); |
||
240 | } |
||
241 | |||
242 | public static function filter($filters = [], $options = []): array |
||
245 | } |
||
246 | |||
247 | public static function listing($filters = [], $options = []): array |
||
248 | { |
||
250 | } |
||
251 | |||
252 | |||
253 | |||
254 | public static function get_many_by_AIPK($aipk_values) |
||
265 |