Skip to content

Commit 8ca936b

Browse files
committed
Changed Connector and AsyncConnector fetch() method parameter order.
Although this seems at first glance like a pointless BC break, we already broke BC with connectors when retry() was removed from ConnectionContext. Typical Connector implementations should no longer be concerned with the ConnectionContext at all, so we'd rather move it to the secondary position, since the source is always needed and thus should be the primary parameter.
1 parent 511ca5f commit 8ca936b

File tree

8 files changed

+30
-30
lines changed

8 files changed

+30
-30
lines changed

src/Connector/AsyncConnector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ interface AsyncConnector
1111
/**
1212
* Fetches data from the specified source.
1313
*
14-
* @param ConnectionContext $context Runtime connection settings and methods.
1514
* @param string $source Source.
15+
* @param ConnectionContext $context Runtime connection settings and methods.
1616
*
1717
* @return \Closure Closure that returns a Promise or raw data.
1818
*/
19-
public function fetchAsync(ConnectionContext $context, string $source): \Closure;
19+
public function fetchAsync(string $source, ConnectionContext $context): \Closure;
2020
}

src/Connector/CachingConnector.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ public function __clone()
5050
}
5151

5252
/**
53-
* @param ConnectionContext $context
5453
* @param string $source
54+
* @param ConnectionContext $context
5555
*
5656
* @return mixed
5757
*
58-
* @throws InvalidCacheKeyException
58+
* @throws InvalidCacheKeyException Cache key contains invalid data.
5959
*/
60-
public function fetch(ConnectionContext $context, string $source)
60+
public function fetch(string $source, ConnectionContext $context)
6161
{
6262
if ($context->mustCache()) {
6363
$options = $this->connector instanceof ConnectorOptions ? $this->connector->getOptions()->copy() : [];
@@ -70,7 +70,7 @@ public function fetch(ConnectionContext $context, string $source)
7070
}
7171
}
7272

73-
$data = $this->connector->fetch($context, $source);
73+
$data = $this->connector->fetch($source, $context);
7474

7575
isset($key) && $this->cache->save($this->cache->getItem($key)->set($data));
7676

src/Connector/Connector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ interface Connector
1111
/**
1212
* Fetches data from the specified source optionally augmented by the specified options.
1313
*
14-
* @param ConnectionContext $context Runtime connection settings and methods.
1514
* @param string $source Source.
15+
* @param ConnectionContext $context Runtime connection settings and methods.
1616
*
1717
* @return mixed Data.
1818
*/
19-
public function fetch(ConnectionContext $context, string $source);
19+
public function fetch(string $source, ConnectionContext $context);
2020
}

src/Connector/ImportConnector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function fetch(string $source)
6666
return \ScriptFUSION\Retry\retry(
6767
$this->maxFetchAttempts,
6868
function () use ($source) {
69-
return $this->connector->fetch($this->connectionContext, $source);
69+
return $this->connector->fetch($source, $this->connectionContext);
7070
},
7171
$this->createExceptionHandler()
7272
);
@@ -77,7 +77,7 @@ public function fetchAsync(string $source): Promise
7777
return \ScriptFUSION\Retry\retryAsync(
7878
$this->maxFetchAttempts,
7979
function () use ($source): Promise {
80-
return \Amp\call($this->connector->fetchAsync($this->connectionContext, $source));
80+
return \Amp\call($this->connector->fetchAsync($source, $this->connectionContext));
8181
},
8282
$this->createExceptionHandler()
8383
);

src/Connector/NullConnector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
final class NullConnector implements Connector
77
{
8-
public function fetch(ConnectionContext $context, string $source)
8+
public function fetch(string $source, ConnectionContext $context)
99
{
1010
// Intentionally empty.
1111
}

test/Integration/Porter/Connector/CachingConnectorTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ protected function setUp(): void
6969
*/
7070
public function testCacheEnabled(): void
7171
{
72-
self::assertSame('foo', $this->connector->fetch($this->context, 'baz'));
73-
self::assertSame('foo', $this->connector->fetch($this->context, 'baz'));
72+
self::assertSame('foo', $this->connector->fetch('baz', $this->context));
73+
self::assertSame('foo', $this->connector->fetch('baz', $this->context));
7474
}
7575

7676
/**
@@ -81,35 +81,35 @@ public function testCacheDisabled(): void
8181
// The default connection context has caching disabled.
8282
$context = FixtureFactory::buildConnectionContext();
8383

84-
self::assertSame('foo', $this->connector->fetch($context, 'baz'));
85-
self::assertSame('bar', $this->connector->fetch($context, 'baz'));
84+
self::assertSame('foo', $this->connector->fetch('baz', $context));
85+
self::assertSame('bar', $this->connector->fetch('baz', $context));
8686
}
8787

8888
/**
8989
* Tests that when sources are the same but options are different, the cache is not reused.
9090
*/
9191
public function testCacheBypassedForDifferentOptions(): void
9292
{
93-
self::assertSame('foo', $this->connector->fetch($this->context, 'baz'));
93+
self::assertSame('foo', $this->connector->fetch('baz', $this->context));
9494

9595
$this->options->setFoo('bar');
96-
self::assertSame('bar', $this->connector->fetch($this->context, 'baz'));
96+
self::assertSame('bar', $this->connector->fetch('baz', $this->context));
9797
}
9898

9999
/**
100100
* Tests that when the same options are specified by two different object instances, the cache is reused.
101101
*/
102102
public function testCacheUsedForDifferentOptionsInstance(): void
103103
{
104-
self::assertSame('foo', $this->connector->fetch($this->context, 'baz'));
104+
self::assertSame('foo', $this->connector->fetch('baz', $this->context));
105105

106106
$this->wrappedConnector->shouldReceive('getOptions')->andReturn($options = clone $this->options);
107107
self::assertNotSame($this->options, $options);
108-
self::assertSame('foo', $this->connector->fetch($this->context, 'baz'));
108+
self::assertSame('foo', $this->connector->fetch('baz', $this->context));
109109

110110
// Ensure new options have really taken effect by changing option. Cache should no longer be used.
111111
$options->setFoo('bar');
112-
self::assertSame('bar', $this->connector->fetch($this->context, 'baz'));
112+
self::assertSame('bar', $this->connector->fetch('baz', $this->context));
113113
}
114114

115115
public function testNullAndEmptyOptionsAreEquivalent(): void
@@ -119,8 +119,8 @@ public function testNullAndEmptyOptionsAreEquivalent(): void
119119
$this->wrappedConnector->shouldReceive('getOptions')->andReturn($options);
120120
self::assertEmpty($this->wrappedConnector->getOptions()->copy());
121121

122-
self::assertSame('foo', $this->connector->fetch($this->context, 'baz'));
123-
self::assertSame('foo', $this->connector->fetch($this->context, 'baz'));
122+
self::assertSame('foo', $this->connector->fetch('baz', $this->context));
123+
self::assertSame('foo', $this->connector->fetch('baz', $this->context));
124124
}
125125

126126
/**
@@ -145,7 +145,7 @@ function ($key) use ($reservedCharacters): void {
145145
->shouldReceive('set')->andReturn(\Mockery::mock(CacheItemInterface::class))
146146
;
147147

148-
$connector->fetch($this->context, $reservedCharacters);
148+
$connector->fetch($reservedCharacters, $this->context);
149149
}
150150

151151
/**
@@ -163,9 +163,9 @@ public function testCacheKeyGenerator(): void
163163
->getMock()
164164
);
165165

166-
self::assertSame('foo', $connector->fetch($this->context, $source));
167-
self::assertSame('foo', $connector->fetch($this->context, $source));
168-
self::assertSame('bar', $connector->fetch($this->context, $source));
166+
self::assertSame('foo', $connector->fetch($source, $this->context));
167+
self::assertSame('foo', $connector->fetch($source, $this->context));
168+
self::assertSame('bar', $connector->fetch($source, $this->context));
169169
}
170170

171171
public function testFetchThrowsInvalidCacheKeyExceptionOnNonPSR6CompliantCacheKey(): void
@@ -180,7 +180,7 @@ public function testFetchThrowsInvalidCacheKeyExceptionOnNonPSR6CompliantCacheKe
180180

181181
$this->expectException(InvalidCacheKeyException::class);
182182
$this->expectExceptionMessage('contains one or more reserved characters');
183-
$connector->fetch($this->context, 'baz');
183+
$connector->fetch('baz', $this->context);
184184
}
185185

186186
/**

test/Unit/Porter/Connector/ImportConnectorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public function testCallGraph(): void
3232
\Mockery::mock(Connector::class)
3333
->shouldReceive('fetch')
3434
->with(
35-
$context = FixtureFactory::buildConnectionContext(),
36-
$source = 'bar'
35+
$source = 'bar',
36+
$context = FixtureFactory::buildConnectionContext()
3737
)->once()
3838
->andReturn($output = 'foo')
3939
->getMock(),

test/Unit/Porter/Connector/NullConnectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ final class NullConnectorTest extends TestCase
1414
{
1515
public function test(): void
1616
{
17-
self::assertNull((new NullConnector)->fetch(FixtureFactory::buildConnectionContext(), 'foo'));
17+
self::assertNull((new NullConnector)->fetch('foo', FixtureFactory::buildConnectionContext()));
1818
}
1919
}

0 commit comments

Comments
 (0)