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 |
||
| 36 | abstract class AbstractObserver implements ObserverInterface |
||
| 37 | { |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The actual row, that has to be processed. |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $row = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The obeserver's subject instance. |
||
| 48 | * |
||
| 49 | * @var object |
||
| 50 | */ |
||
| 51 | protected $subject; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Initializes the observer with the passed subject instance. |
||
| 55 | * |
||
| 56 | * @param object|null $subject The observer's subject instance |
||
| 57 | */ |
||
| 58 | public function __construct($subject = null) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Set's the obeserver's subject instance to initialize the observer with. |
||
| 67 | * |
||
| 68 | * @param object $subject The observer's subject |
||
| 69 | * |
||
| 70 | * @return void |
||
| 71 | */ |
||
| 72 | public function setSubject($subject) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Return's the observer's subject instance. |
||
| 79 | * |
||
| 80 | * @return object The observer's subject instance |
||
| 81 | */ |
||
| 82 | public function getSubject() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Set's the array containing header row. |
||
| 89 | * |
||
| 90 | * @param array $headers The array with the header row |
||
| 91 | * |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | public function setHeaders(array $headers) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Return's the array containing header row. |
||
| 101 | * |
||
| 102 | * @return array The array with the header row |
||
| 103 | */ |
||
| 104 | public function getHeaders() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Return's the RegistryProcessor instance to handle the running threads. |
||
| 111 | * |
||
| 112 | * @return \TechDivision\Import\Services\RegistryProcessorInterface The registry processor instance |
||
| 113 | */ |
||
| 114 | public function getRegistryProcessor() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Set's the actual row, that has to be processed. |
||
| 121 | * |
||
| 122 | * @param array $row The row |
||
| 123 | * |
||
| 124 | * @return void |
||
| 125 | */ |
||
| 126 | protected function setRow(array $row) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Return's the actual row, that has to be processed. |
||
| 133 | * |
||
| 134 | * @return array The row |
||
| 135 | */ |
||
| 136 | protected function getRow() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Queries whether or not debug mode is enabled or not, default is TRUE. |
||
| 143 | * |
||
| 144 | * @return boolean TRUE if debug mode is enabled, else FALSE |
||
| 145 | */ |
||
| 146 | protected function isDebugMode() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Stop's observer execution on the actual row. |
||
| 153 | * |
||
| 154 | * @return void |
||
| 155 | */ |
||
| 156 | protected function skipRow() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Return's the name of the file to import. |
||
| 163 | * |
||
| 164 | * @return string The filename |
||
| 165 | */ |
||
| 166 | protected function getFilename() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Return's the actual line number. |
||
| 173 | * |
||
| 174 | * @return integer The line number |
||
| 175 | */ |
||
| 176 | protected function getLineNumber() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Return's the system logger. |
||
| 183 | * |
||
| 184 | * @return \Psr\Log\LoggerInterface The system logger instance |
||
| 185 | */ |
||
| 186 | protected function getSystemLogger() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Return's the multiple field delimiter character to use, default value is comma (,). |
||
| 193 | * |
||
| 194 | * @return string The multiple field delimiter character |
||
| 195 | */ |
||
| 196 | protected function getMultipleFieldDelimiter() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Queries whether or not the header with the passed name is available. |
||
| 203 | * |
||
| 204 | * @param string $name The header name to query |
||
| 205 | * |
||
| 206 | * @return boolean TRUE if the header is available, else FALSE |
||
| 207 | */ |
||
| 208 | protected function hasHeader($name) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Return's the header value for the passed name. |
||
| 215 | * |
||
| 216 | * @param string $name The name of the header to return the value for |
||
| 217 | * |
||
| 218 | * @return mixed The header value |
||
| 219 | * \InvalidArgumentException Is thrown, if the header with the passed name is NOT available |
||
| 220 | */ |
||
| 221 | protected function getHeader($name) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Add's the header with the passed name and position, if not NULL. |
||
| 228 | * |
||
| 229 | * @param string $name The header name to add |
||
| 230 | * |
||
| 231 | * @return integer The new headers position |
||
| 232 | */ |
||
| 233 | protected function addHeader($name) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Return's the ID of the product that has been created recently. |
||
| 240 | * |
||
| 241 | * @return string The entity Id |
||
| 242 | */ |
||
| 243 | protected function getLastEntityId() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Return's the source date format to use. |
||
| 250 | * |
||
| 251 | * @return string The source date format |
||
| 252 | */ |
||
| 253 | protected function getSourceDateFormat() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Cast's the passed value based on the backend type information. |
||
| 260 | * |
||
| 261 | * @param string $backendType The backend type to cast to |
||
| 262 | * @param mixed $value The value to be casted |
||
| 263 | * |
||
| 264 | * @return mixed The casted value |
||
| 265 | */ |
||
| 266 | protected function castValueByBackendType($backendType, $value) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Set's the store view code the create the product/attributes for. |
||
| 273 | * |
||
| 274 | * @param string $storeViewCode The store view code |
||
| 275 | * |
||
| 276 | * @return void |
||
| 277 | */ |
||
| 278 | protected function setStoreViewCode($storeViewCode) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Return's the store view code the create the product/attributes for. |
||
| 285 | * |
||
| 286 | * @param string|null $default The default value to return, if the store view code has not been set |
||
| 287 | * |
||
| 288 | * @return string The store view code |
||
| 289 | */ |
||
| 290 | protected function getStoreViewCode($default = null) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Prepare's the store view code in the subject. |
||
| 297 | * |
||
| 298 | * @return void |
||
| 299 | */ |
||
| 300 | protected function prepareStoreViewCode() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Tries to format the passed value to a valid date with format 'Y-m-d H:i:s'. |
||
| 314 | * If the passed value is NOT a valid date, NULL will be returned. |
||
| 315 | * |
||
| 316 | * @param string|null $value The value to format |
||
| 317 | * |
||
| 318 | * @return string The formatted date |
||
| 319 | */ |
||
| 320 | protected function formatDate($value) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Extracts the elements of the passed value by exploding them |
||
| 334 | * with the also passed delimiter. |
||
| 335 | * |
||
| 336 | * @param string $value The value to extract |
||
| 337 | * @param string|null $delimiter The delimiter used to extrace the elements |
||
| 338 | * |
||
| 339 | * @return array The exploded values |
||
| 340 | */ |
||
| 341 | protected function explode($value, $delimiter = null) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Query whether or not a value for the column with the passed name exists. |
||
| 355 | * |
||
| 356 | * @param string $name The column name to query for a valid value |
||
| 357 | * |
||
| 358 | * @return boolean TRUE if the value is set, else FALSE |
||
| 359 | */ |
||
| 360 | protected function hasValue($name) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Set the value in the passed column name. |
||
| 378 | * |
||
| 379 | * @param string $name The column name to set the value for |
||
| 380 | * @param mixed $value The value to set |
||
| 381 | * |
||
| 382 | * @return void |
||
| 383 | */ |
||
| 384 | protected function setValue($name, $value) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Resolve's the value with the passed colum name from the actual row. If a callback will |
||
| 391 | * be passed, the callback will be invoked with the found value as parameter. If |
||
| 392 | * the value is NULL or empty, the default value will be returned. |
||
| 393 | * |
||
| 394 | * @param string $name The name of the column to return the value for |
||
| 395 | * @param mixed|null $default The default value, that has to be returned, if the row's value is empty |
||
| 396 | * @param callable|null $callback The callback that has to be invoked on the value, e. g. to format it |
||
| 397 | * |
||
| 398 | * @return mixed|null The, almost formatted, value |
||
| 399 | */ |
||
| 400 | protected function getValue($name, $default = null, callable $callback = null) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Return's the Magento configuration value. |
||
| 432 | * |
||
| 433 | * @param string $path The Magento path of the requested configuration value |
||
| 434 | * @param mixed $default The default value that has to be returned, if the requested configuration value is not set |
||
| 435 | * @param string $scope The scope the configuration value has been set |
||
| 436 | * @param integer $scopeId The scope ID the configuration value has been set |
||
| 437 | * |
||
| 438 | * @return mixed The configuration value |
||
| 439 | * @throws \Exception Is thrown, if nor a value can be found or a default value has been passed |
||
| 440 | */ |
||
| 441 | protected function getCoreConfigData($path, $default = null, $scope = ScopeKeys::SCOPE_DEFAULT, $scopeId = 0) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Initialize's and return's a new entity with the status 'create'. |
||
| 448 | * |
||
| 449 | * @param array $attr The attributes to merge into the new entity |
||
| 450 | * |
||
| 451 | * @return array The initialized entity |
||
| 452 | */ |
||
| 453 | protected function initializeEntity(array $attr = array()) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Merge's and return's the entity with the passed attributes and set's the |
||
| 460 | * status to 'update'. |
||
| 461 | * |
||
| 462 | * @param array $entity The entity to merge the attributes into |
||
| 463 | * @param array $attr The attributes to be merged |
||
| 464 | * |
||
| 465 | * @return array The merged entity |
||
| 466 | */ |
||
| 467 | protected function mergeEntity(array $entity, array $attr) |
||
| 471 | } |
||
| 472 |