Skip to content

Commit 0b44d07

Browse files
committed
Added docblock documentation to Transformer.
Split transformation in Porter into separate method.
1 parent 378c8d7 commit 0b44d07

File tree

2 files changed

+49
-28
lines changed

2 files changed

+49
-28
lines changed

src/Porter.php

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use ScriptFUSION\Porter\Provider\ProviderFactory;
1616
use ScriptFUSION\Porter\Provider\Resource\ProviderResource;
1717
use ScriptFUSION\Porter\Specification\ImportSpecification;
18+
use ScriptFUSION\Porter\Transform\Transformer;
1819
use ScriptFUSION\Retry\ExceptionHandler\ExponentialBackoffExceptionHandler;
1920

2021
/**
@@ -72,13 +73,7 @@ public function import(ImportSpecification $specification)
7273
$records = $this->createProviderRecords($records, $specification->getResource());
7374
}
7475

75-
foreach ($specification->getTransformers() as $transformer) {
76-
if ($transformer instanceof PorterAware) {
77-
$transformer->setPorter($this);
78-
}
79-
80-
$records = $transformer->transform($records, $specification->getContext());
81-
}
76+
$records = $this->transformRecords($records, $specification->getTransformers(), $specification->getContext());
8277

8378
return $this->createPorterRecords($records, $specification);
8479
}
@@ -109,27 +104,10 @@ public function importOne(ImportSpecification $specification)
109104
return $one;
110105
}
111106

112-
private function createProviderRecords(\Iterator $records, ProviderResource $resource)
113-
{
114-
if ($records instanceof \Countable) {
115-
return new CountableProviderRecords($records, count($records), $resource);
116-
}
117-
118-
return new ProviderRecords($records, $resource);
119-
}
120-
121-
private function createPorterRecords(RecordCollection $records, ImportSpecification $specification)
122-
{
123-
if ($records instanceof \Countable) {
124-
return new CountablePorterRecords($records, count($records), $specification);
125-
}
126-
127-
return new PorterRecords($records, $specification);
128-
}
129-
130107
private function fetch(ProviderResource $resource, CacheAdvice $cacheAdvice = null)
131108
{
132109
$provider = $this->getProvider($resource->getProviderClassName(), $resource->getProviderTag());
110+
133111
$this->applyCacheAdvice($provider, $cacheAdvice ?: $this->defaultCacheAdvice);
134112

135113
if (($records = \ScriptFUSION\Retry\retry(
@@ -152,6 +130,44 @@ function (\Exception $exception) {
152130
throw new ImportException(get_class($provider) . '::fetch() did not return an Iterator.');
153131
}
154132

133+
/**
134+
* @param RecordCollection $records
135+
* @param Transformer[] $transformers
136+
* @param mixed $context
137+
*
138+
* @return RecordCollection
139+
*/
140+
private function transformRecords(RecordCollection $records, array $transformers, $context)
141+
{
142+
foreach ($transformers as $transformer) {
143+
if ($transformer instanceof PorterAware) {
144+
$transformer->setPorter($this);
145+
}
146+
147+
$records = $transformer->transform($records, $context);
148+
}
149+
150+
return $records;
151+
}
152+
153+
private function createProviderRecords(\Iterator $records, ProviderResource $resource)
154+
{
155+
if ($records instanceof \Countable) {
156+
return new CountableProviderRecords($records, count($records), $resource);
157+
}
158+
159+
return new ProviderRecords($records, $resource);
160+
}
161+
162+
private function createPorterRecords(RecordCollection $records, ImportSpecification $specification)
163+
{
164+
if ($records instanceof \Countable) {
165+
return new CountablePorterRecords($records, count($records), $specification);
166+
}
167+
168+
return new PorterRecords($records, $specification);
169+
}
170+
155171
private function applyCacheAdvice(Provider $provider, CacheAdvice $cacheAdvice)
156172
{
157173
try {
@@ -223,7 +239,7 @@ public function getProvider($name, $tag = null)
223239
return $provider;
224240
}
225241
} catch (ObjectNotCreatedException $exception) {
226-
// Intentionally empty.
242+
// We will throw our own exception.
227243
}
228244

229245
throw new ProviderNotFoundException(

src/Transform/Transformer.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33

44
use ScriptFUSION\Porter\Collection\RecordCollection;
55

6+
/**
7+
* Provides a method to transform imported data.
8+
*/
69
interface Transformer
710
{
811
/**
9-
* @param RecordCollection $records
10-
* @param mixed $context
12+
* Transforms the specified record collection decorated with the specified context data.
13+
*
14+
* @param RecordCollection $records Record collection.
15+
* @param mixed $context Context data.
1116
*
1217
* @return RecordCollection
1318
*/

0 commit comments

Comments
 (0)