Complex classes like Sitewards_B2BProfessional_Model_Observer 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Sitewards_B2BProfessional_Model_Observer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Sitewards_B2BProfessional_Model_Observer |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * The last product Id |
||
| 19 | * |
||
| 20 | * @var int |
||
| 21 | */ |
||
| 22 | protected static $iLastProductId = 0; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The b2b prof helper class |
||
| 26 | * |
||
| 27 | * @var Sitewards_B2BProfessional_Helper_Data |
||
| 28 | */ |
||
| 29 | protected $oB2BHelper; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Init the helper object |
||
| 33 | */ |
||
| 34 | public function __construct() |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Check if the extension is active |
||
| 41 | * |
||
| 42 | * @return bool |
||
| 43 | */ |
||
| 44 | protected function isExtensionActive() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Check to see if the product being added to the cart can be bought |
||
| 51 | * |
||
| 52 | * @param Varien_Event_Observer $oObserver |
||
| 53 | * @throws Mage_Catalog_Exception |
||
| 54 | */ |
||
| 55 | public function catalogProductTypePrepareFullOptions(Varien_Event_Observer $oObserver) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Replace the price information with the desired message |
||
| 68 | * |
||
| 69 | * @param Varien_Event_Observer $oObserver |
||
| 70 | */ |
||
| 71 | public function coreBlockAbstractToHtmlAfter(Varien_Event_Observer $oObserver) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Check to see if the user will need to be redirected to the login page or another saved via the admin |
||
| 95 | * |
||
| 96 | * @param Varien_Event_Observer $oObserver |
||
| 97 | */ |
||
| 98 | public function controllerActionPredispatch(Varien_Event_Observer $oObserver) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Remove the price option from the order by options |
||
| 119 | * |
||
| 120 | * @param Varien_Event_Observer $oObserver |
||
| 121 | */ |
||
| 122 | public function coreBlockAbstractToHtmlBefore(Varien_Event_Observer $oObserver) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * If we have a Mage_Catalog_Block_Layer_View |
||
| 135 | * - remove the price attribute |
||
| 136 | * |
||
| 137 | * @param Varien_Event_Observer $oObserver |
||
| 138 | */ |
||
| 139 | public function coreLayoutBlockCreateAfter(Varien_Event_Observer $oObserver) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Checks if the product can be added to the cart and if not, adds a dummy required |
||
| 155 | * option in order to replace the add-to-cart button's url with the view-details url |
||
| 156 | * |
||
| 157 | * @param Varien_Event_Observer $oObserver |
||
| 158 | */ |
||
| 159 | public function catalogBlockProductListCollectionLoadAfter(Varien_Event_Observer $oObserver) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * checks if the block represents a price block |
||
| 173 | * |
||
| 174 | * @param Mage_Core_Block_Abstract $oBlock |
||
| 175 | * @return bool |
||
| 176 | */ |
||
| 177 | protected function isExactlyPriceBlock($oBlock) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * checks if the block represents an add-to-cart block |
||
| 184 | * |
||
| 185 | * @param Mage_Core_Block_Abstract $oBlock |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | protected function isExactlyAddToCartBlock($oBlock) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * From a block get all the category filters when set |
||
| 195 | * |
||
| 196 | * @param Mage_Catalog_Block_Layer_View $oBlock |
||
| 197 | * @return int[] |
||
| 198 | */ |
||
| 199 | protected function getCategoryFilters($oBlock) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Return the default category, |
||
| 221 | * - either from the filter, |
||
| 222 | * - the current category, |
||
| 223 | * - or the root category |
||
| 224 | * |
||
| 225 | * @return int[] |
||
| 226 | */ |
||
| 227 | protected function getDefaultCategoryOptions() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Remove the price filter options from the filterable attributes |
||
| 247 | * |
||
| 248 | * @param Mage_Catalog_Block_Layer_View $oBlock |
||
| 249 | */ |
||
| 250 | protected function removePriceFilter($oBlock) |
||
| 251 | { |
||
| 252 | $aFilterableAttributes = $oBlock->getData('_filterable_attributes'); |
||
| 253 | $aNewFilterableAttributes = array(); |
||
| 254 | |||
| 255 | if (is_array($aFilterableAttributes) || is_object($aFilterableAttributes)) { |
||
| 256 | foreach ($aFilterableAttributes as $oFilterableAttribute) { |
||
| 257 | if ($oFilterableAttribute->getAttributeCode() != 'price') { |
||
| 258 | $aNewFilterableAttributes[] = $oFilterableAttribute; |
||
| 259 | } |
||
| 260 | } |
||
| 261 | } |
||
| 262 | $oBlock->setData('_filterable_attributes', $aNewFilterableAttributes); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Set type id to combined to stop tax being displayed via Symmetrics_TweaksGerman_Block_Tax |
||
| 267 | * |
||
| 268 | * @param Mage_Catalog_Model_Product $oProduct |
||
| 269 | */ |
||
| 270 | protected function setSymmetricsProductType(Mage_Catalog_Model_Product $oProduct) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * For a given product price block check if the product is active |
||
| 282 | * - if it is active then set the price html as the default message |
||
| 283 | * |
||
| 284 | * @param Mage_Catalog_Block_Product_Price $oBlock |
||
| 285 | * @param Varien_Object $oTransport |
||
| 286 | */ |
||
| 287 | protected function transformPriceBlock($oBlock, $oTransport) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Get the product attached to an event |
||
| 306 | * |
||
| 307 | * @param Varien_Event_Observer $oObserver |
||
| 308 | * @return Mage_Catalog_Model_Product |
||
| 309 | */ |
||
| 310 | protected function getEventsProduct(Varien_Event_Observer $oObserver) |
||
| 314 | } |
||
| 315 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.