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 |
||
| 35 | abstract class AbstractObserver implements ObserverInterface |
||
| 36 | { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The actual row, that has to be processed. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $row = array(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The obeserver's subject instance. |
||
| 47 | * |
||
| 48 | * @var object |
||
| 49 | */ |
||
| 50 | protected $subject; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Initializes the observer with the passed subject instance. |
||
| 54 | * |
||
| 55 | * @param object|null $subject The observer's subject instance |
||
| 56 | */ |
||
| 57 | public function __construct($subject = null) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Set's the obeserver's subject instance to initialize the observer with. |
||
| 66 | * |
||
| 67 | * @param object $subject The observer's subject |
||
| 68 | * |
||
| 69 | * @return void |
||
| 70 | */ |
||
| 71 | public function setSubject($subject) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Return's the observer's subject instance. |
||
| 78 | * |
||
| 79 | * @return object The observer's subject instance |
||
| 80 | */ |
||
| 81 | public function getSubject() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Set's the array containing header row. |
||
| 88 | * |
||
| 89 | * @param array $headers The array with the header row |
||
| 90 | * |
||
| 91 | * @return void |
||
| 92 | */ |
||
| 93 | public function setHeaders(array $headers) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Return's the array containing header row. |
||
| 100 | * |
||
| 101 | * @return array The array with the header row |
||
| 102 | */ |
||
| 103 | public function getHeaders() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Set's the actual row, that has to be processed. |
||
| 110 | * |
||
| 111 | * @param array $row The row |
||
| 112 | * |
||
| 113 | * @return void |
||
| 114 | */ |
||
| 115 | protected function setRow(array $row) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Return's the actual row, that has to be processed. |
||
| 122 | * |
||
| 123 | * @return array The row |
||
| 124 | */ |
||
| 125 | protected function getRow() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Queries whether or not debug mode is enabled or not, default is TRUE. |
||
| 132 | * |
||
| 133 | * @return boolean TRUE if debug mode is enabled, else FALSE |
||
| 134 | */ |
||
| 135 | protected function isDebugMode() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Stop's observer execution on the actual row. |
||
| 142 | * |
||
| 143 | * @return void |
||
| 144 | */ |
||
| 145 | protected function skipRow() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Return's the name of the file to import. |
||
| 152 | * |
||
| 153 | * @return string The filename |
||
| 154 | */ |
||
| 155 | protected function getFilename() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Return's the actual line number. |
||
| 162 | * |
||
| 163 | * @return integer The line number |
||
| 164 | */ |
||
| 165 | protected function getLineNumber() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Return's the system logger. |
||
| 172 | * |
||
| 173 | * @return \Psr\Log\LoggerInterface The system logger instance |
||
| 174 | */ |
||
| 175 | protected function getSystemLogger() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Return's the multiple field delimiter character to use, default value is comma (,). |
||
| 182 | * |
||
| 183 | * @return string The multiple field delimiter character |
||
| 184 | */ |
||
| 185 | protected function getMultipleFieldDelimiter() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Queries whether or not the header with the passed name is available. |
||
| 192 | * |
||
| 193 | * @param string $name The header name to query |
||
| 194 | * |
||
| 195 | * @return boolean TRUE if the header is available, else FALSE |
||
| 196 | */ |
||
| 197 | protected function hasHeader($name) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Return's the header value for the passed name. |
||
| 204 | * |
||
| 205 | * @param string $name The name of the header to return the value for |
||
| 206 | * |
||
| 207 | * @return mixed The header value |
||
| 208 | * \InvalidArgumentException Is thrown, if the header with the passed name is NOT available |
||
| 209 | */ |
||
| 210 | protected function getHeader($name) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Add's the header with the passed name and position, if not NULL. |
||
| 217 | * |
||
| 218 | * @param string $name The header name to add |
||
| 219 | * |
||
| 220 | * @return integer The new headers position |
||
| 221 | */ |
||
| 222 | protected function addHeader($name) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Return's the ID of the product that has been created recently. |
||
| 229 | * |
||
| 230 | * @return string The entity Id |
||
| 231 | */ |
||
| 232 | protected function getLastEntityId() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Return's the source date format to use. |
||
| 239 | * |
||
| 240 | * @return string The source date format |
||
| 241 | */ |
||
| 242 | protected function getSourceDateFormat() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Cast's the passed value based on the backend type information. |
||
| 249 | * |
||
| 250 | * @param string $backendType The backend type to cast to |
||
| 251 | * @param mixed $value The value to be casted |
||
| 252 | * |
||
| 253 | * @return mixed The casted value |
||
| 254 | */ |
||
| 255 | protected function castValueByBackendType($backendType, $value) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Set's the store view code the create the product/attributes for. |
||
| 262 | * |
||
| 263 | * @param string $storeViewCode The store view code |
||
| 264 | * |
||
| 265 | * @return void |
||
| 266 | */ |
||
| 267 | protected function setStoreViewCode($storeViewCode) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Return's the store view code the create the product/attributes for. |
||
| 274 | * |
||
| 275 | * @param string|null $default The default value to return, if the store view code has not been set |
||
| 276 | * |
||
| 277 | * @return string The store view code |
||
| 278 | */ |
||
| 279 | protected function getStoreViewCode($default = null) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Prepare's the store view code in the subject. |
||
| 286 | * |
||
| 287 | * @return void |
||
| 288 | */ |
||
| 289 | protected function prepareStoreViewCode() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Tries to format the passed value to a valid date with format 'Y-m-d H:i:s'. |
||
| 303 | * If the passed value is NOT a valid date, NULL will be returned. |
||
| 304 | * |
||
| 305 | * @param string|null $value The value to format |
||
| 306 | * |
||
| 307 | * @return string The formatted date |
||
| 308 | */ |
||
| 309 | protected function formatDate($value) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Extracts the elements of the passed value by exploding them |
||
| 323 | * with the also passed delimiter. |
||
| 324 | * |
||
| 325 | * @param string $value The value to extract |
||
| 326 | * @param string|null $delimiter The delimiter used to extrace the elements |
||
| 327 | * |
||
| 328 | * @return array The exploded values |
||
| 329 | */ |
||
| 330 | protected function explode($value, $delimiter = null) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Query whether or not a value for the column with the passed name exists. |
||
| 344 | * |
||
| 345 | * @param string $name The column name to query for a valid value |
||
| 346 | * |
||
| 347 | * @return boolean TRUE if the value is set, else FALSE |
||
| 348 | */ |
||
| 349 | protected function hasValue($name) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Set the value in the passed column name. |
||
| 367 | * |
||
| 368 | * @param string $name The column name to set the value for |
||
| 369 | * @param mixed $value The value to set |
||
| 370 | * |
||
| 371 | * @return void |
||
| 372 | */ |
||
| 373 | protected function setValue($name, $value) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Resolve's the value with the passed colum name from the actual row. If a callback will |
||
| 380 | * be passed, the callback will be invoked with the found value as parameter. If |
||
| 381 | * the value is NULL or empty, the default value will be returned. |
||
| 382 | * |
||
| 383 | * @param string $name The name of the column to return the value for |
||
| 384 | * @param mixed|null $default The default value, that has to be returned, if the row's value is empty |
||
| 385 | * @param callable|null $callback The callback that has to be invoked on the value, e. g. to format it |
||
| 386 | * |
||
| 387 | * @return mixed|null The, almost formatted, value |
||
| 388 | */ |
||
| 389 | protected function getValue($name, $default = null, callable $callback = null) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Initialize's and return's a new entity with the status 'create'. |
||
| 421 | * |
||
| 422 | * @param array $attr The attributes to merge into the new entity |
||
| 423 | * |
||
| 424 | * @return array The initialized entity |
||
| 425 | */ |
||
| 426 | protected function initializeEntity(array $attr = array()) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Merge's and return's the entity with the passed attributes and set's the |
||
| 433 | * status to 'update'. |
||
| 434 | * |
||
| 435 | * @param array $entity The entity to merge the attributes into |
||
| 436 | * @param array $attr The attributes to be merged |
||
| 437 | * |
||
| 438 | * @return array The merged entity |
||
| 439 | */ |
||
| 440 | protected function mergeEntity(array $entity, array $attr) |
||
| 444 | } |
||
| 445 |