Skip to content

Commit

Permalink
Added Transformer support to Porter::import and ImportSpecification.
Browse files Browse the repository at this point in the history
Added Transformer interface.
Refactored filter code into FilterTransformer.
Removed filters and mappings from Porter and ImportSpecification.
Updated tests.
  • Loading branch information
Bilge committed Dec 18, 2016
1 parent ac2ffb6 commit ca03a92
Show file tree
Hide file tree
Showing 16 changed files with 180 additions and 460 deletions.
5 changes: 0 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ install:
script:
- bin/test --coverage-clover=build/logs/clover.xml

# Remove mapper and run all non-Mapper tests again.
- composer --dev --no-update-with-dependencies remove scriptfusion/mapper
- '! composer info | grep ^scriptfusion/mapper\\b'
- bin/test --exclude-group Mapper

after_success:
- composer --prefer-source require satooshi/php-coveralls
- vendor/bin/coveralls -v
21 changes: 0 additions & 21 deletions src/Collection/CountableMappedRecords.php

This file was deleted.

25 changes: 0 additions & 25 deletions src/Collection/MappedRecords.php

This file was deleted.

25 changes: 0 additions & 25 deletions src/Mapper/PorterMapper.php

This file was deleted.

10 changes: 0 additions & 10 deletions src/Mapper/Strategy/InvalidCallbackResultException.php

This file was deleted.

58 changes: 0 additions & 58 deletions src/Mapper/Strategy/SubImport.php

This file was deleted.

75 changes: 6 additions & 69 deletions src/Porter.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
<?php
namespace ScriptFUSION\Porter;

use ScriptFUSION\Mapper\CollectionMapper;
use ScriptFUSION\Mapper\Mapping;
use ScriptFUSION\Porter\Cache\CacheAdvice;
use ScriptFUSION\Porter\Cache\CacheToggle;
use ScriptFUSION\Porter\Cache\CacheUnavailableException;
use ScriptFUSION\Porter\Collection\CountableMappedRecords;
use ScriptFUSION\Porter\Collection\CountablePorterRecords;
use ScriptFUSION\Porter\Collection\CountableProviderRecords;
use ScriptFUSION\Porter\Collection\FilteredRecords;
use ScriptFUSION\Porter\Collection\MappedRecords;
use ScriptFUSION\Porter\Collection\PorterRecords;
use ScriptFUSION\Porter\Collection\ProviderRecords;
use ScriptFUSION\Porter\Collection\RecordCollection;
use ScriptFUSION\Porter\Connector\RecoverableConnectorException;
use ScriptFUSION\Porter\Mapper\PorterMapper;
use ScriptFUSION\Porter\Provider\ObjectNotCreatedException;
use ScriptFUSION\Porter\Provider\Provider;
use ScriptFUSION\Porter\Provider\ProviderFactory;
Expand All @@ -40,11 +34,6 @@ class Porter
*/
private $providerFactory;

/**
* @var CollectionMapper
*/
private $mapper;

/**
* @var CacheAdvice
*/
Expand All @@ -63,7 +52,6 @@ class Porter
public function __construct()
{
$this->defaultCacheAdvice = CacheAdvice::SHOULD_NOT_CACHE();
$this->fetchExceptionHandler = new ExponentialBackoffExceptionHandler;
}

/**
Expand All @@ -84,12 +72,12 @@ public function import(ImportSpecification $specification)
$records = $this->createProviderRecords($records, $specification->getResource());
}

if ($specification->getFilter()) {
$records = $this->filter($records, $specification->getFilter(), $specification->getContext());
}
foreach ($specification->getTransformers() as $transformer) {
if ($transformer instanceof PorterAware) {
$transformer->setPorter($this);
}

if ($specification->getMapping()) {
$records = $this->map($records, $specification->getMapping(), $specification->getContext());
$records = $transformer->transform($records, $specification->getContext());
}

return $this->createPorterRecords($records, $specification);
Expand Down Expand Up @@ -164,37 +152,6 @@ function (\Exception $exception) {
throw new ImportException(get_class($provider) . '::fetch() did not return an Iterator.');
}

private function filter(ProviderRecords $records, callable $predicate, $context)
{
$filter = function () use ($records, $predicate, $context) {
foreach ($records as $record) {
if ($predicate($record, $context)) {
yield $record;
}
}
};

return new FilteredRecords($filter(), $records, $filter);
}

private function map(RecordCollection $records, Mapping $mapping, $context)
{
return $this->createMappedRecords(
$this->getOrCreateMapper()->mapCollection($records, $mapping, $context),
$records,
$mapping
);
}

private function createMappedRecords(\Iterator $records, RecordCollection $previous, Mapping $mapping)
{
if ($previous instanceof \Countable) {
return new CountableMappedRecords($records, count($previous), $previous, $mapping);
}

return new MappedRecords($records, $previous, $mapping);
}

private function applyCacheAdvice(Provider $provider, CacheAdvice $cacheAdvice)
{
try {
Expand Down Expand Up @@ -304,26 +261,6 @@ private function getOrCreateProviderFactory()
return $this->providerFactory ?: $this->providerFactory = new ProviderFactory;
}

/**
* @return CollectionMapper
*/
private function getOrCreateMapper()
{
return $this->mapper ?: $this->mapper = new PorterMapper($this);
}

/**
* @param CollectionMapper $mapper
*
* @return $this
*/
public function setMapper(CollectionMapper $mapper)
{
$this->mapper = $mapper;

return $this;
}

/**
* Gets the maximum number of fetch attempts per import.
*
Expand Down Expand Up @@ -353,7 +290,7 @@ public function setMaxFetchAttempts($attempts)
*/
private function getFetchExceptionHandler()
{
return $this->fetchExceptionHandler;
return $this->fetchExceptionHandler ?: $this->fetchExceptionHandler = new ExponentialBackoffExceptionHandler;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Specification/DuplicateTransformerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace ScriptFUSION\Porter\Specification;

/**
* The exception that is thrown when a transformer already exists at the target site.
*/
class DuplicateTransformerException extends \RuntimeException
{
// Intentionally empty.
}
Loading

0 comments on commit ca03a92

Please sign in to comment.