Total Complexity | 61 |
Total Lines | 480 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CheckoutWizard 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 CheckoutWizard, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class CheckoutWizard extends ZervWizard |
||
8 | { |
||
9 | /** |
||
10 | * CheckoutWizard constructor. |
||
11 | */ |
||
12 | public function __construct() |
||
13 | { |
||
14 | global $field; |
||
15 | // start the session and initialize the wizard |
||
16 | if (!isset($_SESSION)) { |
||
17 | session_start(); |
||
18 | } |
||
19 | parent::__construct($_SESSION, __CLASS__); |
||
20 | |||
21 | $this->addStep('fieldname', _MA_PEDIGREE_ENTER_FIELD); |
||
22 | if (0 == $this->getValue('field')) { //only for a new field |
||
|
|||
23 | $this->addStep('fieldtype', _MA_PEDIGREE_FIELD_TYP_SEL); |
||
24 | if (('selectbox' === $this->getValue('fieldtype')) || ('radiobutton' === $this->getValue('fieldtype'))) { |
||
25 | $this->addStep('lookup', _MA_PEDIGREE_FIELD_ADD_VALUE); |
||
26 | } |
||
27 | } |
||
28 | |||
29 | $this->addStep('Settings', _MA_PEDIGREE_FIELD_PARAM); |
||
30 | if ('hassearch' === $this->getValue('hassearch')) { |
||
31 | $this->addStep('search', _MA_PEDIGREE_SEARCH_PARAMFIELD); |
||
32 | } |
||
33 | if ('picture' !== $this->getValue('fieldtype')) { |
||
34 | $this->addStep('defaultvalue', _MA_PEDIGREE_FIELD_DEFAUT); |
||
35 | } |
||
36 | $this->addStep('confirm', _MA_PEDIGREE_FIELDCONFIRM); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @todo change access to fields using Pedigree\Fields |
||
41 | * @return void |
||
42 | */ |
||
43 | public function prepareFieldname() |
||
44 | { |
||
45 | global $field; |
||
46 | if (0 == !$field) { |
||
47 | // field already exists (editing mode) |
||
48 | |||
49 | $sql = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . ' WHERE id=' . $field; |
||
50 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
51 | while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { |
||
52 | $name = $row['fieldname']; |
||
53 | $fieldexplanation = $row['fieldexplanation']; |
||
54 | $fieldtype = $row['fieldtype']; |
||
55 | } |
||
56 | $this->setValue('name', $name); |
||
57 | $this->setValue('explain', $fieldexplanation); |
||
58 | //set the fieldtype because we wont allow it to be edited |
||
59 | $this->setValue('fieldtype', $fieldtype); |
||
60 | } |
||
61 | $this->setValue('field', $field); //is it a new field or are we editing a field |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param $form |
||
66 | * |
||
67 | * @return bool |
||
68 | */ |
||
69 | public function processFieldname(&$form) |
||
70 | { |
||
71 | $name = $this->coalesce($form['name']); |
||
72 | if (strlen($name) > 0) { |
||
73 | $this->setValue('name', $name); |
||
74 | } else { |
||
75 | $this->addError('name', _MA_PEDIGREE_FIELD_NAM); |
||
76 | } |
||
77 | |||
78 | $fieldexplanation = $this->coalesce($form['explain']); |
||
79 | if (strlen($fieldexplanation) > 0) { |
||
80 | $this->setValue('explain', $fieldexplanation); |
||
81 | } else { |
||
82 | $this->addError('explain', _MA_PEDIGREE_FIELD_EXPLAN1); |
||
83 | } |
||
84 | |||
85 | return !$this->isError(); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Setup this class' fieldtype array |
||
90 | * @return void |
||
91 | */ |
||
92 | public function prepareFieldtype() |
||
93 | { |
||
94 | $this->fieldtype[] = ['value' => 'radiobutton', 'description' => _MA_PEDIGREE_RADIOBUTTONFIELD]; |
||
95 | $this->fieldtype[] = ['value' => 'selectbox', 'description' => _MA_PEDIGREE_DROPDOWNFIELD]; |
||
96 | $this->fieldtype[] = ['value' => 'textbox', 'description' => _MA_PEDIGREE_TEXTBOXFIELD]; |
||
97 | $this->fieldtype[] = ['value' => 'textarea', 'description' => _MA_PEDIGREE_TEXTAREAFIELD]; |
||
98 | $this->fieldtype[] = ['value' => 'DateSelect', 'description' => _MA_PEDIGREE_DATEFIELD]; |
||
99 | $this->fieldtype[] = ['value' => 'urlfield', 'description' => _MA_PEDIGREE_URLFIELD]; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param $form |
||
104 | * |
||
105 | * @return bool |
||
106 | */ |
||
107 | public function processFieldtype($form) |
||
108 | { |
||
109 | $this->prepareFieldtype(); |
||
110 | $fieldtype = $this->coalesce($form['fieldtype']); |
||
111 | $this->setValue('fieldtype', $fieldtype); |
||
112 | |||
113 | return !$this->isError(); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param $form |
||
118 | * |
||
119 | * @return bool |
||
120 | */ |
||
121 | public function processLookup($form) |
||
122 | { |
||
123 | $fc = $this->coalesce($form['fc']); |
||
124 | $this->setValue('fc', $fc); |
||
125 | $lookup = $this->coalesce($form['lookup' . $fc]); |
||
126 | $lookupid = $this->coalesce($form['id' . $fc]); |
||
127 | if (strlen($lookup) > 0) { |
||
128 | $this->setValue('lookup' . $fc, $lookup); |
||
129 | $this->setValue('id' . $fc, $lookupid); |
||
130 | } |
||
131 | $lastlookup = $this->getValue('lookup' . $fc); |
||
132 | if ('' == $lastlookup) { |
||
133 | $this->setValue('fc', $fc - 1); |
||
134 | } |
||
135 | |||
136 | for ($i = 0; $i < $fc; ++$i) { |
||
137 | $radioarray[] = [ |
||
138 | 'id' => $this->getValue('id' . ($i + 1)), |
||
139 | 'value' => $this->getValue('lookup' . ($i + 1)) |
||
140 | ]; |
||
141 | } |
||
142 | //print_r($radioarray); die(); |
||
143 | $this->setValue('radioarray', $radioarray); |
||
144 | |||
145 | return !$this->isError(); |
||
146 | // |
||
147 | } |
||
148 | |||
149 | public function prepareSettings() |
||
150 | { |
||
151 | if (0 == !$this->getValue('field')) { |
||
152 | // field allready exists (editing mode) |
||
153 | |||
154 | { |
||
155 | $sql = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . " WHERE id='" . $this->getValue('field') . "'"; |
||
156 | } |
||
157 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
158 | while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { |
||
159 | $hs = $row['hassearch']; |
||
160 | if ('1' == $hs) { |
||
161 | $this->setValue('hassearch', 'hassearch'); |
||
162 | } |
||
163 | $vip = $row['viewinpedigree']; |
||
164 | if ('1' == $vip) { |
||
165 | $this->setValue('viewinpedigree', 'viewinpedigree'); |
||
166 | } |
||
167 | $via = $row['viewinadvanced']; |
||
168 | if ('1' == $via) { |
||
169 | $this->setValue('viewinadvanced', 'viewinadvanced'); |
||
170 | } |
||
171 | $vipie = $row['viewinpie']; |
||
172 | if ('1' == $vipie) { |
||
173 | $this->setValue('viewinpie', 'viewinpie'); |
||
174 | } |
||
175 | $vil = $row['viewinlist']; |
||
176 | if ('1' == $vil) { |
||
177 | $this->setValue('viewinlist', 'viewinlist'); |
||
178 | } |
||
179 | $lit = $row['litter']; |
||
180 | if ('1' == $lit) { |
||
181 | $this->setValue('litter', 'litter'); |
||
182 | } |
||
183 | $glit = $row['generallitter']; |
||
184 | if ('1' == $glit) { |
||
185 | $this->setValue('generallitter', 'generallitter'); |
||
186 | } |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param $form |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | public function processSettings($form) |
||
197 | { |
||
198 | $this->setValue('hassearch', $this->coalesce($form['hasSearch'])); |
||
199 | $this->setValue('viewinpedigree', $this->coalesce($form['viewinpedigree'])); |
||
200 | $this->setValue('viewinadvanced', $this->coalesce($form['viewinadvanced'])); |
||
201 | $this->setValue('viewinpie', $this->coalesce($form['viewinpie'])); |
||
202 | $this->setValue('viewinlist', $this->coalesce($form['viewinlist'])); |
||
203 | $this->setValue('litter', $this->coalesce($form['litter'])); |
||
204 | $this->setValue('generallitter', $this->coalesce($form['generallitter'])); |
||
205 | |||
206 | //if both litter and general litter are set; unset generallitter |
||
207 | if (('litter' === $this->getValue('litter')) && ('generallitter' === $this->getValue('generallitter'))) { |
||
208 | $this->setValue('generallitter', 0); |
||
209 | } |
||
210 | |||
211 | return !$this->isError(); |
||
212 | } |
||
213 | |||
214 | public function prepareSearch() |
||
215 | { |
||
216 | if (0 == !$this->getValue('field')) { |
||
217 | // field allready exists (editing mode) |
||
218 | |||
219 | $sql = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . ' WHERE id=' . $this->getValue('field'); |
||
220 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
221 | while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { |
||
222 | if ('hasearch' === $this->getValue('hassearch')) { |
||
223 | $searchname = $row['searchname']; |
||
224 | $this->setValue('searchname', $searchname); |
||
225 | $searchexplain = $row['searchexplanation']; |
||
226 | $this->setValue('searchexplain', $searchexplain); |
||
227 | } |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @param $form |
||
234 | * |
||
235 | * @return bool |
||
236 | * @todo move language strings to language files |
||
237 | */ |
||
238 | public function processSearch($form) |
||
239 | { |
||
240 | $searchname = $this->coalesce($form['searchname']); |
||
241 | if (strlen($searchname) > 0) { |
||
242 | $this->setValue('searchname', $searchname); |
||
243 | } else { |
||
244 | $this->addError('searchname', 'Please enter the searchname'); |
||
245 | } |
||
246 | |||
247 | $fieldexplanation = $this->coalesce($form['searchexplain']); |
||
248 | if (strlen($fieldexplanation) > 0) { |
||
249 | $this->setValue('searchexplain', $fieldexplanation); |
||
250 | } else { |
||
251 | $this->addError('searchexplain', 'Please enter the search explanation for this field'); |
||
252 | } |
||
253 | |||
254 | return !$this->isError(); |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @return void |
||
259 | */ |
||
260 | public function prepareDefaultvalue() |
||
280 | } |
||
281 | } |
||
282 | } |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @param $form |
||
287 | * |
||
288 | * @return bool |
||
289 | * @todo move language string to language file |
||
290 | */ |
||
291 | public function processDefaultValue($form) |
||
292 | { |
||
293 | $defaultvalue = $this->coalesce($form['defaultvalue']); |
||
294 | if (strlen($defaultvalue) >= 0) { |
||
295 | $this->setValue('defaultvalue', $defaultvalue); |
||
296 | } else { |
||
297 | $this->addError('defaultvalue', 'Please enter a defaultvalue'); |
||
298 | } |
||
299 | |||
300 | return !$this->isError(); |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @param $form |
||
305 | * |
||
306 | * @return bool |
||
307 | */ |
||
308 | public function processConfirm($form) |
||
309 | { |
||
310 | return !$this->isError(); |
||
311 | } |
||
312 | |||
313 | public function completeCallback() |
||
473 | } |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * Miscellaneous utility functions |
||
478 | * |
||
479 | * @param $email |
||
480 | * |
||
481 | * @return int |
||
482 | */ |
||
483 | |||
484 | public function isValidEmail($email) |
||
487 | } |
||
488 | } |
||
489 |