Skip to content

Commit

Permalink
Renamed ProviderDataSource::getProviderName -> getProviderClassName.
Browse files Browse the repository at this point in the history
Renamed MutableCacheState -> CacheToggle.
  • Loading branch information
Bilge committed Jul 3, 2016
1 parent 1d63d15 commit 4fa7b75
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
namespace ScriptFUSION\Porter\Cache;

/**
* Defines methods for getting and setting cache availability.
* Defines methods for getting and setting whether a cache is enabled.
*/
interface MutableCacheState
interface CacheToggle
{
/**
* Enables the cache, permitting subsequent cache access operations.
Expand Down
4 changes: 2 additions & 2 deletions src/Porter/Connector/CachingConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
namespace ScriptFUSION\Porter\Connector;

use Psr\Cache\CacheItemPoolInterface;
use ScriptFUSION\Porter\Cache\CacheToggle;
use ScriptFUSION\Porter\Cache\MemoryCache;
use ScriptFUSION\Porter\Cache\MutableCacheState;
use ScriptFUSION\Porter\Options\EncapsulatedOptions;

abstract class CachingConnector implements Connector, MutableCacheState
abstract class CachingConnector implements Connector, CacheToggle
{
private $cache;

Expand Down
8 changes: 0 additions & 8 deletions src/Porter/Mapper/PorterMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
namespace ScriptFUSION\Porter\Mapper;

use ScriptFUSION\Mapper\CollectionMapper;
use ScriptFUSION\Mapper\Mapping;
use ScriptFUSION\Porter\Collection\MappedRecords;
use ScriptFUSION\Porter\Collection\RecordCollection;
use ScriptFUSION\Porter\Porter;
use ScriptFUSION\Porter\PorterAware;

Expand All @@ -17,11 +14,6 @@ public function __construct(Porter $porter)
$this->porter = $porter;
}

public function mapRecords(RecordCollection $records, Mapping $mapping, $context = null)
{
return new MappedRecords($this->mapCollection($records, $mapping, $context), $records);
}

protected function injectDependencies($object)
{
parent::injectDependencies($object);
Expand Down
18 changes: 10 additions & 8 deletions src/Porter/Porter.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<?php
namespace ScriptFUSION\Porter;

use ScriptFUSION\Mapper\CollectionMapper;
use ScriptFUSION\Mapper\Mapping;
use ScriptFUSION\Porter\Cache\CacheAdvice;
use ScriptFUSION\Porter\Cache\CacheOperationProhibitedException;
use ScriptFUSION\Porter\Cache\MutableCacheState;
use ScriptFUSION\Porter\Cache\CacheToggle;
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\Mapper\PorterMapper;
use ScriptFUSION\Porter\Provider\DataSource\ProviderDataSource;
use ScriptFUSION\Porter\Provider\ObjectNotCreatedException;
use ScriptFUSION\Porter\Provider\Provider;
use ScriptFUSION\Porter\Provider\DataSource\ProviderDataSource;
use ScriptFUSION\Porter\Provider\ProviderFactory;
use ScriptFUSION\Porter\Specification\ImportSpecification;

Expand Down Expand Up @@ -59,7 +61,7 @@ public function import(ImportSpecification $specification)

private function fetch(ProviderDataSource $dataSource, CacheAdvice $cacheAdvice = null)
{
$provider = $this->getProvider($dataSource->getProviderName());
$provider = $this->getProvider($dataSource->getProviderClassName());
$this->applyCacheAdvice($provider, $cacheAdvice ?: $this->defaultCacheAdvice);

return $provider->fetch($dataSource);
Expand All @@ -80,10 +82,10 @@ private function filter(ProviderRecords $records, callable $predicate, $context)

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

private function applyCacheAdvice(MutableCacheState $cache, CacheAdvice $cacheAdvice)
private function applyCacheAdvice(CacheToggle $cache, CacheAdvice $cacheAdvice)
{
try {
switch ("$cacheAdvice") {
Expand Down Expand Up @@ -151,19 +153,19 @@ private function getOrCreateProviderFactory()
}

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/Porter/Provider/DataSource/ProviderDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface ProviderDataSource
*
* @return string
*/
public function getProviderName();
public function getProviderClassName();

/**
* Fetches data from the provider using the the specified connector and
Expand Down
2 changes: 1 addition & 1 deletion src/Porter/Provider/DataSource/StaticDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function __construct(\Iterator $data)
$this->data = $data;
}

public function getProviderName()
public function getProviderClassName()
{
return StaticDataProvider::class;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Porter/Provider/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
namespace ScriptFUSION\Porter\Provider;

use ScriptFUSION\Porter\Cache\CacheOperationProhibitedException;
use ScriptFUSION\Porter\Cache\MutableCacheState;
use ScriptFUSION\Porter\Cache\CacheToggle;
use ScriptFUSION\Porter\Connector\Connector;
use ScriptFUSION\Porter\Provider\DataSource\ProviderDataSource;

abstract class Provider implements MutableCacheState
abstract class Provider implements CacheToggle
{
private $connector;

Expand All @@ -24,7 +24,7 @@ public function __construct(Connector $connector)
*/
public function fetch(ProviderDataSource $dataSource)
{
if ($dataSource->getProviderName() !== static::class) {
if ($dataSource->getProviderClassName() !== static::class) {
throw new ForeignDataSourceException(sprintf(
'Cannot fetch data from foreign source: "%s".',
get_class($dataSource)
Expand All @@ -46,7 +46,7 @@ public function enableCache()
{
$connector = $this->getConnector();

if (!$connector instanceof MutableCacheState) {
if (!$connector instanceof CacheToggle) {
throw $this->createCacheUnavailableException();
}

Expand All @@ -57,7 +57,7 @@ public function disableCache()
{
$connector = $this->getConnector();

if (!$connector instanceof MutableCacheState) {
if (!$connector instanceof CacheToggle) {
throw $this->createCacheUnavailableException();
}

Expand All @@ -68,7 +68,7 @@ public function isCacheEnabled()
{
$connector = $this->getConnector();

if (!$connector instanceof MutableCacheState) {
if (!$connector instanceof CacheToggle) {
return false;
}

Expand Down
8 changes: 6 additions & 2 deletions test/Integration/Porter/PorterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
namespace ScriptFUSIONTest\Integration\Porter;

use Mockery\MockInterface;
use ScriptFUSION\Porter\Collection\FilteredRecords;
use ScriptFUSION\Porter\Collection\PorterRecords;
use ScriptFUSION\Porter\Collection\ProviderRecords;
use ScriptFUSION\Porter\Porter;
use ScriptFUSION\Porter\Provider\Provider;
use ScriptFUSION\Porter\Provider\DataSource\ProviderDataSource;
use ScriptFUSION\Porter\Provider\Provider;
use ScriptFUSION\Porter\ProviderNotFoundException;
use ScriptFUSION\Porter\Specification\ImportSpecification;

Expand All @@ -24,7 +26,7 @@ protected function setUp()
{
$this->porter = (new Porter)->addProvider($this->provider = \Mockery::spy(Provider::class));
$this->dataSource = \Mockery::mock(ProviderDataSource::class)
->shouldReceive('getProviderName')
->shouldReceive('getProviderClassName')
->andReturn(get_class($this->provider))
->getMock();
}
Expand Down Expand Up @@ -59,6 +61,7 @@ public function testImport()
$records = $this->porter->import(new ImportSpecification($this->dataSource));

self::assertInstanceOf(PorterRecords::class, $records);
self::assertInstanceOf(ProviderRecords::class, $records->getPreviousCollection());
self::assertSame('foo', $records->current());
}

Expand All @@ -73,6 +76,7 @@ public function testFilter()
})
);

self::assertInstanceOf(FilteredRecords::class, $records->getPreviousCollection());
self::assertSame([1, 3, 5, 7, 9], iterator_to_array($records));
}
}
4 changes: 1 addition & 3 deletions test/Unit/Porter/Mapper/PorterMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use ScriptFUSION\Mapper\AnonymousMapping;
use ScriptFUSION\Mapper\Strategy\Strategy;
use ScriptFUSION\Porter\Collection\MappedRecords;
use ScriptFUSION\Porter\Collection\RecordCollection;
use ScriptFUSION\Porter\Mapper\PorterMapper;
use ScriptFUSION\Porter\Porter;
Expand All @@ -24,7 +23,7 @@ public function testMap()
[new \ArrayIterator([[1], [2], [3]])]
)->makePartial();

$mappedRecords = $mapper->mapRecords(
$mappedRecords = $mapper->mapCollection(
$records,
new AnonymousMapping([$strategy = \Mockery::mock(implode(',', [Strategy::class, PorterAware::class]))])
);
Expand All @@ -33,7 +32,6 @@ public function testMap()
return $data[0] * $data[0];
})->getMock()->shouldReceive('setPorter')->with($porter)->atLeast()->once();

self::assertInstanceOf(MappedRecords::class, $mappedRecords);
self::assertSame([[1], [4], [9]], iterator_to_array($mappedRecords));
}
}
6 changes: 3 additions & 3 deletions test/Unit/Porter/Provider/ProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use ScriptFUSION\Porter\Cache\CacheOperationProhibitedException;
use ScriptFUSION\Porter\Connector\CachingConnector;
use ScriptFUSION\Porter\Connector\Connector;
use ScriptFUSION\Porter\Provider\DataSource\ProviderDataSource;
use ScriptFUSION\Porter\Provider\ForeignDataSourceException;
use ScriptFUSION\Porter\Provider\Provider;
use ScriptFUSION\Porter\Provider\DataSource\ProviderDataSource;

final class ProviderTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -50,7 +50,7 @@ public function testFetch()
->with($this->connector)
->andReturn('foo')
->getMock()
->shouldReceive('getProviderName')
->shouldReceive('getProviderClassName')
->andReturn(get_class($this->provider))
->getMock()
)
Expand All @@ -63,7 +63,7 @@ public function testFetchForeignProvider()

$this->provider->fetch(
\Mockery::mock(ProviderDataSource::class)
->shouldReceive('getProviderName')
->shouldReceive('getProviderClassName')
->andReturn('foo')
->getMock()
);
Expand Down

0 comments on commit 4fa7b75

Please sign in to comment.