Skip to content

Commit 53549d6

Browse files
ballercatroippi
authored andcommitted
BREAKING: Change prepareRequest to a reducer (#10)
* Change prepareRequest to a reducer * tweak api, match spec exactly * typehint prepareRequest * Revert "typehint prepareRequest" This reverts commit 79ce391. * naming, fix a few phpcs warnings
1 parent 9112a8b commit 53549d6

File tree

5 files changed

+27
-17
lines changed

5 files changed

+27
-17
lines changed

src/Renderer.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,21 +245,24 @@ protected function createJobs()
245245
}
246246

247247
/**
248-
* @param $jobs
248+
* Prepare Request
249+
*
250+
* @param array $jobs Jobs
251+
*
249252
* @return array
250253
*/
251254
protected function prepareRequest($jobs)
252255
{
253-
$preparedJobs = array_map(function ($job) {
254-
foreach ($this->plugins as $plugin) {
255-
$job = $plugin->prepareRequest($job);
256-
}
257-
return $job;
258-
}, $jobs);
256+
$preparedJobs = $jobs;
257+
foreach ($this->plugins as $plugin) {
258+
// Pass both jobs we are working with an original, incoming jobs so
259+
// that every plugin has a chance to see _all_ original jobs.
260+
$preparedJobs = $plugin->prepareRequest($preparedJobs, $jobs);
261+
}
259262

260263
$shouldSend = true;
261264
foreach ($this->plugins as $plugin) {
262-
$shouldSend = $shouldSend && $plugin->shouldSendRequest($jobs);
265+
$shouldSend = $shouldSend && $plugin->shouldSendRequest($preparedJobs);
263266
}
264267

265268
return [$shouldSend, $preparedJobs];

src/plugins/BasePlugin.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class BasePlugin implements Plugin
1414
/**
1515
* {@inheritdoc}
1616
*/
17-
public function prepareRequest($request)
17+
public function prepareRequest(array $jobs, array $originalJobs)
1818
{
19-
return $request;
19+
return $jobs;
2020
}
2121

2222
/**

src/plugins/Plugin.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ interface Plugin
2020
public function getViewData($name, array $data);
2121

2222
/**
23-
* @param \WF\Hypernova\Job $request
23+
* @param \WF\Hypernova\Job[] $jobs
24+
* @param \WF\Hypernova\Job[] $originalJobs
2425
* @return \WF\Hypernova\Job
2526
*/
26-
public function prepareRequest($request);
27+
public function prepareRequest(array $jobs, array $originalJobs);
2728

2829
/**
2930
* @param \WF\Hypernova\Job[] $jobs

tests/BasePluginTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ public function testPrepareRequest()
1919
$plugin = new BasePlugin();
2020

2121
$job = Job::fromArray(['name' => 'foo', 'data' => ['bar' => 'baz']]);
22+
$jobs = [$job];
2223

23-
$this->assertEquals($job, $plugin->prepareRequest($job));
24+
$this->assertEquals($jobs, $plugin->prepareRequest($jobs, [$jobs]));
2425
}
2526

2627
public function testOnError()
@@ -80,4 +81,4 @@ private function makeJob()
8081
{
8182
return Job::fromArray(['name' => 'foo', 'data' => []]);
8283
}
83-
}
84+
}

tests/RendererTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ public function testPrepareRequestCallsPlugin()
104104

105105
$plugin->expects($this->exactly(2))
106106
->method('prepareRequest')
107-
->with($this->equalTo($this->defaultJob))
108-
->willReturn($this->defaultJob);
107+
->with($this->equalTo([$this->defaultJob]))
108+
->willReturn([$this->defaultJob]);
109109

110110
$this->renderer->addPlugin($plugin);
111111
$this->renderer->addPlugin($plugin);
@@ -120,6 +120,9 @@ public function testShouldSend()
120120
$pluginDontSend = $this->createMock(BasePlugin::class);
121121
$pluginDoSend = $this->createMock(BasePlugin::class);
122122

123+
$pluginDoSend->method('prepareRequest')->will($this->returnArgument(0));
124+
$pluginDontSend->method('prepareRequest')->will($this->returnArgument(0));
125+
123126
$pluginDontSend->expects($this->once())
124127
->method('shouldSendRequest')
125128
->willReturn(false);
@@ -130,7 +133,9 @@ public function testShouldSend()
130133
$this->renderer->addPlugin($pluginDontSend);
131134
$this->renderer->addPlugin($pluginDoSend);
132135

133-
$this->assertFalse($this->callInternalMethodOfThing($this->renderer, 'prepareRequest', [[$this->defaultJob]])[0]);
136+
$result = $this->callInternalMethodOfThing($this->renderer, 'prepareRequest', [[$this->defaultJob]]);
137+
$this->assertEquals([$this->defaultJob], $result[1]);
138+
$this->assertFalse($result[0]);
134139
}
135140

136141
public function testRenderShouldNotSend()

0 commit comments

Comments
 (0)