Skip to content

Commit 61723d2

Browse files
authored
Allow new processors to be passed through configuration. (#11)
1 parent 656e398 commit 61723d2

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/FileFetcher.php

+36-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ protected function __construct(string $identifier, $storage, array $config = nul
2727
{
2828
parent::__construct($identifier, $storage, $config);
2929

30-
$config = $this->validateConfig($config);
30+
$this->setProcessors($config);
3131

32-
$this->processors = self::getProcessors();
32+
$config = $this->validateConfig($config);
3333

3434
// [State]
3535

@@ -134,4 +134,38 @@ private function validateConfig($config): array
134134
}
135135
return $config;
136136
}
137+
138+
private function setProcessors($config)
139+
{
140+
$this->processors = self::getProcessors();
141+
142+
if (!isset($config['processors'])) {
143+
return;
144+
}
145+
146+
if (!is_array($config['processors'])) {
147+
return;
148+
}
149+
150+
foreach ($config['processors'] as $processorClass) {
151+
$this->setProcessor($processorClass);
152+
}
153+
154+
$this->processors = array_merge($this->processors, self::getProcessors());
155+
}
156+
157+
private function setProcessor($processorClass)
158+
{
159+
if (!class_exists($processorClass)) {
160+
return;
161+
}
162+
163+
$classes = class_implements($processorClass);
164+
if (!in_array(ProcessorInterface::class, $classes)) {
165+
return;
166+
}
167+
168+
$instance = new $processorClass();
169+
$this->processors = array_merge([$processorClass => $instance], $this->processors);
170+
}
137171
}

test/FileFetcherTest.php

+14-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Contracts\Mock\Storage\Memory;
66
use FileFetcher\FileFetcher;
7+
use FileFetcher\Processor\Local;
8+
use PHPUnit\Framework\TestCase;
79
use Procrastinator\Result;
810

911
class FileFetcherTest extends \PHPUnit\Framework\TestCase
@@ -18,7 +20,8 @@ public function testRemote()
1820
"1",
1921
new Memory(),
2022
[
21-
"filePath" => "http://samplecsvs.s3.amazonaws.com/Sacramentorealestatetransactions.csv"
23+
"filePath" => "http://samplecsvs.s3.amazonaws.com/Sacramentorealestatetransactions.csv",
24+
"processors" => [Local::class]
2225
]
2326
);
2427

@@ -36,12 +39,15 @@ public function testLocal()
3639
{
3740
$local_file = __DIR__ . "/files/tiny.csv";
3841

42+
$config = [
43+
"filePath" => $local_file,
44+
"processors" => [TestCase::class]
45+
];
46+
3947
$fetcher = FileFetcher::get(
4048
"1",
4149
new Memory(),
42-
[
43-
"filePath" => $local_file
44-
]
50+
$config
4551
);
4652

4753
$fetcher->setTimeLimit(1);
@@ -64,7 +70,8 @@ public function testTimeOut()
6470
{
6571
$store = new Memory();
6672
$config = [
67-
"filePath" => "https://dkan-default-content-files.s3.amazonaws.com/{$this->sampleCsvSize}_mb_sample.csv"
73+
"filePath" => "https://dkan-default-content-files.s3.amazonaws.com/{$this->sampleCsvSize}_mb_sample.csv",
74+
"processors" => "Bad"
6875
];
6976

7077
$fetcher = FileFetcher::get("1", $store, $config);
@@ -102,7 +109,8 @@ public function testIncompatibleServer()
102109
"1",
103110
new Memory(),
104111
[
105-
"filePath" => $url
112+
"filePath" => $url,
113+
"processors" => ["Bad"]
106114
]
107115
);
108116
$fetcher->setTimeLimit(1);

0 commit comments

Comments
 (0)