Skip to content

Commit 53fd56d

Browse files
committed
Add request conditional plugin
Applied fixes from StyleCI
1 parent 741f0c4 commit 53fd56d

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"php-http/httplug": "^1.0",
1616
"php-http/message-factory": "^1.0.2",
1717
"php-http/client-common": "^1.0",
18-
"php-http/message": "^1.0",
18+
"php-http/message": "^1.1",
1919
"symfony/options-resolver": "^2.6|^3.0"
2020
},
2121
"require-dev": {

spec/RequestConditionalPluginSpec.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Plugin;
4+
5+
use Http\Client\Plugin\Plugin;
6+
use Http\Message\RequestMatcher;
7+
use Http\Promise\Promise;
8+
use Psr\Http\Message\RequestInterface;
9+
use PhpSpec\ObjectBehavior;
10+
use Prophecy\Argument;
11+
12+
class RequestConditionalPluginSpec extends ObjectBehavior
13+
{
14+
function let(RequestMatcher $requestMatcher, Plugin $plugin)
15+
{
16+
$this->beConstructedWith($requestMatcher, $plugin);
17+
}
18+
19+
function it_is_initializable()
20+
{
21+
$this->shouldHaveType('Http\Client\Plugin\RequestConditionalPlugin');
22+
}
23+
24+
function it_is_a_plugin()
25+
{
26+
$this->shouldImplement('Http\Client\Plugin\Plugin');
27+
}
28+
29+
function it_matches_a_request_and_delegates_to_plugin(
30+
RequestInterface $request,
31+
RequestMatcher $requestMatcher,
32+
Plugin $plugin
33+
) {
34+
$requestMatcher->matches($request)->willReturn(true);
35+
$plugin->handleRequest($request, Argument::type('callable'), Argument::type('callable'))->shouldBeCalled();
36+
37+
$this->handleRequest($request, function () {}, function () {});
38+
}
39+
40+
function it_does_not_match_a_request(
41+
RequestInterface $request,
42+
RequestMatcher $requestMatcher,
43+
Plugin $plugin,
44+
Promise $promise
45+
) {
46+
$requestMatcher->matches($request)->willReturn(false);
47+
$plugin->handleRequest($request, Argument::type('callable'), Argument::type('callable'))->shouldNotBeCalled();
48+
49+
$next = function (RequestInterface $request) use($promise) {
50+
return $promise->getWrappedObject();
51+
};
52+
53+
$this->handleRequest($request, $next, function () {})->shouldReturn($promise);
54+
}
55+
}

src/RequestConditionalPlugin.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Http\Client\Plugin;
4+
5+
use Http\Message\RequestMatcher;
6+
use Psr\Http\Message\RequestInterface;
7+
8+
/**
9+
* Apply a delegated plugin based on a request match.
10+
*
11+
* @author Márk Sági-Kazár <[email protected]>
12+
*/
13+
final class RequestConditionalPlugin implements Plugin
14+
{
15+
/**
16+
* @var RequestMatcher
17+
*/
18+
private $requestMatcher;
19+
20+
/**
21+
* @var Plugin
22+
*/
23+
private $delegatedPlugin;
24+
25+
/**
26+
* @param RequestMatcher $requestMatcher
27+
* @param Plugin $delegatedPlugin
28+
*/
29+
public function __construct(RequestMatcher $requestMatcher, Plugin $delegatedPlugin)
30+
{
31+
$this->requestMatcher = $requestMatcher;
32+
$this->delegatedPlugin = $delegatedPlugin;
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
39+
{
40+
if ($this->requestMatcher->matches($request)) {
41+
return $this->delegatedPlugin->handleRequest($request, $next, $first);
42+
}
43+
44+
return $next($request);
45+
}
46+
}

0 commit comments

Comments
 (0)