Skip to content

Commit 0a80812

Browse files
committed
Removed EncapsulatedOptions parameter from Connector::fetch method.
Removed ProviderOptions. Added ConnectorOptions interface and accompanying trait. Forced ConnectorOptions to implement __clone in Porter. These changes stop passing EncapsulatedOptions around in method calls, instead tieing them to their Connector implementation. This makes sense because connectors have a 1:1 relationship with their options. That is, different options cannot be used with different connectors. To make connectors and their options immutable during import, connectors are now cloned in Porter::fetch() and connectors exporting options must implement __clone() to deep clone their options.
1 parent 653774c commit 0a80812

16 files changed

Lines changed: 110 additions & 113 deletions

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ php:
1010
- 5.6
1111
- 7.0
1212
- 7.1
13+
- 7.2
1314

1415
env:
1516
matrix:
@@ -21,7 +22,7 @@ matrix:
2122

2223
cache:
2324
directories:
24-
- .composer/cache
25+
- vendor
2526

2627
install:
2728
- alias composer=composer\ -n && composer selfupdate

src/Collection/RecordCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ abstract class RecordCollection implements \Iterator
1010

1111
private $previousCollection;
1212

13-
public function __construct(\Iterator $records, RecordCollection $previousCollection = null)
13+
public function __construct(\Iterator $records, self $previousCollection = null)
1414
{
1515
$this->records = $records;
1616
$this->previousCollection = $previousCollection;

src/Connector/CachingConnector.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use ScriptFUSION\Porter\Cache\InvalidCacheKeyException;
77
use ScriptFUSION\Porter\Cache\JsonCacheKeyGenerator;
88
use ScriptFUSION\Porter\Cache\MemoryCache;
9-
use ScriptFUSION\Porter\Options\EncapsulatedOptions;
109

1110
/**
1211
* Wraps a connector to cache fetched data using PSR-6-compliant objects.
@@ -39,28 +38,27 @@ public function __construct(
3938
}
4039

4140
/**
41+
* @param ConnectionContext $context
4242
* @param string $source
43-
* @param EncapsulatedOptions|null $options
4443
*
4544
* @return mixed
4645
*
4746
* @throws InvalidCacheKeyException
4847
*/
49-
public function fetch(ConnectionContext $context, $source, EncapsulatedOptions $options = null)
48+
public function fetch(ConnectionContext $context, $source)
5049
{
5150
if ($context->mustCache()) {
52-
$optionsCopy = $options ? $options->copy() : [];
51+
$options = $this->connector instanceof ConnectorOptions ? $this->connector->getOptions()->copy() : [];
52+
ksort($options);
5353

54-
ksort($optionsCopy);
55-
56-
$this->validateCacheKey($key = $this->cacheKeyGenerator->generateCacheKey($source, $optionsCopy));
54+
$this->validateCacheKey($key = $this->cacheKeyGenerator->generateCacheKey($source, $options));
5755

5856
if ($this->cache->hasItem($key)) {
5957
return $this->cache->getItem($key)->get();
6058
}
6159
}
6260

63-
$data = $this->connector->fetch($context, $source, $options);
61+
$data = $this->connector->fetch($context, $source);
6462

6563
isset($key) && $this->cache->save($this->cache->getItem($key)->set($data));
6664

src/Connector/Connector.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22
namespace ScriptFUSION\Porter\Connector;
33

4-
use ScriptFUSION\Porter\Options\EncapsulatedOptions;
5-
64
/**
75
* Provides a method for fetching data from a remote source.
86
*/
@@ -13,9 +11,8 @@ interface Connector
1311
*
1412
* @param ConnectionContext $context Runtime connection settings and methods.
1513
* @param string $source Source.
16-
* @param EncapsulatedOptions|null $options Optional. Options.
1714
*
1815
* @return mixed Data.
1916
*/
20-
public function fetch(ConnectionContext $context, $source, EncapsulatedOptions $options = null);
17+
public function fetch(ConnectionContext $context, $source);
2118
}

src/Connector/ConnectorOptions.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace ScriptFUSION\Porter\Connector;
3+
4+
use ScriptFUSION\Porter\Options\EncapsulatedOptions;
5+
6+
interface ConnectorOptions
7+
{
8+
/**
9+
* @return EncapsulatedOptions
10+
*/
11+
public function getOptions();
12+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace ScriptFUSION\Porter\Connector;
3+
4+
use ScriptFUSION\Porter\Options\EncapsulatedOptions;
5+
6+
trait ConnectorOptionsTrait
7+
{
8+
/**
9+
* @var EncapsulatedOptions
10+
*/
11+
private $options;
12+
13+
/**
14+
* @return EncapsulatedOptions
15+
*/
16+
public function getOptions()
17+
{
18+
return $this->options;
19+
}
20+
21+
public function __clone()
22+
{
23+
$this->options = clone $this->options;
24+
}
25+
}

src/Connector/ImportConnector.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
namespace ScriptFUSION\Porter\Connector;
33

44
use ScriptFUSION\Porter\Cache\CacheUnavailableException;
5-
use ScriptFUSION\Porter\Options\EncapsulatedOptions;
65

76
/**
87
* Connector whose lifecycle is synchronised with an import operation. Ensures correct ConnectionContext is delivered
@@ -26,8 +25,8 @@ public function __construct(Connector $connector, ConnectionContext $context)
2625
$this->context = $context;
2726
}
2827

29-
public function fetch($source, EncapsulatedOptions $options = null)
28+
public function fetch($source)
3029
{
31-
return $this->connector->fetch($this->context, $source, $options);
30+
return $this->connector->fetch($this->context, $source);
3231
}
3332
}

src/Connector/NullConnector.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
<?php
22
namespace ScriptFUSION\Porter\Connector;
33

4-
use ScriptFUSION\Porter\Options\EncapsulatedOptions;
5-
64
class NullConnector implements Connector
75
{
8-
public function fetch(ConnectionContext $context, $source, EncapsulatedOptions $options = null)
6+
public function fetch(ConnectionContext $context, $source)
97
{
108
// Intentionally empty.
119
}

src/Porter.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
use ScriptFUSION\Porter\Collection\RecordCollection;
1010
use ScriptFUSION\Porter\Connector\ConnectionContext;
1111
use ScriptFUSION\Porter\Connector\ConnectionContextFactory;
12+
use ScriptFUSION\Porter\Connector\ConnectorOptions;
1213
use ScriptFUSION\Porter\Connector\ImportConnector;
1314
use ScriptFUSION\Porter\Provider\ForeignResourceException;
1415
use ScriptFUSION\Porter\Provider\ObjectNotCreatedException;
1516
use ScriptFUSION\Porter\Provider\Provider;
1617
use ScriptFUSION\Porter\Provider\ProviderFactory;
17-
use ScriptFUSION\Porter\Provider\ProviderOptions;
1818
use ScriptFUSION\Porter\Provider\Resource\ProviderResource;
1919
use ScriptFUSION\Porter\Specification\ImportSpecification;
2020
use ScriptFUSION\Porter\Transform\Transformer;
@@ -109,10 +109,17 @@ private function fetch(ProviderResource $resource, $providerName, ConnectionCont
109109
));
110110
}
111111

112-
$records = $resource->fetch(
113-
new ImportConnector($provider->getConnector(), $context),
114-
$provider instanceof ProviderOptions ? clone $provider->getOptions() : null
115-
);
112+
$connector = $provider->getConnector();
113+
114+
/* __clone method cannot be specified in interface due to Mockery limitation.
115+
See https://github.com/mockery/mockery/issues/669 */
116+
if ($connector instanceof ConnectorOptions && !method_exists($connector, '__clone')) {
117+
throw new \LogicException(
118+
'Connector with options must implement __clone() method to deep clone options.'
119+
);
120+
}
121+
122+
$records = $resource->fetch(new ImportConnector(clone $connector, $context));
116123

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

src/Provider/ProviderOptions.php

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

0 commit comments

Comments
 (0)