Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Porter 3.0 – Transformers #21

Merged
merged 3 commits into from
Jan 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ install:
script:
- composer 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'
- composer test -- --exclude-group Mapper

after_success:
- composer require satooshi/php-coveralls
- vendor/bin/coveralls -v
152 changes: 70 additions & 82 deletions README.md

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
"zendframework/zend-uri": "^2"
},
"require-dev": {
"scriptfusion/mapper": "^1",
"phpunit/phpunit": "^4.8",
"mockery/mockery": "^0.9.4",
"symfony/process": "^3"
},
"suggest" : {
"scriptfusion/mapper": "Transforms imported data."
"transformers/mapping": "Transforms array collections using Mappings."
},
"autoload": {
"psr-4": {
Expand Down
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.

107 changes: 30 additions & 77 deletions src/Porter.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
<?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;
use ScriptFUSION\Porter\Provider\Resource\ProviderResource;
use ScriptFUSION\Porter\Specification\ImportSpecification;
use ScriptFUSION\Porter\Transform\Transformer;
use ScriptFUSION\Retry\ExceptionHandler\ExponentialBackoffExceptionHandler;

/**
Expand All @@ -40,11 +35,6 @@ class Porter
*/
private $providerFactory;

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

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

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

if ($specification->getFilter()) {
$records = $this->filter($records, $specification->getFilter(), $specification->getContext());
}

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

return $this->createPorterRecords($records, $specification);
}
Expand Down Expand Up @@ -121,27 +104,10 @@ public function importOne(ImportSpecification $specification)
return $one;
}

private function createProviderRecords(\Iterator $records, ProviderResource $resource)
{
if ($records instanceof \Countable) {
return new CountableProviderRecords($records, count($records), $resource);
}

return new ProviderRecords($records, $resource);
}

private function createPorterRecords(RecordCollection $records, ImportSpecification $specification)
{
if ($records instanceof \Countable) {
return new CountablePorterRecords($records, count($records), $specification);
}

return new PorterRecords($records, $specification);
}

private function fetch(ProviderResource $resource, CacheAdvice $cacheAdvice = null)
{
$provider = $this->getProvider($resource->getProviderClassName(), $resource->getProviderTag());

$this->applyCacheAdvice($provider, $cacheAdvice ?: $this->defaultCacheAdvice);

if (($records = \ScriptFUSION\Retry\retry(
Expand All @@ -164,35 +130,42 @@ function (\Exception $exception) {
throw new ImportException(get_class($provider) . '::fetch() did not return an Iterator.');
}

private function filter(ProviderRecords $records, callable $predicate, $context)
/**
* @param RecordCollection $records
* @param Transformer[] $transformers
* @param mixed $context
*
* @return RecordCollection
*/
private function transformRecords(RecordCollection $records, array $transformers, $context)
{
$filter = function () use ($records, $predicate, $context) {
foreach ($records as $record) {
if ($predicate($record, $context)) {
yield $record;
}
foreach ($transformers as $transformer) {
if ($transformer instanceof PorterAware) {
$transformer->setPorter($this);
}
};

return new FilteredRecords($filter(), $records, $filter);
$records = $transformer->transform($records, $context);
}

return $records;
}

private function map(RecordCollection $records, Mapping $mapping, $context)
private function createProviderRecords(\Iterator $records, ProviderResource $resource)
{
return $this->createMappedRecords(
$this->getOrCreateMapper()->mapCollection($records, $mapping, $context),
$records,
$mapping
);
if ($records instanceof \Countable) {
return new CountableProviderRecords($records, count($records), $resource);
}

return new ProviderRecords($records, $resource);
}

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

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

private function applyCacheAdvice(Provider $provider, CacheAdvice $cacheAdvice)
Expand Down Expand Up @@ -266,7 +239,7 @@ public function getProvider($name, $tag = null)
return $provider;
}
} catch (ObjectNotCreatedException $exception) {
// Intentionally empty.
// We will throw our own exception.
}

throw new ProviderNotFoundException(
Expand Down Expand Up @@ -304,26 +277,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 +306,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