Skip to content

Commit f79ad75

Browse files
committed
Removed ConnectionContext from Connector and AsyncConnector interfaces.
1 parent 0a8cdca commit f79ad75

File tree

14 files changed

+121
-174
lines changed

14 files changed

+121
-174
lines changed

src/Connector/AsyncConnector.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ interface AsyncConnector
1212
* Fetches data from the specified source.
1313
*
1414
* @param string $source Source.
15-
* @param ConnectionContext $context Runtime connection settings and methods.
1615
*
1716
* @return mixed Async generator function or any return value compatible with Amp\call.
1817
*/
19-
public function fetchAsync(string $source, ConnectionContext $context);
18+
public function fetchAsync(string $source);
2019
}

src/Connector/CachingConnector.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,23 @@ public function __clone()
5151

5252
/**
5353
* @param string $source
54-
* @param ConnectionContext $context
5554
*
5655
* @return mixed
5756
*
5857
* @throws InvalidCacheKeyException Cache key contains invalid data.
5958
*/
60-
public function fetch(string $source, ConnectionContext $context)
59+
public function fetch(string $source)
6160
{
62-
if ($context->mustCache()) {
63-
$options = $this->connector instanceof ConnectorOptions ? $this->connector->getOptions()->copy() : [];
64-
ksort($options);
61+
$options = $this->connector instanceof ConnectorOptions ? $this->connector->getOptions()->copy() : [];
62+
ksort($options);
6563

66-
$this->validateCacheKey($key = $this->cacheKeyGenerator->generateCacheKey($source, $options));
64+
$this->validateCacheKey($key = $this->cacheKeyGenerator->generateCacheKey($source, $options));
6765

68-
if ($this->cache->hasItem($key)) {
69-
return $this->cache->getItem($key)->get();
70-
}
66+
if ($this->cache->hasItem($key)) {
67+
return $this->cache->getItem($key)->get();
7168
}
7269

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

7572
isset($key) && $this->cache->save($this->cache->getItem($key)->set($data));
7673

src/Connector/ConnectionContext.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/Connector/Connector.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ interface Connector
1212
* Fetches data from the specified source optionally augmented by the specified options.
1313
*
1414
* @param string $source Source.
15-
* @param ConnectionContext $context Runtime connection settings and methods.
1615
*
1716
* @return mixed Data.
1817
*/
19-
public function fetch(string $source, ConnectionContext $context);
18+
public function fetch(string $source);
2019
}

src/Connector/ImportConnector.php

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
use ScriptFUSION\Porter\Connector\Recoverable\RecoverableException;
99
use ScriptFUSION\Porter\Connector\Recoverable\RecoverableExceptionHandler;
1010
use ScriptFUSION\Porter\Connector\Recoverable\StatelessRecoverableExceptionHandler;
11+
use function Amp\call;
12+
use function Amp\Promise\all;
13+
use function ScriptFUSION\Retry\retry;
14+
use function ScriptFUSION\Retry\retryAsync;
1115

1216
/**
13-
* Connector whose lifecycle is synchronised with an import operation. Ensures correct ConnectionContext is delivered
14-
* to the wrapped connector and intercepts failed connections to facilitate automatic retries.
17+
* Connector whose lifecycle is synchronised with an import operation. Intercepts failed connections to facilitate
18+
* automatic retries and manages caching.
1519
*
1620
* Do not store references to this connector that would prevent it expiring when an import operation ends.
1721
*
@@ -21,8 +25,6 @@ final class ImportConnector implements ConnectorWrapper
2125
{
2226
private $connector;
2327

24-
private $connectionContext;
25-
2628
/**
2729
* User-defined exception handler called when a recoverable exception is thrown by Connector::fetch().
2830
*
@@ -41,45 +43,49 @@ final class ImportConnector implements ConnectorWrapper
4143

4244
/**
4345
* @param Connector|AsyncConnector $connector Wrapped connector.
44-
* @param ConnectionContext $connectionContext Connection context.
4546
* @param RecoverableExceptionHandler $recoverableExceptionHandler User's recoverable exception handler.
4647
* @param int $maxFetchAttempts
48+
* @param bool $mustCache True if the response must be cached, otherwise false.
4749
*/
4850
public function __construct(
4951
$connector,
50-
ConnectionContext $connectionContext,
5152
RecoverableExceptionHandler $recoverableExceptionHandler,
52-
int $maxFetchAttempts
53+
int $maxFetchAttempts,
54+
bool $mustCache
5355
) {
54-
if ($connectionContext->mustCache() && !$connector instanceof CachingConnector) {
56+
if ($mustCache && !$connector instanceof CachingConnector) {
5557
throw CacheUnavailableException::createUnsupported();
5658
}
5759

58-
$this->connector = clone $connector;
59-
$this->connectionContext = $connectionContext;
60+
$this->connector = clone (
61+
$connector instanceof CachingConnector && !$mustCache
62+
// Bypass cache when not required.
63+
? $connector->getWrappedConnector()
64+
: $connector
65+
);
6066
$this->userExceptionHandler = $recoverableExceptionHandler;
6167
$this->maxFetchAttempts = $maxFetchAttempts;
6268
}
6369

6470
public function fetch(string $source)
6571
{
66-
return \ScriptFUSION\Retry\retry(
72+
return retry(
6773
$this->maxFetchAttempts,
6874
function () use ($source) {
69-
return $this->connector->fetch($source, $this->connectionContext);
75+
return $this->connector->fetch($source);
7076
},
7177
$this->createExceptionHandler()
7278
);
7379
}
7480

7581
public function fetchAsync(string $source): Promise
7682
{
77-
return \ScriptFUSION\Retry\retryAsync(
83+
return retryAsync(
7884
$this->maxFetchAttempts,
7985
function () use ($source): Promise {
80-
return \Amp\call(
86+
return call(
8187
function () use ($source) {
82-
return $this->connector->fetchAsync($source, $this->connectionContext);
88+
return $this->connector->fetchAsync($source);
8389
}
8490
);
8591
},
@@ -108,14 +114,15 @@ private function createExceptionHandler(): \Closure
108114
/*
109115
* Handlers may return a Promise, but all other return values are discarded. Although the underlying
110116
* library supports returning false, Porter only allows exceptions to short-circuit. However,
111-
* Porter does nothing to restrict promises that return false, although it is discouraged.
117+
* Porter does nothing to restrict promises that return false, although it is discouraged and may be
118+
* prevented in future. TODO: Mask promise return values.
112119
*/
113120
return ($promises = array_filter(
114121
$results,
115122
static function ($value): bool {
116123
return $value instanceof Promise;
117124
}
118-
)) ? \Amp\Promise\all($promises) : null;
125+
)) ? all($promises) : null;
119126
};
120127
}
121128

src/Connector/ImportConnectorFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public static function create($connector, Specification $specification): ImportC
2020
{
2121
return new ImportConnector(
2222
$connector,
23-
new ConnectionContext($specification->mustCache()),
2423
$specification->getRecoverableExceptionHandler(),
25-
$specification->getMaxFetchAttempts()
24+
$specification->getMaxFetchAttempts(),
25+
$specification->mustCache()
2626
);
2727
}
2828
}

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(string $source, ConnectionContext $context)
8+
public function fetch(string $source)
99
{
1010
// Intentionally empty.
1111
}

src/Specification/Specification.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ private function hasTransformer(AnysyncTransformer $transformer): bool
147147
}
148148

149149
/**
150-
* @return mixed
150+
* Gets the context to be passed to transformers.
151+
*
152+
* @return mixed Context.
151153
*
152154
* @deprecated TODO: Evaluate whether context can be removed.
153155
*/
@@ -157,7 +159,9 @@ final public function getContext()
157159
}
158160

159161
/**
160-
* @param mixed $context
162+
* Sets the context to be passed transformers.
163+
*
164+
* @param mixed $context Context.
161165
*
162166
* @return $this
163167
*
@@ -171,14 +175,18 @@ final public function setContext($context): self
171175
}
172176

173177
/**
174-
* @return bool
178+
* Gets a value indicating whether raw data caching is enabled for this import.
179+
*
180+
* @return bool True if caching is enabled, otherwise false.
175181
*/
176182
final public function mustCache(): bool
177183
{
178184
return $this->mustCache;
179185
}
180186

181187
/**
188+
* Enables raw data caching fetched for this import.
189+
*
182190
* @return $this
183191
*/
184192
final public function enableCache(): self
@@ -189,6 +197,8 @@ final public function enableCache(): self
189197
}
190198

191199
/**
200+
* Disables raw data caching.
201+
*
192202
* @return $this
193203
*/
194204
final public function disableCache(): self

test/FixtureFactory.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace ScriptFUSIONTest;
55

6-
use ScriptFUSION\Porter\Connector\ConnectionContext;
76
use ScriptFUSION\Porter\Connector\Connector;
87
use ScriptFUSION\Porter\Connector\ImportConnector;
98
use ScriptFUSION\Porter\Connector\Recoverable\RecoverableExceptionHandler;
@@ -14,25 +13,17 @@ final class FixtureFactory
1413
{
1514
use StaticClass;
1615

17-
/**
18-
* Builds ConnectionContexts with sane defaults for testing.
19-
*/
20-
public static function buildConnectionContext(bool $mustCache = false): ConnectionContext
21-
{
22-
return new ConnectionContext($mustCache);
23-
}
24-
2516
public static function buildImportConnector(
2617
Connector $connector,
27-
ConnectionContext $context = null,
2818
RecoverableExceptionHandler $recoverableExceptionHandler = null,
29-
int $maxFetchAttempts = ImportSpecification::DEFAULT_FETCH_ATTEMPTS
19+
int $maxFetchAttempts = ImportSpecification::DEFAULT_FETCH_ATTEMPTS,
20+
bool $mustCache = false
3021
): ImportConnector {
3122
return new ImportConnector(
3223
$connector,
33-
$context ?: self::buildConnectionContext(),
3424
$recoverableExceptionHandler ?: \Mockery::spy(RecoverableExceptionHandler::class),
35-
$maxFetchAttempts
25+
$maxFetchAttempts,
26+
$mustCache
3627
);
3728
}
3829
}

0 commit comments

Comments
 (0)