Skip to content

Commit fd79d5a

Browse files
committed
Added DataSource abstraction.
Removed failed abstractions: ConnectorOptions and EncapsulatedOptions. Removed redundant abstraction: CacheKeyGenerator.
1 parent 6e8bf1e commit fd79d5a

24 files changed

+102
-516
lines changed

src/Cache/CacheKeyGenerator.php

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

src/Cache/JsonCacheKeyGenerator.php

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

src/CloneNotImplementedException.php

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

src/Connector/AsyncConnector.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
use Amp\Promise;
77

88
/**
9-
* Provides a method for fetching data from a remote source asynchronously.
9+
* Provides a method for fetching data from a source asynchronously.
1010
*/
1111
interface AsyncConnector
1212
{
1313
/**
1414
* Fetches data from the specified source.
1515
*
16-
* @param string $source Source.
16+
* @param DataSource $source Source.
1717
*
18-
* @return Promise Fetched data.
18+
* @return Promise<mixed> Data.
1919
*/
20-
public function fetchAsync(string $source): Promise;
20+
public function fetchAsync(DataSource $source): Promise;
2121
}

src/Connector/CachingConnector.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
namespace ScriptFUSION\Porter\Connector;
55

66
use Psr\Cache\CacheItemPoolInterface;
7-
use ScriptFUSION\Porter\Cache\CacheKeyGenerator;
87
use ScriptFUSION\Porter\Cache\InvalidCacheKeyException;
9-
use ScriptFUSION\Porter\Cache\JsonCacheKeyGenerator;
108
use ScriptFUSION\Porter\Cache\MemoryCache;
119

1210
/**
@@ -16,6 +14,8 @@
1614
*/
1715
class CachingConnector implements Connector, ConnectorWrapper
1816
{
17+
public const RESERVED_CHARACTERS = '{}()/\@:';
18+
1919
/**
2020
* @var Connector
2121
*/
@@ -26,19 +26,12 @@ class CachingConnector implements Connector, ConnectorWrapper
2626
*/
2727
private $cache;
2828

29-
/**
30-
* @var CacheKeyGenerator
31-
*/
32-
private $cacheKeyGenerator;
33-
3429
public function __construct(
3530
Connector $connector,
36-
CacheItemPoolInterface $cache = null,
37-
CacheKeyGenerator $cacheKeyGenerator = null
31+
CacheItemPoolInterface $cache = null
3832
) {
3933
$this->connector = $connector;
4034
$this->cache = $cache ?: new MemoryCache;
41-
$this->cacheKeyGenerator = $cacheKeyGenerator ?: new JsonCacheKeyGenerator;
4235
}
4336

4437
public function __clone()
@@ -50,18 +43,11 @@ public function __clone()
5043
}
5144

5245
/**
53-
* @param string $source
54-
*
55-
* @return mixed
56-
*
5746
* @throws InvalidCacheKeyException Cache key contains invalid data.
5847
*/
59-
public function fetch(string $source)
48+
public function fetch(DataSource $source)
6049
{
61-
$options = $this->connector instanceof ConnectorOptions ? $this->connector->getOptions()->copy() : [];
62-
ksort($options);
63-
64-
$this->validateCacheKey($key = $this->cacheKeyGenerator->generateCacheKey($source, $options));
50+
$this->validateCacheKey($key = $source->computeHash());
6551

6652
if ($this->cache->hasItem($key)) {
6753
return $this->cache->getItem($key)->get();
@@ -79,11 +65,11 @@ public function fetch(string $source)
7965
*/
8066
private function validateCacheKey(string $key): void
8167
{
82-
if (strpbrk($key, CacheKeyGenerator::RESERVED_CHARACTERS) !== false) {
68+
if (strpbrk($key, self::RESERVED_CHARACTERS) !== false) {
8369
throw new InvalidCacheKeyException(sprintf(
8470
'Cache key "%s" contains one or more reserved characters: "%s".',
8571
$key,
86-
CacheKeyGenerator::RESERVED_CHARACTERS
72+
self::RESERVED_CHARACTERS
8773
));
8874
}
8975
}

src/Connector/Connector.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
namespace ScriptFUSION\Porter\Connector;
55

66
/**
7-
* Provides a method for fetching data from a remote source.
7+
* Provides a method for fetching data from a source.
88
*/
99
interface Connector
1010
{
1111
/**
12-
* Fetches data from the specified source optionally augmented by the specified options.
12+
* Fetches data from the specified source.
1313
*
14-
* @param string $source Source.
14+
* @param DataSource $source Source.
1515
*
1616
* @return mixed Data.
1717
*/
18-
public function fetch(string $source);
18+
public function fetch(DataSource $source);
1919
}

src/Connector/ConnectorOptions.php

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

src/Connector/DataSource.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ScriptFUSION\Porter\Connector;
5+
6+
/**
7+
* Specifies a data source and the necessary parameters required to fetch it.
8+
*/
9+
interface DataSource
10+
{
11+
/**
12+
* Computes the hash code for this object's state. The computed hash must be the same when the object state
13+
* is unchanged or is still considered equivalent for cache key purposes, otherwise the hash code must always
14+
* be different for different states.
15+
*
16+
* @return string Hash code.
17+
*/
18+
public function computeHash(): string;
19+
}

src/Connector/ImportConnector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function __construct(
6666
$this->maxFetchAttempts = $maxFetchAttempts;
6767
}
6868

69-
public function fetch(string $source)
69+
public function fetch(DataSource $source)
7070
{
7171
return retry(
7272
$this->maxFetchAttempts,
@@ -77,7 +77,7 @@ function () use ($source) {
7777
);
7878
}
7979

80-
public function fetchAsync(string $source): Promise
80+
public function fetchAsync(DataSource $source): Promise
8181
{
8282
return retryAsync(
8383
$this->maxFetchAttempts,

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

src/Options/EncapsulatedOptions.php

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

src/Porter.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use ScriptFUSION\Porter\Collection\PorterRecords;
1616
use ScriptFUSION\Porter\Collection\ProviderRecords;
1717
use ScriptFUSION\Porter\Collection\RecordCollection;
18-
use ScriptFUSION\Porter\Connector\ConnectorOptions;
1918
use ScriptFUSION\Porter\Connector\ImportConnectorFactory;
2019
use ScriptFUSION\Porter\Provider\AsyncProvider;
2120
use ScriptFUSION\Porter\Provider\ObjectNotCreatedException;
@@ -119,14 +118,6 @@ private function fetch(ImportSpecification $specification): \Iterator
119118

120119
$connector = $provider->getConnector();
121120

122-
/* __clone method cannot be specified in interface due to Mockery limitation.
123-
See https://github.com/mockery/mockery/issues/669 */
124-
if ($connector instanceof ConnectorOptions && !method_exists($connector, '__clone')) {
125-
throw new CloneNotImplementedException(
126-
'Connector with options must implement __clone() method to deep clone options.'
127-
);
128-
}
129-
130121
return $resource->fetch(ImportConnectorFactory::create($connector, $specification));
131122
}
132123

@@ -198,14 +189,6 @@ private function fetchAsync(AsyncImportSpecification $specification): Iterator
198189

199190
$connector = $provider->getAsyncConnector();
200191

201-
/* __clone method cannot be specified in interface due to Mockery limitation.
202-
See https://github.com/mockery/mockery/issues/669 */
203-
if ($connector instanceof ConnectorOptions && !method_exists($connector, '__clone')) {
204-
throw new CloneNotImplementedException(
205-
'Connector with options must implement __clone() method to deep clone options.'
206-
);
207-
}
208-
209192
return $resource->fetchAsync(ImportConnectorFactory::create($connector, $specification));
210193
}
211194

src/Provider/Resource/ProviderResource.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ interface ProviderResource
1818
public function getProviderClassName(): string;
1919

2020
/**
21-
* Fetches data from the provider using the the specified connector and presents its data as an enumerable series.
21+
* Fetches data from the provider using the the specified connector and presents it as an iterable series.
2222
*
2323
* @param ImportConnector $connector Connector.
2424
*
25-
* @return \Iterator Enumerable data series.
25+
* @return \Iterator Iterable data series.
2626
*/
2727
public function fetch(ImportConnector $connector): \Iterator;
2828
}

src/Provider/Resource/StaticResource.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
namespace ScriptFUSION\Porter\Provider\Resource;
55

66
use ScriptFUSION\Porter\Connector\ImportConnector;
7-
use ScriptFUSION\Porter\Options\EncapsulatedOptions;
87
use ScriptFUSION\Porter\Provider\StaticDataProvider;
98

109
class StaticResource implements ProviderResource
@@ -21,7 +20,7 @@ public function getProviderClassName(): string
2120
return StaticDataProvider::class;
2221
}
2322

24-
public function fetch(ImportConnector $connector, EncapsulatedOptions $options = null): \Iterator
23+
public function fetch(ImportConnector $connector): \Iterator
2524
{
2625
return $this->data;
2726
}

0 commit comments

Comments
 (0)