-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
ImportSpecification.php
264 lines (222 loc) · 5.67 KB
/
ImportSpecification.php
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
namespace ScriptFUSION\Porter\Specification;
use ScriptFUSION\Porter\Connector\FetchExceptionHandler\ExponentialSleepFetchExceptionHandler;
use ScriptFUSION\Porter\Connector\FetchExceptionHandler\FetchExceptionHandler;
use ScriptFUSION\Porter\Provider\Resource\ProviderResource;
use ScriptFUSION\Porter\Transform\Transformer;
/**
* Specifies which resource to import and how the data should be transformed.
*/
class ImportSpecification
{
const DEFAULT_FETCH_ATTEMPTS = 5;
/**
* @var ProviderResource
*/
private $resource;
/**
* @var string
*/
private $providerName;
/**
* @var Transformer[]
*/
private $transformers;
/**
* @var mixed
*/
private $context;
/**
* @var bool
*/
private $mustCache = false;
/**
* @var int
*/
private $maxFetchAttempts = self::DEFAULT_FETCH_ATTEMPTS;
/**
* @var FetchExceptionHandler
*/
private $fetchExceptionHandler;
public function __construct(ProviderResource $resource)
{
$this->resource = $resource;
$this->clearTransformers();
}
public function __clone()
{
$this->resource = clone $this->resource;
$transformers = $this->transformers;
$this->clearTransformers()->addTransformers(array_map(
function (Transformer $transformer) {
return clone $transformer;
},
$transformers
));
is_object($this->context) && $this->context = clone $this->context;
$this->fetchExceptionHandler && $this->fetchExceptionHandler = clone $this->fetchExceptionHandler;
}
/**
* @return ProviderResource
*/
final public function getResource()
{
return $this->resource;
}
/**
* Gets the provider service name.
*
* @return string Provider name.
*/
final public function getProviderName()
{
return $this->providerName;
}
/**
* Sets the provider service name.
*
* @param string $providerName Provider name.
*
* @return $this
*/
final public function setProviderName($providerName)
{
$this->providerName = "$providerName";
return $this;
}
/**
* Gets the ordered list of transformers.
*
* @return Transformer[]
*/
final public function getTransformers()
{
return $this->transformers;
}
/**
* Adds the specified transformer.
*
* @param Transformer $transformer Transformer.
*
* @return $this
*/
final public function addTransformer(Transformer $transformer)
{
if ($this->hasTransformer($transformer)) {
throw new DuplicateTransformerException('Transformer already added.');
}
$this->transformers[spl_object_hash($transformer)] = $transformer;
return $this;
}
/**
* Adds one or more transformers.
*
* @param Transformer[] $transformers Transformers.
*
* @return $this
*/
final public function addTransformers(array $transformers)
{
foreach ($transformers as $transformer) {
$this->addTransformer($transformer);
}
return $this;
}
/**
* @return $this
*/
final public function clearTransformers()
{
$this->transformers = [];
return $this;
}
private function hasTransformer(Transformer $transformer)
{
return isset($this->transformers[spl_object_hash($transformer)]);
}
/**
* @return mixed
*/
final public function getContext()
{
return $this->context;
}
/**
* @param mixed $context
*
* @return $this
*/
final public function setContext($context)
{
$this->context = $context;
return $this;
}
/**
* @return bool
*/
final public function mustCache()
{
return $this->mustCache;
}
/**
* @return $this
*/
final public function enableCache()
{
$this->mustCache = true;
return $this;
}
/**
* @return $this
*/
final public function disableCache()
{
$this->mustCache = false;
return $this;
}
/**
* Gets the maximum number of fetch attempts per connection.
*
* @return int Maximum fetch attempts.
*/
final public function getMaxFetchAttempts()
{
return $this->maxFetchAttempts;
}
/**
* Sets the maximum number of fetch attempts per connection before failure is considered permanent.
*
* @param int $attempts Maximum fetch attempts.
*
* @return $this
*/
final public function setMaxFetchAttempts($attempts)
{
if (!is_int($attempts) || $attempts < 1) {
throw new \InvalidArgumentException('Fetch attempts must be greater than or equal to 1.');
}
$this->maxFetchAttempts = $attempts;
return $this;
}
/**
* Gets the exception handler invoked each time a fetch attempt fails.
*
* @return FetchExceptionHandler Fetch exception handler.
*/
final public function getFetchExceptionHandler()
{
return $this->fetchExceptionHandler ?: $this->fetchExceptionHandler = new ExponentialSleepFetchExceptionHandler;
}
/**
* Sets the exception handler invoked each time a fetch attempt fails.
*
* @param FetchExceptionHandler $fetchExceptionHandler Fetch exception handler.
*
* @return $this
*/
final public function setFetchExceptionHandler(FetchExceptionHandler $fetchExceptionHandler)
{
$this->fetchExceptionHandler = $fetchExceptionHandler;
return $this;
}
}