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
}

0 commit comments

Comments
 (0)