Complex classes like AbstractObserver 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 AbstractObserver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | abstract class AbstractObserver implements ObserverInterface |
||
| 38 | { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The actual row, that has to be processed. |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $row = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The obeserver's subject instance. |
||
| 49 | * |
||
| 50 | * @var object |
||
| 51 | */ |
||
| 52 | protected $subject; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Initializes the observer with the passed subject instance. |
||
| 56 | * |
||
| 57 | * @param object|null $subject The observer's subject instance |
||
| 58 | */ |
||
| 59 | public function __construct($subject = null) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Set's the obeserver's subject instance to initialize the observer with. |
||
| 68 | * |
||
| 69 | * @param object $subject The observer's subject |
||
| 70 | * |
||
| 71 | * @return void |
||
| 72 | */ |
||
| 73 | public function setSubject($subject) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Return's the observer's subject instance. |
||
| 80 | * |
||
| 81 | * @return object The observer's subject instance |
||
| 82 | */ |
||
| 83 | public function getSubject() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Set's the array containing header row. |
||
| 90 | * |
||
| 91 | * @param array $headers The array with the header row |
||
| 92 | * |
||
| 93 | * @return void |
||
| 94 | */ |
||
| 95 | public function setHeaders(array $headers) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Return's the array containing header row. |
||
| 102 | * |
||
| 103 | * @return array The array with the header row |
||
| 104 | */ |
||
| 105 | public function getHeaders() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Return's the RegistryProcessor instance to handle the running threads. |
||
| 112 | * |
||
| 113 | * @return \TechDivision\Import\Services\RegistryProcessorInterface The registry processor instance |
||
| 114 | */ |
||
| 115 | public function getRegistryProcessor() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Set's the actual row, that has to be processed. |
||
| 122 | * |
||
| 123 | * @param array $row The row |
||
| 124 | * |
||
| 125 | * @return void |
||
| 126 | */ |
||
| 127 | protected function setRow(array $row) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Return's the actual row, that has to be processed. |
||
| 134 | * |
||
| 135 | * @return array The row |
||
| 136 | */ |
||
| 137 | protected function getRow() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Queries whether or not debug mode is enabled or not, default is TRUE. |
||
| 144 | * |
||
| 145 | * @return boolean TRUE if debug mode is enabled, else FALSE |
||
| 146 | */ |
||
| 147 | protected function isDebugMode() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Stop's observer execution on the actual row. |
||
| 154 | * |
||
| 155 | * @return void |
||
| 156 | */ |
||
| 157 | protected function skipRow() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Return's the name of the file to import. |
||
| 164 | * |
||
| 165 | * @return string The filename |
||
| 166 | */ |
||
| 167 | protected function getFilename() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Return's the actual line number. |
||
| 174 | * |
||
| 175 | * @return integer The line number |
||
| 176 | */ |
||
| 177 | protected function getLineNumber() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Return's the logger with the passed name, by default the system logger. |
||
| 184 | * |
||
| 185 | * @param string $name The name of the requested system logger |
||
| 186 | * |
||
| 187 | * @return \Psr\Log\LoggerInterface The logger instance |
||
| 188 | * @throws \Exception Is thrown, if the requested logger is NOT available |
||
| 189 | */ |
||
| 190 | protected function getSystemLogger($name = LoggerKeys::SYSTEM) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Return's the array with the system logger instances. |
||
| 197 | * |
||
| 198 | * @return array The logger instance |
||
| 199 | */ |
||
| 200 | protected function getSystemLoggers() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Return's the multiple field delimiter character to use, default value is comma (,). |
||
| 207 | * |
||
| 208 | * @return string The multiple field delimiter character |
||
| 209 | */ |
||
| 210 | protected function getMultipleFieldDelimiter() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Queries whether or not the header with the passed name is available. |
||
| 217 | * |
||
| 218 | * @param string $name The header name to query |
||
| 219 | * |
||
| 220 | * @return boolean TRUE if the header is available, else FALSE |
||
| 221 | */ |
||
| 222 | protected function hasHeader($name) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Return's the header value for the passed name. |
||
| 229 | * |
||
| 230 | * @param string $name The name of the header to return the value for |
||
| 231 | * |
||
| 232 | * @return mixed The header value |
||
| 233 | * \InvalidArgumentException Is thrown, if the header with the passed name is NOT available |
||
| 234 | */ |
||
| 235 | protected function getHeader($name) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Add's the header with the passed name and position, if not NULL. |
||
| 242 | * |
||
| 243 | * @param string $name The header name to add |
||
| 244 | * |
||
| 245 | * @return integer The new headers position |
||
| 246 | */ |
||
| 247 | protected function addHeader($name) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Return's the ID of the product that has been created recently. |
||
| 254 | * |
||
| 255 | * @return string The entity Id |
||
| 256 | */ |
||
| 257 | protected function getLastEntityId() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Return's the source date format to use. |
||
| 264 | * |
||
| 265 | * @return string The source date format |
||
| 266 | */ |
||
| 267 | protected function getSourceDateFormat() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Cast's the passed value based on the backend type information. |
||
| 274 | * |
||
| 275 | * @param string $backendType The backend type to cast to |
||
| 276 | * @param mixed $value The value to be casted |
||
| 277 | * |
||
| 278 | * @return mixed The casted value |
||
| 279 | */ |
||
| 280 | protected function castValueByBackendType($backendType, $value) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Set's the store view code the create the product/attributes for. |
||
| 287 | * |
||
| 288 | * @param string $storeViewCode The store view code |
||
| 289 | * |
||
| 290 | * @return void |
||
| 291 | */ |
||
| 292 | protected function setStoreViewCode($storeViewCode) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Return's the store view code the create the product/attributes for. |
||
| 299 | * |
||
| 300 | * @param string|null $default The default value to return, if the store view code has not been set |
||
| 301 | * |
||
| 302 | * @return string The store view code |
||
| 303 | */ |
||
| 304 | protected function getStoreViewCode($default = null) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Prepare's the store view code in the subject. |
||
| 311 | * |
||
| 312 | * @return void |
||
| 313 | */ |
||
| 314 | protected function prepareStoreViewCode() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Tries to format the passed value to a valid date with format 'Y-m-d H:i:s'. |
||
| 328 | * If the passed value is NOT a valid date, NULL will be returned. |
||
| 329 | * |
||
| 330 | * @param string|null $value The value to format |
||
| 331 | * |
||
| 332 | * @return string The formatted date |
||
| 333 | */ |
||
| 334 | protected function formatDate($value) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Extracts the elements of the passed value by exploding them |
||
| 348 | * with the also passed delimiter. |
||
| 349 | * |
||
| 350 | * @param string $value The value to extract |
||
| 351 | * @param string|null $delimiter The delimiter used to extrace the elements |
||
| 352 | * |
||
| 353 | * @return array The exploded values |
||
| 354 | */ |
||
| 355 | protected function explode($value, $delimiter = null) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Query whether or not a value for the column with the passed name exists. |
||
| 369 | * |
||
| 370 | * @param string $name The column name to query for a valid value |
||
| 371 | * |
||
| 372 | * @return boolean TRUE if the value is set, else FALSE |
||
| 373 | */ |
||
| 374 | protected function hasValue($name) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Set the value in the passed column name. |
||
| 392 | * |
||
| 393 | * @param string $name The column name to set the value for |
||
| 394 | * @param mixed $value The value to set |
||
| 395 | * |
||
| 396 | * @return void |
||
| 397 | */ |
||
| 398 | protected function setValue($name, $value) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Resolve's the value with the passed colum name from the actual row. If a callback will |
||
| 405 | * be passed, the callback will be invoked with the found value as parameter. If |
||
| 406 | * the value is NULL or empty, the default value will be returned. |
||
| 407 | * |
||
| 408 | * @param string $name The name of the column to return the value for |
||
| 409 | * @param mixed|null $default The default value, that has to be returned, if the row's value is empty |
||
| 410 | * @param callable|null $callback The callback that has to be invoked on the value, e. g. to format it |
||
| 411 | * |
||
| 412 | * @return mixed|null The, almost formatted, value |
||
| 413 | */ |
||
| 414 | protected function getValue($name, $default = null, callable $callback = null) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Return's the Magento configuration value. |
||
| 446 | * |
||
| 447 | * @param string $path The Magento path of the requested configuration value |
||
| 448 | * @param mixed $default The default value that has to be returned, if the requested configuration value is not set |
||
| 449 | * @param string $scope The scope the configuration value has been set |
||
| 450 | * @param integer $scopeId The scope ID the configuration value has been set |
||
| 451 | * |
||
| 452 | * @return mixed The configuration value |
||
| 453 | * @throws \Exception Is thrown, if nor a value can be found or a default value has been passed |
||
| 454 | */ |
||
| 455 | protected function getCoreConfigData($path, $default = null, $scope = ScopeKeys::SCOPE_DEFAULT, $scopeId = 0) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Initialize's and return's a new entity with the status 'create'. |
||
| 462 | * |
||
| 463 | * @param array $attr The attributes to merge into the new entity |
||
| 464 | * |
||
| 465 | * @return array The initialized entity |
||
| 466 | */ |
||
| 467 | protected function initializeEntity(array $attr = array()) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Merge's and return's the entity with the passed attributes and set's the |
||
| 474 | * status to 'update'. |
||
| 475 | * |
||
| 476 | * @param array $entity The entity to merge the attributes into |
||
| 477 | * @param array $attr The attributes to be merged |
||
| 478 | * |
||
| 479 | * @return array The merged entity |
||
| 480 | */ |
||
| 481 | protected function mergeEntity(array $entity, array $attr) |
||
| 485 | } |
||
| 486 |