Skip to content

Commit 86d34a5

Browse files
committed
Added ConnectorWrapper interface.
Added missing CachingConnector::__clone() method. Added ImportConnector::findBaseConnector(). Changed CachingConnector to implement ConnectorWrapper. Changed ImportConnector to clone its connector instead of relying on caller. Fixed some ImportSpecification docblocks.
1 parent 07a2445 commit 86d34a5

File tree

6 files changed

+57
-12
lines changed

6 files changed

+57
-12
lines changed

src/Connector/CachingConnector.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* Wraps a connector to cache fetched data using PSR-6-compliant objects.
1212
*/
13-
class CachingConnector implements Connector
13+
class CachingConnector implements Connector, ConnectorWrapper
1414
{
1515
/**
1616
* @var Connector
@@ -37,6 +37,14 @@ public function __construct(
3737
$this->cacheKeyGenerator = $cacheKeyGenerator ?: new JsonCacheKeyGenerator;
3838
}
3939

40+
public function __clone()
41+
{
42+
$this->connector = clone $this->connector;
43+
44+
/* It doesn't make sense to clone the cache because we want cache state to be shared between imports.
45+
We're also not cloning the CacheKeyGenerator because they're expected to be stateless algorithms. */
46+
}
47+
4048
/**
4149
* @param ConnectionContext $context
4250
* @param string $source
@@ -87,4 +95,9 @@ private function validateCacheKey($key)
8795
));
8896
}
8997
}
98+
99+
public function getWrappedConnector()
100+
{
101+
return $this->connector;
102+
}
90103
}

src/Connector/ConnectorWrapper.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace ScriptFUSION\Porter\Connector;
3+
4+
/**
5+
* Designates a connector decorator object and provides a method to access the wrapped connector.
6+
*/
7+
interface ConnectorWrapper
8+
{
9+
/**
10+
* Gets the wrapped connector.
11+
*
12+
* @return Connector
13+
*/
14+
public function getWrappedConnector();
15+
}

src/Connector/ImportConnector.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* Do not store references to this connector that would prevent it expiring when an import operation ends.
1212
*/
13-
final class ImportConnector
13+
final class ImportConnector implements ConnectorWrapper
1414
{
1515
private $connector;
1616

@@ -22,7 +22,7 @@ public function __construct(Connector $connector, ConnectionContext $context)
2222
throw CacheUnavailableException::createUnsupported();
2323
}
2424

25-
$this->connector = $connector;
25+
$this->connector = clone $connector;
2626
$this->context = $context;
2727
}
2828

@@ -34,13 +34,29 @@ public function fetch($source)
3434
/**
3535
* Gets the wrapped connector. Useful for resources to reconfigure connector options during this import.
3636
*
37-
* @return Connector
37+
* @return Connector Wrapped connector.
3838
*/
3939
public function getWrappedConnector()
4040
{
4141
return $this->connector;
4242
}
4343

44+
/**
45+
* Finds the base connector by traversing the stack of wrapped connectors.
46+
*
47+
* @return Connector Base connector.
48+
*/
49+
public function findBaseConnector()
50+
{
51+
$connector = $this->connector;
52+
53+
while ($connector instanceof ConnectorWrapper) {
54+
$connector = $connector->getWrappedConnector();
55+
}
56+
57+
return $connector;
58+
}
59+
4460
/**
4561
* Sets the exception handler to be called when a recoverable exception is thrown by Connector::fetch().
4662
*

src/Porter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private function fetch(ProviderResource $resource, $providerName, ConnectionCont
119119
);
120120
}
121121

122-
$records = $resource->fetch(new ImportConnector(clone $connector, $context));
122+
$records = $resource->fetch(new ImportConnector($connector, $context));
123123

124124
if (!$records instanceof \Iterator) {
125125
throw new ImportException(get_class($resource) . '::fetch() did not return an Iterator.');

src/Specification/ImportSpecification.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ final public function getResource()
8080
}
8181

8282
/**
83-
* Gets the provider name.
83+
* Gets the provider service name.
8484
*
8585
* @return string Provider name.
8686
*/
@@ -90,7 +90,7 @@ final public function getProviderName()
9090
}
9191

9292
/**
93-
* Sets the provider name.
93+
* Sets the provider service name.
9494
*
9595
* @param string $providerName Provider name.
9696
*
@@ -211,7 +211,7 @@ final public function disableCache()
211211
}
212212

213213
/**
214-
* Gets the maximum number of fetch attempts per import.
214+
* Gets the maximum number of fetch attempts per connection.
215215
*
216216
* @return int Maximum fetch attempts.
217217
*/
@@ -221,7 +221,7 @@ final public function getMaxFetchAttempts()
221221
}
222222

223223
/**
224-
* Sets the maximum number of fetch attempts per import.
224+
* Sets the maximum number of fetch attempts per connection before failure is considered permanent.
225225
*
226226
* @param int $attempts Maximum fetch attempts.
227227
*

test/Unit/Porter/Connector/ImportConnectorTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function testFetchCacheDisabled()
5555
public function testFetchCacheEnabled()
5656
{
5757
$connector = new ImportConnector(
58-
\Mockery::mock(CachingConnector::class)
58+
\Mockery::mock(CachingConnector::class, [\Mockery::mock(Connector::class)])
5959
->shouldReceive('fetch')
6060
->andReturn($output = 'foo')
6161
->getMock(),
@@ -79,7 +79,7 @@ public function testFetchCacheEnabledButNotAvailable()
7979
}
8080

8181
/**
82-
* Tests that getting the wrapped connector returns exactly the same connector as constructed with.
82+
* Tests that getting the wrapped connector returns a clone of the original connector passed to the constructor.
8383
*/
8484
public function testGetWrappedConnector()
8585
{
@@ -88,7 +88,8 @@ public function testGetWrappedConnector()
8888
FixtureFactory::buildConnectionContext()
8989
);
9090

91-
self::assertSame($wrappedConnector, $connector->getWrappedConnector());
91+
self::assertNotSame($wrappedConnector, $connector->getWrappedConnector());
92+
self::assertSame(get_class($wrappedConnector), get_class($connector->getWrappedConnector()));
9293
}
9394

9495
/**

0 commit comments

Comments
 (0)