Total Complexity | 65 |
Total Lines | 512 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Products 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 Products, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class Products extends OledrionObject |
||
33 | { |
||
34 | /** |
||
35 | * constructor |
||
36 | * |
||
37 | * normally, this is called from child classes only |
||
38 | */ |
||
39 | public function __construct() |
||
40 | { |
||
41 | $this->initVar('product_id', XOBJ_DTYPE_INT, null, false); |
||
42 | $this->initVar('product_cid', XOBJ_DTYPE_INT, null, false); |
||
43 | $this->initVar('product_title', XOBJ_DTYPE_TXTBOX, null, false); |
||
44 | $this->initVar('product_vendor_id', XOBJ_DTYPE_INT, null, false); |
||
45 | $this->initVar('product_sku', XOBJ_DTYPE_TXTBOX, null, false); |
||
46 | $this->initVar('product_extraid', XOBJ_DTYPE_TXTBOX, null, false); |
||
47 | $this->initVar('product_width', XOBJ_DTYPE_TXTBOX, null, false); |
||
48 | $this->initVar('product_length', XOBJ_DTYPE_TXTBOX, null, false); |
||
49 | $this->initVar('product_unitmeasure1', XOBJ_DTYPE_TXTBOX, null, false); |
||
50 | $this->initVar('product_url', XOBJ_DTYPE_TXTBOX, null, false); |
||
51 | $this->initVar('product_url2', XOBJ_DTYPE_TXTBOX, null, false); |
||
52 | $this->initVar('product_url3', XOBJ_DTYPE_TXTBOX, null, false); |
||
53 | $this->initVar('product_image_url', XOBJ_DTYPE_TXTBOX, null, false); |
||
54 | $this->initVar('product_thumb_url', XOBJ_DTYPE_TXTBOX, null, false); |
||
55 | $this->initVar('product_submitter', XOBJ_DTYPE_INT, null, false); |
||
56 | $this->initVar('product_online', XOBJ_DTYPE_INT, null, false); |
||
57 | // B.R. Start |
||
58 | $this->initVar('skip_packing', XOBJ_DTYPE_INT, null, false); |
||
59 | $this->initVar('skip_location', XOBJ_DTYPE_INT, null, false); |
||
60 | $this->initVar('skip_delivery', XOBJ_DTYPE_INT, null, false); |
||
61 | // B.R. End |
||
62 | $this->initVar('product_date', XOBJ_DTYPE_TXTBOX, null, false); |
||
63 | $this->initVar('product_submitted', XOBJ_DTYPE_INT, null, false); |
||
64 | $this->initVar('product_hits', XOBJ_DTYPE_INT, null, false); |
||
65 | $this->initVar('product_rating', XOBJ_DTYPE_INT, null, false); |
||
66 | $this->initVar('product_votes', XOBJ_DTYPE_INT, null, false); |
||
67 | $this->initVar('product_comments', XOBJ_DTYPE_INT, null, false); |
||
68 | $this->initVar('product_price', XOBJ_DTYPE_TXTBOX, 0, false); |
||
69 | $this->initVar('product_shipping_price', XOBJ_DTYPE_TXTBOX, 0, false); |
||
70 | $this->initVar('product_discount_price', XOBJ_DTYPE_TXTBOX, 0, false); |
||
71 | $this->initVar('product_stock', XOBJ_DTYPE_INT, null, false); |
||
72 | $this->initVar('product_alert_stock', XOBJ_DTYPE_INT, null, false); |
||
73 | $this->initVar('product_summary', XOBJ_DTYPE_OTHER, null, false); |
||
74 | $this->initVar('product_description', XOBJ_DTYPE_OTHER, null, false); |
||
75 | $this->initVar('product_attachment', XOBJ_DTYPE_TXTBOX, null, false); |
||
76 | $this->initVar('product_weight', XOBJ_DTYPE_TXTBOX, null, false); |
||
77 | $this->initVar('product_unitmeasure2', XOBJ_DTYPE_TXTBOX, null, false); |
||
78 | $this->initVar('product_vat_id', XOBJ_DTYPE_INT, null, false); |
||
79 | $this->initVar('product_download_url', XOBJ_DTYPE_TXTBOX, null, false); |
||
80 | $this->initVar('product_recommended', XOBJ_DTYPE_TXTBOX, null, false); |
||
81 | $this->initVar('product_metakeywords', XOBJ_DTYPE_TXTBOX, null, false); |
||
82 | $this->initVar('product_metadescription', XOBJ_DTYPE_TXTBOX, null, false); |
||
83 | $this->initVar('product_metatitle', XOBJ_DTYPE_TXTBOX, null, false); |
||
84 | $this->initVar('product_delivery_time', XOBJ_DTYPE_INT, null, false); |
||
85 | $this->initVar('product_ecotaxe', XOBJ_DTYPE_TXTBOX, 0, false); |
||
86 | $this->initVar('product_property1', XOBJ_DTYPE_TXTBOX, null, false); |
||
87 | $this->initVar('product_property2', XOBJ_DTYPE_TXTBOX, null, false); |
||
88 | $this->initVar('product_property3', XOBJ_DTYPE_TXTBOX, null, false); |
||
89 | $this->initVar('product_property4', XOBJ_DTYPE_TXTBOX, null, false); |
||
90 | $this->initVar('product_property5', XOBJ_DTYPE_TXTBOX, null, false); |
||
91 | $this->initVar('product_property6', XOBJ_DTYPE_TXTBOX, null, false); |
||
92 | $this->initVar('product_property7', XOBJ_DTYPE_TXTBOX, null, false); |
||
93 | $this->initVar('product_property8', XOBJ_DTYPE_TXTBOX, null, false); |
||
94 | $this->initVar('product_property9', XOBJ_DTYPE_TXTBOX, null, false); |
||
95 | $this->initVar('product_property10', XOBJ_DTYPE_TXTBOX, null, false); |
||
96 | |||
97 | // Pour autoriser le html |
||
98 | $this->initVar('dohtml', XOBJ_DTYPE_INT, 1, false); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Indique si le produit courant est visible (périmé, encore en stock, en ligne etc) |
||
103 | * |
||
104 | * @return bool |
||
105 | * @since 2.3.2009.03.17 |
||
106 | */ |
||
107 | public function isProductVisible() |
||
108 | { |
||
109 | $isAdmin = Oledrion\Utility::isAdmin(); |
||
110 | if (0 == $this->getVar('product_online')) { |
||
111 | if (!$isAdmin) { |
||
112 | return false; |
||
113 | } |
||
114 | } |
||
115 | if (0 == Oledrion\Utility::getModuleOption('show_unpublished') && $this->getVar('product_submitted') > time()) { |
||
116 | if (!$isAdmin) { |
||
117 | return false; |
||
118 | } |
||
119 | } |
||
120 | if (0 == Oledrion\Utility::getModuleOption('nostock_display') && 0 == $this->getVar('product_stock')) { |
||
121 | if (!$isAdmin) { |
||
122 | return false; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | return true; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Retourne l'URL de l'image du produit courant |
||
131 | * |
||
132 | * @return string L'URL |
||
133 | */ |
||
134 | public function getPictureUrl() |
||
135 | { |
||
136 | if ('' !== xoops_trim($this->getVar('product_image_url'))) { |
||
137 | return OLEDRION_PICTURES_URL . '/' . $this->getVar('product_image_url'); |
||
138 | } |
||
139 | |||
140 | return ''; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Retourne le chemin de l'image du produit courant |
||
145 | * |
||
146 | * @return string Le chemin |
||
147 | */ |
||
148 | public function getPicturePath() |
||
149 | { |
||
150 | if ('' !== xoops_trim($this->getVar('product_image_url'))) { |
||
151 | return OLEDRION_PICTURES_PATH . '/' . $this->getVar('product_image_url'); |
||
152 | } |
||
153 | |||
154 | return ''; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Retourne l'URL de la vignette du produit courant |
||
159 | * |
||
160 | * @return string L'URL |
||
161 | */ |
||
162 | public function getThumbUrl() |
||
163 | { |
||
164 | if ('' !== xoops_trim($this->getVar('product_thumb_url'))) { |
||
165 | return OLEDRION_PICTURES_URL . '/' . $this->getVar('product_thumb_url'); |
||
166 | } |
||
167 | |||
168 | return ''; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Retourne l'URL de la vignette du produit courant |
||
173 | * |
||
174 | * @return string L'URL |
||
175 | */ |
||
176 | public function getThumbPath() |
||
177 | { |
||
178 | if ('' !== xoops_trim($this->getVar('product_thumb_url'))) { |
||
179 | return OLEDRION_PICTURES_PATH . '/' . $this->getVar('product_thumb_url'); |
||
180 | } |
||
181 | |||
182 | return ''; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Indique si l'image du produit existe |
||
187 | * |
||
188 | * @return bool Vrai si l'image existe sinon faux |
||
189 | */ |
||
190 | public function pictureExists() |
||
191 | { |
||
192 | $return = false; |
||
193 | if ('' !== xoops_trim($this->getVar('product_image_url')) && file_exists(OLEDRION_PICTURES_PATH . '/' . $this->getVar('product_image_url'))) { |
||
194 | $return = true; |
||
195 | } |
||
196 | |||
197 | return $return; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Indique si la vignette du produit existe |
||
202 | * |
||
203 | * @return bool Vrai si l'image existe sinon faux |
||
204 | */ |
||
205 | public function thumbExists() |
||
206 | { |
||
207 | $return = false; |
||
208 | if ('' !== xoops_trim($this->getVar('product_thumb_url')) && file_exists(OLEDRION_PICTURES_PATH . '/' . $this->getVar('product_thumb_url'))) { |
||
209 | $return = true; |
||
210 | } |
||
211 | |||
212 | return $return; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Supprime l'image associée à un produit |
||
217 | */ |
||
218 | public function deletePicture() |
||
219 | { |
||
220 | if ($this->pictureExists()) { |
||
221 | if (false === @unlink(OLEDRION_PICTURES_PATH . '/' . $this->getVar('product_image_url'))) { |
||
222 | throw new \RuntimeException('The picture ' . OLEDRION_PICTURES_PATH . '/' . $this->getVar('product_image_url') . ' could not be deleted.'); |
||
223 | } |
||
224 | } |
||
225 | $this->setVar('product_image_url', ''); |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Indique si le fichier attaché à un produit existe |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | public function attachmentExists() |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Supprime le fichier attaché |
||
245 | */ |
||
246 | public function deleteAttachment() |
||
247 | { |
||
248 | if ($this->attachmentExists()) { |
||
249 | if (false === @unlink(OLEDRION_ATTACHED_FILES_PATH . '/' . $this->getVar('product_attachment'))) { |
||
250 | throw new \RuntimeException('The file ' . OLEDRION_ATTACHED_FILES_PATH . '/' . $this->getVar('product_attachment') . ' could not be deleted.'); |
||
251 | } |
||
252 | } |
||
253 | $this->setVar('product_attachment', ''); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Supprime la miniature associée à un produit |
||
258 | */ |
||
259 | public function deleteThumb() |
||
260 | { |
||
261 | if ($this->thumbExists()) { |
||
262 | if (false === @unlink(OLEDRION_PICTURES_PATH . '/' . $this->getVar('product_thumb_url'))) { |
||
263 | throw new \RuntimeException('The thumb ' . OLEDRION_PICTURES_PATH . '/' . $this->getVar('product_thumb_url') . ' could not be deleted.'); |
||
264 | } |
||
265 | } |
||
266 | $this->setVar('product_thumb_url', ''); |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Supprime les 2 images (raccourcis) |
||
271 | */ |
||
272 | public function deletePictures() |
||
273 | { |
||
274 | $this->deletePicture(); |
||
275 | $this->deleteThumb(); |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Retourne le prix TTC du prix réduit du produit courant |
||
280 | * |
||
281 | * @return float Le montant TTC du prix réduit |
||
282 | */ |
||
283 | public function getDiscountTTC() |
||
284 | { |
||
285 | return Oledrion\Utility::getAmountWithVat($this->getVar('product_discount_price', 'e'), $this->getVar('product_vat_id')); |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Retourne le montant TTC du prix normal du produit |
||
290 | * |
||
291 | * @return float |
||
292 | */ |
||
293 | public function getTTC() |
||
294 | { |
||
295 | return Oledrion\Utility::getAmountWithVat($this->getVar('product_price', 'e'), $this->getVar('product_vat_id')); |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Indique si le produit courant est recommandé. |
||
300 | * |
||
301 | * @param bool $withDescription |
||
302 | * @return bool Vrai si le produit est recommandé sinon faux |
||
303 | */ |
||
304 | public function isRecommended($withDescription = false) |
||
305 | { |
||
306 | if ('0000-00-00' != $this->getVar('product_recommended')) { |
||
307 | return $withDescription ? _YES : true; |
||
|
|||
308 | } |
||
309 | |||
310 | return $withDescription ? _NO : false; |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Place le produit courant dans l'état "recommandé" |
||
315 | */ |
||
316 | public function setRecommended() |
||
317 | { |
||
318 | $this->setVar('product_recommended', date('Y-m-d')); |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Enlève "l'attribut" recommandé d'un produit |
||
323 | */ |
||
324 | public function unsetRecommended() |
||
325 | { |
||
326 | $this->setVar('product_recommended', '0000-00-00'); |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Retourne l'image qui indique si le produit est recommandé ou pas |
||
331 | * |
||
332 | * @return string La chaine à utiliser pour voir l'image |
||
333 | */ |
||
334 | public function recommendedPicture() |
||
335 | { |
||
336 | if ($this->isRecommended()) { |
||
337 | return '<img src="' . OLEDRION_IMAGES_URL . 'heart.png" alt="' . _OLEDRION_IS_RECOMMENDED . '"> '; |
||
338 | } |
||
339 | |||
340 | return '<img src="' . OLEDRION_IMAGES_URL . 'blank.gif" alt="">'; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Retourne le lien du produit courant en tenant compte de l'URL Rewriting |
||
345 | * |
||
346 | * @param int $product_id L'identifiant du produit |
||
347 | * @param string $product_title Le titre du produit |
||
348 | * @param bool $shortVersion Indique si on veut la version avec l'url complpète ou la version avec juste la page et le paramètre |
||
349 | * @return string |
||
350 | */ |
||
351 | public function getLink($product_id = 0, $product_title = '', $shortVersion = false) |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Retourne le nombre d'attributs du produit courant |
||
389 | * |
||
390 | * @return int |
||
391 | * @since 2.3.2009.03.19 |
||
392 | */ |
||
393 | public function productAttributesCount() |
||
394 | { |
||
395 | $db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
396 | $attributesHandler = new Oledrion\AttributesHandler($db); |
||
397 | |||
398 | return $attributesHandler->getProductAttributesCount($this->getVar('product_id')); |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Retourne le nombre d'attributs obligatoires d'un produit |
||
403 | * |
||
404 | * @note : La fonction est "doublée", elle se trouve içi et dans la classe des attributs pour des raisons de facilité (et de logique) |
||
405 | * |
||
406 | * @return int |
||
407 | * @since 2.3.2009.03.20 |
||
408 | */ |
||
409 | public function getProductMandatoryAttributesCount() |
||
415 | } |
||
416 | |||
417 | /** |
||
418 | * Retourne la liste des attributs obligatoires du produit |
||
419 | * |
||
420 | * @return array |
||
421 | * @since 2.3.2009.03.20 |
||
422 | */ |
||
423 | public function getProductMandatoryFieldsList() |
||
424 | { |
||
425 | $db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
426 | $attributesHandler = new Oledrion\AttributesHandler($db); |
||
427 | |||
428 | return $attributesHandler->getProductMandatoryFieldsList($this); |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * Retourne la liste des attributs du produit courant |
||
433 | * |
||
434 | * @param null $attributesIds |
||
435 | * @return array Objets de type Oledrion_attributes |
||
436 | * @since 2.3.2009.03.20 |
||
437 | */ |
||
438 | public function getProductsAttributesList($attributesIds = null) |
||
439 | { |
||
440 | $db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
441 | $attributesHandler = new Oledrion\AttributesHandler($db); |
||
442 | |||
443 | return $attributesHandler->getProductsAttributesList($this->getVar('product_id'), $attributesIds); |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * Retourne le montant HT initial des options |
||
448 | * |
||
449 | * @return float |
||
450 | */ |
||
451 | public function getInitialOptionsPrice() |
||
452 | { |
||
453 | $db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
454 | $attributesHandler = new Oledrion\AttributesHandler($db); |
||
455 | |||
456 | return $attributesHandler->getInitialOptionsPrice($this); |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * @return int |
||
461 | */ |
||
462 | public function isNewProduct() |
||
463 | { |
||
464 | $time = time() - (60 * 60 * 24 * 10); |
||
465 | if ($this->getVar('product_submitted') > $time) { |
||
466 | return 1; |
||
467 | } |
||
468 | |||
469 | return 0; |
||
470 | } |
||
471 | |||
472 | /** |
||
473 | * Retourne les éléments du produits formatés pour affichage |
||
474 | * |
||
475 | * @param string $format Le format à utiliser |
||
476 | * @return array Les informations formatées |
||
477 | */ |
||
478 | public function toArray($format = 's') |
||
544 | } |
||
545 | } |
||
546 |