Skip to content

Commit

Permalink
Add progress bar if $migrator->withProgressBar() is called
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Hall committed Mar 28, 2018
1 parent 20e30fb commit 48559e3
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"psr/cache": "^1.0",
"cache/array-adapter": "^1.0",
"illuminate/support": "^5.1",
"rapidwebltd/array_undot": "^1.0"
"rapidwebltd/array_undot": "^1.0",
"rapidwebltd/php-cli-progress-bar": "^1.0"
}
}
1 change: 1 addition & 0 deletions src/Examples/AssocArraytoAssocArrayMigration/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
$dataItem->value = round($dataItem->value, 1);
}
})
->withProgressBar()
->migrate();

var_dump($destinationArray);
1 change: 1 addition & 0 deletions src/Examples/CSVtoCSVMigration/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
->setDestination($csvDestination)
->setFieldsToMigrate(['Author'])
->setFieldMap(['Author' => 'Writer'])
->withProgressBar()
->migrate();
1 change: 1 addition & 0 deletions src/Examples/PDOtoCSVMigration/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
->addDestination($csvDestination)
->addDestination($emailsCSVDestination, ['email'])
->setFieldsToMigrate(['email', 'name'])
->withProgressBar()
->migrate();
1 change: 1 addition & 0 deletions src/Examples/PDOtoPDOMigration/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@
}
}
})
->withProgressBar()
->migrate();
1 change: 1 addition & 0 deletions src/Examples/XMLtoCSVMigration/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
$migrator->setSource($xmlSource)
->setDestination($csvDestination)
->setFieldsToMigrate(['loc'])
->withProgressBar()
->migrate();
31 changes: 31 additions & 0 deletions src/Objects/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use RapidWeb\uxdm\Objects\Exceptions\MissingFieldToMigrateException;
use RapidWeb\uxdm\Objects\Exceptions\NoDestinationException;
use RapidWeb\uxdm\Objects\Exceptions\NoSourceException;
use RapidWeb\CliProgressBar\ProgressBar;

class Migrator
{
Expand All @@ -22,6 +23,8 @@ class Migrator
private $sourceCachePool;
private $sourceCacheKey;
private $sourceCacheExpiresAfter;
private $showProgressBar = false;
private $progressBar;

public function setSource(SourceInterface $source)
{
Expand Down Expand Up @@ -96,6 +99,12 @@ public function setSourceCache(CacheItemPoolInterface $sourceCachePool, $sourceC
return $this;
}

public function withProgressBar()
{
$this->showProgressBar = true;
return $this;
}

private function getSourceDataRows($page)
{
if (!$this->sourceCachePool || !$this->sourceCacheKey) {
Expand Down Expand Up @@ -147,7 +156,14 @@ public function migrate()

$results = [];

if ($this->showProgressBar) {
$this->progressBar = new ProgressBar();
$this->progressBar->setMaxProgress($this->source->countDataRows() * count($this->destinationContainers));
$this->progressBar->display();
}

for ($page = 1; $page < PHP_INT_MAX; $page++) {

$dataRows = $this->getSourceDataRows($page);

if (!$dataRows) {
Expand Down Expand Up @@ -177,6 +193,7 @@ public function migrate()
foreach ($this->destinationContainers as $destinationContainer) {
if (!$destinationContainer->fields) {
$results[] = $destinationContainer->destination->putDataRows($dataRows);
$this->advanceProgressBar(count($dataRows));
continue;
}

Expand All @@ -193,9 +210,23 @@ public function migrate()
}

$results[] = $destinationContainer->destination->putDataRows($destinationDataRows);
$this->advanceProgressBar(count($dataRows));
}
}

if ($this->showProgressBar) {
$this->progressBar->complete();
}

return $results;
}

private function advanceProgressBar($amount)
{
if ($this->showProgressBar) {
for ($i=0; $i < $amount; $i++) {
$this->progressBar->advance()->display();
}
}
}
}

0 comments on commit 48559e3

Please sign in to comment.