Skip to content

Commit

Permalink
Ability to use ImportBuilder to create an import without using Import…
Browse files Browse the repository at this point in the history
…Request
  • Loading branch information
mathielen committed Dec 17, 2015
1 parent 745a497 commit f028fe6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
26 changes: 19 additions & 7 deletions src/Mathielen/ImportEngine/Import/ImportBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Mathielen\ImportEngine\Event\ImportConfigureEvent;
use Mathielen\ImportEngine\ValueObject\ImportRequest;
use Mathielen\ImportEngine\ValueObject\ImportRun;
use Mathielen\ImportEngine\ValueObject\StorageSelection;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class ImportBuilder
Expand All @@ -34,7 +35,7 @@ class ImportBuilder
public function __construct(
ImporterRepository $importerRepository,
StorageLocator $storageLocator,
EventDispatcherInterface $eventDispatcher=null)
EventDispatcherInterface $eventDispatcher = null)
{
$this->importerRepository = $importerRepository;
$this->storageLocator = $storageLocator;
Expand Down Expand Up @@ -68,8 +69,9 @@ public function rebuild(ImportRun $importRun)

/**
* @return Import
* @throws InvalidConfigurationException
*/
public function build(ImportRequest $importRequest)
public function buildFromRequest(ImportRequest $importRequest)
{
$importerId = $importRequest->getImporterId();
$storageSelection = null;
Expand All @@ -96,15 +98,25 @@ public function build(ImportRequest $importRequest)
}
}

$importer = $this->importerRepository->get($importerId);
$importConfiguration = new ImportConfiguration($storageSelection, $importerId);

return $this->build($importConfiguration, $sourceStorage, $importRequest->getCreatedBy());
}

/**
* @return Import
* @throws InvalidConfigurationException
*/
public function build(ImportConfiguration $importConfiguration, StorageInterface $sourceStorage = null, $createdBy = null)
{
$importer = $this->importerRepository->get($importConfiguration->getImporterId());
if ($importer->getSourceStorage()) {
$sourceStorage = $importer->getSourceStorage();
} elseif (!$sourceStorage) {
throw new InvalidConfigurationException("Either the importRequest or the importer '$importerId' must have a source storage set.");
throw new InvalidConfigurationException("Either the importRequest or the importer '".$importConfiguration->getImporterId()."' must have a source storage set.");
}

$importConfiguration = new ImportConfiguration($storageSelection, $importerId);
$importRun = $importConfiguration->toRun($importRequest->getCreatedBy());
$importRun = $importConfiguration->toRun($createdBy);
$importRun->setInfo((array) $sourceStorage->info());

//apply static context from importer
Expand All @@ -129,7 +141,7 @@ private function factorImport(Importer $importer, StorageInterface $sourceStorag
new ImportConfigureEvent($import));

$this->eventDispatcher->dispatch(
ImportConfigureEvent::AFTER_BUILD.'.'.$importRun->getConfiguration()->getImporterId(),
ImportConfigureEvent::AFTER_BUILD . '.' . $importRun->getConfiguration()->getImporterId(),
new ImportConfigureEvent($import));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function test()
$importRepository,
$storageLocator
);
$import = $importBuilder->build($importRequest);
$import = $importBuilder->buildFromRequest($importRequest);

$import->mappings()
->add('prefix', 'Anrede', 'upperCase')
Expand Down

0 comments on commit f028fe6

Please sign in to comment.