-
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathImportConnector.php
More file actions
203 lines (175 loc) · 6.62 KB
/
ImportConnector.php
File metadata and controls
203 lines (175 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
declare(strict_types=1);
namespace ScriptFUSION\Porter\Connector;
use Amp\Promise;
use ScriptFUSION\Porter\Cache\CacheUnavailableException;
use ScriptFUSION\Porter\Connector\Recoverable\RecoverableExceptionHandler;
use ScriptFUSION\Porter\Connector\Recoverable\StatelessRecoverableExceptionHandler;
use ScriptFUSION\Porter\ExceptionDescriptor;
/**
* Connector whose lifecycle is synchronised with an import operation. Ensures correct ConnectionContext is delivered
* to the wrapped connector.
*
* Do not store references to this connector that would prevent it expiring when an import operation ends.
*
* @internal Do not create instances of this class in client code.
*/
final class ImportConnector implements ConnectorWrapper
{
private $connector;
private $connectionContext;
/**
* User-defined exception handler called when a recoverable exception is thrown by Connector::fetch().
*
* @var RecoverableExceptionHandler
*/
private $userExceptionHandler;
/**
* Resource-defined exception handler called when a recoverable exception is thrown by Connector::fetch().
*
* @var RecoverableExceptionHandler
*/
private $resourceExceptionHandler;
private $maxFetchAttempts;
/**
* @var ExceptionDescriptor[]
*/
private $recoverableExceptionDescriptors = [];
/**
* @param Connector|AsyncConnector $connector Wrapped connector.
* @param ConnectionContext $connectionContext Connection context.
* @param RecoverableExceptionHandler $recoverableExceptionHandler User's recoverable exception handler.
* @param int $maxFetchAttempts
*/
public function __construct(
$connector,
ConnectionContext $connectionContext,
RecoverableExceptionHandler $recoverableExceptionHandler,
int $maxFetchAttempts
) {
if ($connectionContext->mustCache() && !$connector instanceof CachingConnector) {
throw CacheUnavailableException::createUnsupported();
}
$this->connector = clone $connector;
$this->connectionContext = $connectionContext;
$this->userExceptionHandler = $recoverableExceptionHandler;
$this->maxFetchAttempts = $maxFetchAttempts;
}
public function fetch(string $source)
{
return \ScriptFUSION\Retry\retry(
$this->maxFetchAttempts,
function () use ($source) {
return $this->connector->fetch($source, $this->connectionContext);
},
$this->createExceptionHandler()
);
}
public function fetchAsync(string $source): Promise
{
return \ScriptFUSION\Retry\retryAsync(
$this->maxFetchAttempts,
function () use ($source): Promise {
return \Amp\call(
function () use ($source) {
return $this->connector->fetchAsync($source, $this->connectionContext);
}
);
},
$this->createExceptionHandler()
);
}
private function createExceptionHandler(): \Closure
{
$userHandlerCloned = $resourceHandlerCloned = false;
return function (\Exception $exception) use (&$userHandlerCloned, &$resourceHandlerCloned): void {
// Throw exception instead of retrying, if unrecoverable.
if (!$this->isRecoverable($exception)) {
throw $exception;
}
// Call resource's exception handler, if defined.
if ($this->resourceExceptionHandler) {
self::invokeHandler($this->resourceExceptionHandler, $exception, $resourceHandlerCloned);
}
// Call user's exception handler.
self::invokeHandler($this->userExceptionHandler, $exception, $userHandlerCloned);
};
}
private function isRecoverable(\Exception $exception): bool
{
if ($exception instanceof RecoverableConnectorException) {
return true;
}
foreach ($this->recoverableExceptionDescriptors as $exceptionDescriptor) {
if ($exceptionDescriptor->matches($exception)) {
return true;
}
}
return false;
}
/**
* Invokes the specified fetch exception handler, cloning it if required.
*
* @param RecoverableExceptionHandler $handler Fetch exception handler.
* @param \Exception $exception Exception to pass to the handler.
* @param bool $cloned False if handler requires cloning, true if handler has already been cloned.
*/
private static function invokeHandler(
RecoverableExceptionHandler &$handler,
\Exception $exception,
bool &$cloned
): void {
if (!$cloned && !$handler instanceof StatelessRecoverableExceptionHandler) {
$handler = clone $handler;
$handler->initialize();
$cloned = true;
}
$handler($exception);
}
/**
* Gets the wrapped connector. Useful for resources to reconfigure connector options during this import.
*
* @return Connector|AsyncConnector Wrapped connector.
*/
public function getWrappedConnector()
{
return $this->connector;
}
/**
* Finds the base connector by traversing the stack of wrapped connectors.
*
* @return Connector|AsyncConnector Base connector.
*/
public function findBaseConnector()
{
$connector = $this->connector;
while ($connector instanceof ConnectorWrapper) {
$connector = $connector->getWrappedConnector();
}
return $connector;
}
/**
* Sets an exception handler to be called when a recoverable exception is thrown by Connector::fetch().
*
* The handler is intended to be set by provider resources, once only, and is called before the user-defined
* handler.
*
* @param RecoverableExceptionHandler $recoverableExceptionHandler Recoverable exception handler.
*/
public function setRecoverableExceptionHandler(RecoverableExceptionHandler $recoverableExceptionHandler): void
{
if ($this->resourceExceptionHandler !== null) {
throw new \LogicException('Cannot set resource\'s recoverable exception handler: already set!');
}
$this->resourceExceptionHandler = $recoverableExceptionHandler;
}
/**
* Adds the specified exception descriptor, designating it as a recoverable exception type.
*
* @param ExceptionDescriptor $descriptor
*/
public function addRecoverableExceptionDescriptor(ExceptionDescriptor $descriptor): void
{
$this->recoverableExceptionDescriptors[] = $descriptor;
}
}