| Total Complexity | 49 |
| Total Lines | 449 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
Complex classes like Etl 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 Etl, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 56 | class Etl |
||
| 57 | { |
||
| 58 | /** |
||
| 59 | * @var callable|null |
||
| 60 | */ |
||
| 61 | protected $extract = null; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Used to transform data |
||
| 65 | * @var callable|null |
||
| 66 | */ |
||
| 67 | protected $transform = null; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Used to initialize loader |
||
| 71 | * @var callable|null |
||
| 72 | */ |
||
| 73 | protected $init = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The loader |
||
| 77 | * @var callable |
||
| 78 | */ |
||
| 79 | protected $load; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var callable|null |
||
| 83 | */ |
||
| 84 | protected $flush = null; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var callable|null |
||
| 88 | */ |
||
| 89 | protected $rollback = null; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Total to flush |
||
| 93 | * @var int|null |
||
| 94 | */ |
||
| 95 | protected ?int $flushCount = null; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Whether to flush data |
||
| 99 | * @var bool |
||
| 100 | */ |
||
| 101 | protected bool $isFlush = false; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Whether to skip data |
||
| 105 | * @var bool |
||
| 106 | */ |
||
| 107 | protected bool $isSkip = false; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Whether to stop processing data |
||
| 111 | * @var bool |
||
| 112 | */ |
||
| 113 | protected bool $isStop = false; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Whether to rollback data |
||
| 117 | * @var bool |
||
| 118 | */ |
||
| 119 | protected bool $isRollback = false; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * The event dispatcher |
||
| 123 | * @var DispatcherInterface |
||
| 124 | */ |
||
| 125 | protected DispatcherInterface $dispatcher; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Additional options |
||
| 129 | * @var array<string, mixed> |
||
| 130 | */ |
||
| 131 | protected array $options = []; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Create new instance |
||
| 135 | * @param callable|null $extract |
||
| 136 | * @param callable|null $transform |
||
| 137 | * @param callable|null $init |
||
| 138 | * @param callable|null $load |
||
| 139 | * @param callable|null $flush |
||
| 140 | * @param callable|null $rollback |
||
| 141 | * @param int|null $flushCount |
||
| 142 | * @param DispatcherInterface|null $dispatcher |
||
| 143 | */ |
||
| 144 | public function __construct( |
||
| 145 | ?callable $extract = null, |
||
| 146 | ?callable $transform = null, |
||
| 147 | ?callable $init = null, |
||
| 148 | ?callable $load = null, |
||
| 149 | ?callable $flush = null, |
||
| 150 | ?callable $rollback = null, |
||
| 151 | ?int $flushCount = null, |
||
| 152 | ?DispatcherInterface $dispatcher = null |
||
| 153 | ) { |
||
| 154 | $this->extract = $extract; |
||
| 155 | $this->transform = $transform ?? $this->defaultTransformer(); |
||
| 156 | $this->init = $init; |
||
| 157 | if ($load === null) { |
||
| 158 | $nullLoader = new NullLoader(); |
||
| 159 | $load = [$nullLoader, 'load']; |
||
| 160 | } |
||
| 161 | $this->load = $load; |
||
| 162 | $this->flush = $flush; |
||
| 163 | $this->rollback = $rollback; |
||
| 164 | $this->flushCount = $flushCount !== null ? max(1, $flushCount) : null; |
||
| 165 | $this->dispatcher = $dispatcher ?? new Dispatcher(); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Return the additional options |
||
| 170 | * @return array<string, mixed> |
||
| 171 | */ |
||
| 172 | public function getOptions(): array |
||
| 173 | { |
||
| 174 | return $this->options; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Set the additional options |
||
| 179 | * @param array<string, mixed> $options |
||
| 180 | * @return $this |
||
| 181 | */ |
||
| 182 | public function setOptions(array $options): self |
||
| 183 | { |
||
| 184 | $this->options = $options; |
||
| 185 | return $this; |
||
| 186 | } |
||
| 187 | |||
| 188 | |||
| 189 | /** |
||
| 190 | * Run the ETL on the given input. |
||
| 191 | * @param mixed|null $data |
||
| 192 | * @param array<string, mixed> $options additional options |
||
| 193 | * @return void |
||
| 194 | */ |
||
| 195 | public function process($data = null, array $options = []): void |
||
| 196 | { |
||
| 197 | if (count($options) > 0) { |
||
| 198 | $this->options = $options; |
||
| 199 | } |
||
| 200 | |||
| 201 | $flushCounter = 0; |
||
| 202 | $totalCounter = 0; |
||
| 203 | |||
| 204 | $this->start(); |
||
| 205 | foreach ($this->extract($data) as $key => $item) { |
||
| 206 | if ($this->isSkip) { |
||
| 207 | $this->skip($item, $key); |
||
| 208 | continue; |
||
| 209 | } |
||
| 210 | |||
| 211 | if ($this->isStop) { |
||
| 212 | $this->stop($item, $key); |
||
| 213 | break; |
||
| 214 | } |
||
| 215 | |||
| 216 | $transformed = $this->transform($item, $key); |
||
| 217 | |||
| 218 | if ($this->isSkip) { |
||
| 219 | $this->skip($item, $key); |
||
| 220 | continue; |
||
| 221 | } |
||
| 222 | |||
| 223 | if ($this->isStop) { |
||
| 224 | $this->stop($item, $key); |
||
| 225 | break; |
||
| 226 | } |
||
| 227 | |||
| 228 | $flushCounter++; |
||
| 229 | $totalCounter++; |
||
| 230 | |||
| 231 | if ($totalCounter === 1) { |
||
| 232 | $this->initLoader($item, $key); |
||
| 233 | } |
||
| 234 | |||
| 235 | $needFlush = ($this->flushCount === null ? false : (($totalCounter % $this->flushCount) === 0)); |
||
| 236 | $this->load($transformed(), $item, $key, $needFlush, $flushCounter, $totalCounter); |
||
| 237 | } |
||
| 238 | |||
| 239 | $this->end($flushCounter, $totalCounter); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Ask the ETl to stop. |
||
| 244 | * @param bool $isRollback if the loader should rollback instead of flushing. |
||
| 245 | * @return void |
||
| 246 | */ |
||
| 247 | public function stopProcess(bool $isRollback = false): void |
||
| 248 | { |
||
| 249 | $this->isStop = true; |
||
| 250 | $this->isRollback = $isRollback; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Mark the current item to be skipped. |
||
| 255 | * @return void |
||
| 256 | */ |
||
| 257 | public function skipCurrentItem(): void |
||
| 258 | { |
||
| 259 | $this->isSkip = true; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Ask the loader to trigger flush ASAP. |
||
| 264 | * @return void |
||
| 265 | */ |
||
| 266 | public function triggerFlush(): void |
||
| 267 | { |
||
| 268 | $this->isFlush = true; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Process item skip. |
||
| 273 | * @param mixed $item |
||
| 274 | * @param int|string $key |
||
| 275 | * @return void |
||
| 276 | */ |
||
| 277 | protected function skip($item, $key): void |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param mixed $item |
||
| 285 | * @param int|string $key |
||
| 286 | * @return void |
||
| 287 | */ |
||
| 288 | protected function stop($item, $key): void |
||
| 289 | { |
||
| 290 | $this->dispatcher->dispatch(new ItemEvent(BaseEvent::STOP, $item, $key, $this)); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Start processing |
||
| 295 | * @return void |
||
| 296 | */ |
||
| 297 | protected function start(): void |
||
| 298 | { |
||
| 299 | $this->reset(); |
||
| 300 | $this->dispatcher->dispatch(new StartEvent($this)); |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * reset ETL |
||
| 305 | * @return void |
||
| 306 | */ |
||
| 307 | protected function reset(): void |
||
| 308 | { |
||
| 309 | $this->isFlush = false; |
||
| 310 | $this->isSkip = false; |
||
| 311 | $this->isRollback = false; |
||
| 312 | $this->isStop = false; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Extract data. |
||
| 317 | * @param mixed $data |
||
| 318 | * @return iterable<int|string, mixed> |
||
| 319 | */ |
||
| 320 | protected function extract($data): iterable |
||
| 349 | } |
||
| 350 | } |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Transform data. |
||
| 355 | * @param mixed $item |
||
| 356 | * @param int|string $key |
||
| 357 | * @return callable |
||
| 358 | */ |
||
| 359 | protected function transform($item, $key): callable |
||
| 386 | } |
||
| 387 | }; |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Init the loader on the 1st item. |
||
| 392 | * @param mixed $item |
||
| 393 | * @param int|string $key |
||
| 394 | * @return void |
||
| 395 | */ |
||
| 396 | protected function initLoader($item, $key): void |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Load data. |
||
| 408 | * @param iterable<int|string, mixed> $data |
||
| 409 | * @param mixed $item |
||
| 410 | * @param int|string $key |
||
| 411 | * @param bool $flush |
||
| 412 | * @param int $flushCounter |
||
| 413 | * @param int $totalCounter |
||
| 414 | * @return void |
||
| 415 | */ |
||
| 416 | protected function load( |
||
| 444 | } |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Flush element |
||
| 449 | * @param int $flushCounter |
||
| 450 | * @param bool $partial |
||
| 451 | * @return void |
||
| 452 | */ |
||
| 453 | protected function flush(int &$flushCounter, bool $partial): void |
||
| 454 | { |
||
| 455 | if ($this->flush === null) { |
||
| 456 | return; |
||
| 457 | } |
||
| 458 | ($this->flush)($partial); |
||
| 459 | $this->dispatcher->dispatch(new FlushEvent($this, $flushCounter, $partial)); |
||
| 460 | $flushCounter = 0; |
||
| 461 | $this->isFlush = false; |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Restore loader's initial state. |
||
| 466 | * @param int $flushCounter |
||
| 467 | * @return void |
||
| 468 | */ |
||
| 469 | protected function rollback(int &$flushCounter): void |
||
| 470 | { |
||
| 471 | if ($this->rollback === null) { |
||
| 472 | return; |
||
| 473 | } |
||
| 474 | ($this->rollback)(); |
||
| 475 | $this->dispatcher->dispatch(new RollbackEvent($this, $flushCounter)); |
||
| 476 | $flushCounter = 0; |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Process the end of the ETL. |
||
| 481 | * @param int $flushCounter |
||
| 482 | * @param int $totalCounter |
||
| 483 | * @return void |
||
| 484 | */ |
||
| 485 | protected function end(int $flushCounter, int $totalCounter): void |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * The default transformer to use if none is set |
||
| 499 | * @return callable |
||
| 500 | */ |
||
| 501 | protected function defaultTransformer(): callable |
||
| 505 | }; |
||
| 506 | } |
||
| 507 | } |
||
| 508 |