Skip to content

Commit

Permalink
resultCallback is a single function again
Browse files Browse the repository at this point in the history
  • Loading branch information
garfix committed Jul 14, 2018
1 parent 3757219 commit 5ff7535
Show file tree
Hide file tree
Showing 13 changed files with 26 additions and 43 deletions.
6 changes: 3 additions & 3 deletions Api/ImportConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ class ImportConfig
public $batchSize = 1000;

/**
* @var callable[]
* @var callable
*
* These functions will be called with the result of the import.
* This function will be called with the result of the import.
*
* Function signature:
*
* function(BigBridge\ProductImport\Model\Data\Product $product, $ok, $error);
*/
public $resultCallbacks = [];
public $resultCallback = null;

/**
* Create categories if they do not exist.
Expand Down
2 changes: 0 additions & 2 deletions Api/ImporterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ protected function validateConfig(ImportConfig $config)
throw new Exception("config: batchSize is not an integer");
} else if ($config->batchSize <= 0) {
throw new Exception("config: batchSize should be 1 or more");
} elseif (!is_array($config->resultCallbacks)) {
throw new Exception("config: resultCallbacks should be an array of functions");
}
}
}
2 changes: 1 addition & 1 deletion Api/ProductImportWebApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function process()

$importer = $this->importerFactory->createImporter($config);
$logger = new ProductImportWebApiLogger();
$config->resultCallbacks = [[$logger, 'productImported']];
$config->resultCallback = [$logger, 'productImported'];

$skipXsdValidation = !empty($parameters[self::OPTION_AUTO_CREATE_CATEGORIES]);

Expand Down
2 changes: 1 addition & 1 deletion Console/Command/ProductImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$logger = new ProductImportCommandLogger($output);

$config = new ImportConfig();
$config->resultCallbacks = [[$logger, 'productImported']];
$config->resultCallback = [$logger, 'productImported'];

$config->dryRun = $input->getOption(self::OPTION_DRY_RUN);
$config->autoCreateCategories = $input->getOption(self::OPTION_AUTO_CREATE_CATEGORIES);
Expand Down
2 changes: 1 addition & 1 deletion Model/Resource/ProductStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function storeProducts(array $products, ImportConfig $config)
}

// call user defined functions to let them process the results
foreach ($config->resultCallbacks as $callback) {
if (($callback = $config->resultCallback) !== null) {
foreach ($products as $product) {
if ($product->usedAsPlaceholder()) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion Test/Benchmark/MemoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testImport()
$lastErrors = [];

$config = new ImportConfig();
$config->resultCallbacks[] = function (Product $product) use (&$success, &$lastErrors) {
$config->resultCallback = function (Product $product) use (&$success, &$lastErrors) {
if ($product->getErrors()) {
$lastErrors = $product->getErrors();
}
Expand Down
14 changes: 0 additions & 14 deletions Test/Integration/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,6 @@ public function testConfig()

// ---

$importer = null;
$config = new ImportConfig();
$config->resultCallbacks = function() {};

try {
$importer = self::$factory->createImporter($config);
} catch (Exception $exception) {
$this->assertEquals("config: resultCallbacks should be an array of functions", $exception->getMessage());
}

$this->assertNull($importer);

// ---

$config = new ImportConfig();

try {
Expand Down
2 changes: 1 addition & 1 deletion Test/Integration/IdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testUpdateById()
$errors = [];

$config = new ImportConfig();
$config->resultCallbacks[] = function (Product $product) use (&$errors) {
$config->resultCallback = function (Product $product) use (&$errors) {
$errors = array_merge($errors, $product->getErrors());
};

Expand Down
28 changes: 14 additions & 14 deletions Test/Integration/ImportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function testInsertAndUpdate()
$errors = [];

$config = new ImportConfig();
$config->resultCallbacks[] = function (Product $product) use (&$success, &$errors) {
$config->resultCallback = function (Product $product) use (&$success, &$errors) {
$success = $success && $product->isOk();
$errors = array_merge($errors, $product->getErrors());
};
Expand Down Expand Up @@ -275,7 +275,7 @@ public function testResultCallback()
$lastId = null;

$config = new ImportConfig();
$config->resultCallbacks[] = function(Product $product) use (&$log, &$lastId) {
$config->resultCallback = function(Product $product) use (&$log, &$lastId) {

if ($product->isOk()) {
$log .= sprintf("%s: success! sku = %s, id = %s\n", $product->lineNumber, $product->getSku(), $product->id);
Expand Down Expand Up @@ -319,7 +319,7 @@ public function testCreateCategories()
$success = true;

$config = new ImportConfig();
$config->resultCallbacks[] = function(Product $product) use (&$success) {
$config->resultCallback = function(Product $product) use (&$success) {
$success = $success && $product->isOk();
};

Expand Down Expand Up @@ -358,7 +358,7 @@ public function testWebsites()
$errors = [];

$config = new ImportConfig();
$config->resultCallbacks[] = function(Product $product) use (&$errors) {
$config->resultCallback = function(Product $product) use (&$errors) {
$errors = array_merge($errors, $product->getErrors());
};

Expand Down Expand Up @@ -397,7 +397,7 @@ public function testMissingCategories()
// the essence of this test
$config->autoCreateCategories = false;

$config->resultCallbacks[] = function(Product $product) use (&$success) {
$config->resultCallback = function(Product $product) use (&$success) {
$success = $success && $product->isOk();
};

Expand Down Expand Up @@ -435,7 +435,7 @@ public function testImages()

$config = new ImportConfig();

$config->resultCallbacks[] = function(Product $product) use (&$errors) {
$config->resultCallback = function(Product $product) use (&$errors) {
$errors = array_merge($errors, $product->getErrors());
};

Expand Down Expand Up @@ -586,7 +586,7 @@ public function testStockItem()

$config = new ImportConfig();

$config->resultCallbacks[] = function (Product $product) use (&$errors) {
$config->resultCallback = function (Product $product) use (&$errors) {
$errors = array_merge($errors, $product->getErrors());
};

Expand Down Expand Up @@ -755,7 +755,7 @@ public function testImportConfigurable()

$config = new ImportConfig();

$config->resultCallbacks[] = function (Product $product) use (&$errors) {
$config->resultCallback = function (Product $product) use (&$errors) {
$errors = array_merge($errors, $product->getErrors());
};

Expand Down Expand Up @@ -1172,7 +1172,7 @@ public function testGroupedProduct()

$config = new ImportConfig();

$config->resultCallbacks[] = function (Product $product) use (&$errors) {
$config->resultCallback = function (Product $product) use (&$errors) {
$errors = array_merge($errors, $product->getErrors());
};

Expand Down Expand Up @@ -1389,7 +1389,7 @@ public function testVirtualProduct()
$errors = [];

$config = new ImportConfig();
$config->resultCallbacks[] = function (Product $product) use (&$errors) {
$config->resultCallback = function (Product $product) use (&$errors) {
$errors = array_merge($errors, $product->getErrors());
};

Expand Down Expand Up @@ -1422,7 +1422,7 @@ public function testDownloadableProduct()
$errors = [];

$config = new ImportConfig();
$config->resultCallbacks[] = function (Product $product) use (&$errors) {
$config->resultCallback = function (Product $product) use (&$errors) {
$errors = array_merge($errors, $product->getErrors());
};

Expand Down Expand Up @@ -1561,7 +1561,7 @@ public function testBundleProduct()
$errors = [];

$config = new ImportConfig();
$config->resultCallbacks[] = function (Product $product) use (&$errors) {
$config->resultCallback = function (Product $product) use (&$errors) {
$errors = array_merge($errors, $product->getErrors());
};

Expand Down Expand Up @@ -1702,7 +1702,7 @@ public function testCustomOptions()
$errors = [];

$config = new ImportConfig();
$config->resultCallbacks[] = function (Product $product) use (&$errors) {
$config->resultCallback = function (Product $product) use (&$errors) {
$errors = array_merge($errors, $product->getErrors());
};

Expand Down Expand Up @@ -1898,7 +1898,7 @@ public function testEmptyValues()
$errors = [];

$config = new ImportConfig();
$config->resultCallbacks[] = function (Product $product) use (&$errors) {
$config->resultCallback = function (Product $product) use (&$errors) {
$errors = array_merge($errors, $product->getErrors());
};

Expand Down
2 changes: 1 addition & 1 deletion doc/design-considerations.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ I chose for the option to have the developer explicitly call convertNameToId() b

## Batch processing

I only used batch processing because it is much faster than individual queries per product. For the developer, it is less comfortable, because the importer's process() function doesn't reply with the import results immediately. The resultCallbacks callback array is the only way the developer can get error feedback. It is not ideal, but I could think of no better method.
I only used batch processing because it is much faster than individual queries per product. For the developer, it is less comfortable, because the importer's process() function doesn't reply with the import results immediately. The resultCallback callback is the only way the developer can get error feedback. It is not ideal, but I could think of no better method.

## Placeholders

Expand Down
2 changes: 1 addition & 1 deletion doc/how-to.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ But if your aim is to stop the import after n errors have occurred and report on

$errorCount = 0;

$config->resultCallback[] = function(Product $product) use (&$errorCount, &$log)) {
$config->resultCallback = function(Product $product) use (&$errorCount, &$log)) {

if (!$product->isOk()) {
$errorCount++;
Expand Down
4 changes: 2 additions & 2 deletions doc/importer.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The following example shows you a typical use case scenario for the library:
$config = new ImportConfig();

// a callback function to postprocess imported products
$config->resultCallback[] = function(Product $product) use (&$log) {
$config->resultCallback = function(Product $product) use (&$log) {

if ($product->isOk()) {
$log .= sprintf("%s: success! sku = %s, id = %s\n", $product->lineNumber, $product->getSku(), $product->id);
Expand Down Expand Up @@ -173,7 +173,7 @@ The library detects problems in the input in its id-resolution and validation ph

A product that one or more errors is not imported. Errors can be inspected via a custom callback function you can provide.

$config->resultCallback[] = function(Product $product)) {
$config->resultCallback = function(Product $product)) {
$errors = $product->getErrors();
}

Expand Down
1 change: 0 additions & 1 deletion doc/todo.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Todo

* image not found? ignore, error
* resultCallback not an array

# Validation

Expand Down

0 comments on commit 5ff7535

Please sign in to comment.