Total Complexity | 40 |
Total Lines | 276 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Params 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 Params, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Params extends ArrayCollection |
||
11 | { |
||
12 | protected $is_multipart = false; |
||
13 | |||
14 | /** |
||
15 | * Get instance. |
||
16 | * |
||
17 | * @param $params |
||
18 | * |
||
19 | * @return Params |
||
20 | */ |
||
21 | public static function make($params = []) |
||
22 | { |
||
23 | if ($params instanceof static) { |
||
24 | return $params; |
||
25 | } |
||
26 | |||
27 | return new static($params); |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * Add an "order_by" param to the request. |
||
32 | * |
||
33 | * @param $field |
||
34 | * @param string $direction |
||
35 | * |
||
36 | * @return $this |
||
37 | */ |
||
38 | public function orderBy($field, $direction = 'desc') |
||
39 | { |
||
40 | $key = 'order_by'; |
||
41 | |||
42 | $orderBy = $this->get($key) ?: []; |
||
43 | |||
44 | $direction = $direction === 'desc' ? 'DESCENDING' : 'ASCENDING'; |
||
45 | |||
46 | $orderBy[] = [ |
||
47 | 'sort_field' => $field, |
||
48 | 'sort_type' => $direction, |
||
49 | ]; |
||
50 | |||
51 | return $this->set($key, $orderBy); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Add an "group_by" param to the request. |
||
56 | * |
||
57 | * @return $this |
||
58 | */ |
||
59 | public function groupBy() |
||
60 | { |
||
61 | $key = 'group_by'; |
||
62 | |||
63 | $args = func_get_args(); |
||
64 | |||
65 | $argNum = count($args); |
||
66 | |||
67 | if ($argNum === 1) { |
||
68 | $this->set($key, is_array($args[0]) ? $args[0] : $args); |
||
69 | } elseif ($argNum > 1) { |
||
70 | $this->set($key, $args); |
||
71 | } |
||
72 | |||
73 | return $this; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Set a param. |
||
78 | * |
||
79 | * @param string $key |
||
80 | * @param mixed $value |
||
81 | * |
||
82 | * @return $this|void |
||
83 | */ |
||
84 | public function set($key, $value) |
||
85 | { |
||
86 | parent::set($key, $value); |
||
87 | |||
88 | return $this; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Set a param and set format to multipart. |
||
93 | * |
||
94 | * @param $key |
||
95 | * @param $value |
||
96 | * |
||
97 | * @return Params |
||
98 | */ |
||
99 | public function multipart($key, $value) |
||
100 | { |
||
101 | $this->is_multipart = true; |
||
102 | |||
103 | return $this->set($key, $value); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Set a file to params. |
||
108 | * |
||
109 | * @param $field |
||
110 | * @param $file |
||
111 | * @param $filename |
||
112 | * |
||
113 | * @return $this |
||
114 | */ |
||
115 | public function file($field, $file, $filename = '') |
||
116 | { |
||
117 | if (!$file) { |
||
118 | return $this; |
||
119 | } |
||
120 | |||
121 | if (is_string($file)) { |
||
122 | if (!$filename) { |
||
123 | $filename = basename($filename); |
||
124 | } |
||
125 | |||
126 | $file = fopen($file, 'r'); |
||
127 | } |
||
128 | |||
129 | if (is_resource($file)) { |
||
130 | $this->multipart($field, [ |
||
131 | 'name' => $field, |
||
132 | 'contents' => $file, |
||
133 | 'filename' => $filename, |
||
134 | ]); |
||
135 | } |
||
136 | |||
137 | return $this; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Add an "date_range" param to the request. |
||
142 | * |
||
143 | * @return $this|void |
||
144 | */ |
||
145 | public function setDateRange() |
||
146 | { |
||
147 | $key = 'date_range'; |
||
148 | |||
149 | $args = func_get_args(); |
||
150 | |||
151 | $argNum = count($args); |
||
152 | |||
153 | if ($argNum === 1 && $args[0] instanceof DateRange) { |
||
154 | return $this->set($key, $args[0]); |
||
155 | } |
||
156 | |||
157 | if ($argNum >= 2) { |
||
158 | return $this->set($key, new DateRange($args[0], $args[1])); |
||
159 | } |
||
160 | |||
161 | throw new \InvalidArgumentException('Date range cannot be empty.'); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Add an "time_range" param to the request. |
||
166 | * |
||
167 | * @return $this|void |
||
168 | */ |
||
169 | public function setTimeRange() |
||
170 | { |
||
171 | $key = 'time_range'; |
||
172 | |||
173 | $args = func_get_args(); |
||
174 | |||
175 | $argNum = count($args); |
||
176 | |||
177 | if ($argNum === 1 && $args[0] instanceof TimeRange) { |
||
178 | return $this->set($key, $args[0]); |
||
179 | } |
||
180 | |||
181 | if ($argNum >= 2) { |
||
182 | return $this->set($key, new TimeRange($args[0], $args[1])); |
||
183 | } |
||
184 | |||
185 | throw new \InvalidArgumentException('Time range cannot be empty.'); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Get the filter of builder. |
||
190 | * |
||
191 | * @return Filter|mixed |
||
192 | */ |
||
193 | public function getFilter() |
||
194 | { |
||
195 | $key = 'filtering'; |
||
196 | |||
197 | $filter = $this->get($key); |
||
198 | |||
199 | if (!$filter) { |
||
200 | $filter = new Filter(); |
||
201 | $this->set($key, $filter); |
||
202 | } |
||
203 | |||
204 | return $filter; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Set a filter param to the request. |
||
209 | * |
||
210 | * @param $filter |
||
211 | * |
||
212 | * @return $this|void |
||
213 | */ |
||
214 | public function setFilter($filter) |
||
215 | { |
||
216 | return $this->set('filtering', $filter); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Get the instance as a multipart. |
||
221 | * |
||
222 | * @return array |
||
223 | */ |
||
224 | protected function toMultipart() |
||
225 | { |
||
226 | $results = []; |
||
227 | foreach (parent::toArray() as $field => $value) { |
||
228 | if (is_object($value) && method_exists($value, 'toArray')) { |
||
229 | $results[] = [ |
||
230 | 'name' => $field, |
||
231 | 'contents' => $value->toArray(), |
||
232 | ]; |
||
233 | } else { |
||
234 | if (is_array($value) && isset($value['name']) && isset($value['contents'])) { |
||
235 | $results[] = $value; |
||
236 | } else { |
||
237 | $results[] = [ |
||
238 | 'name' => $field, |
||
239 | 'contents' => $value, |
||
240 | ]; |
||
241 | } |
||
242 | } |
||
243 | } |
||
244 | |||
245 | return $results; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Get the instance as an array. |
||
250 | * |
||
251 | * @return array |
||
252 | */ |
||
253 | protected function __toArray() |
||
254 | { |
||
255 | return array_map(function ($value) { |
||
256 | if (is_object($value) && method_exists($value, 'toArray')) { |
||
257 | return $value->toArray(); |
||
258 | } else { |
||
259 | return $value; |
||
260 | } |
||
261 | }, parent::toArray()); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Get the instance as an array according to format. |
||
266 | * |
||
267 | * @return array |
||
268 | */ |
||
269 | public function toArray() |
||
275 | } |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Determine if it is multipart. |
||
280 | * |
||
281 | * @return bool |
||
282 | */ |
||
283 | public function isMultipart() |
||
286 | } |
||
287 | } |
||
288 |