summaryrefslogtreecommitdiff
path: root/Postman/Postman-Mail/mailgun/vendor/php-http/client-common/src/Plugin/RequestMatcherPlugin.php
blob: 5f72b02d8ac6973ba659ab9d4bf1f7f1eaf97eba (plain)
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
<?php

namespace Http\Client\Common\Plugin;

use Http\Client\Common\Plugin;
use Http\Message\RequestMatcher;
use Psr\Http\Message\RequestInterface;

/**
 * Apply a delegated plugin based on a request match.
 *
 * @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
 */
final class RequestMatcherPlugin implements Plugin
{
    /**
     * @var RequestMatcher
     */
    private $requestMatcher;

    /**
     * @var Plugin
     */
    private $delegatedPlugin;

    /**
     * @param RequestMatcher $requestMatcher
     * @param Plugin         $delegatedPlugin
     */
    public function __construct(RequestMatcher $requestMatcher, Plugin $delegatedPlugin)
    {
        $this->requestMatcher = $requestMatcher;
        $this->delegatedPlugin = $delegatedPlugin;
    }

    /**
     * {@inheritdoc}
     */
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
    {
        if ($this->requestMatcher->matches($request)) {
            return $this->delegatedPlugin->handleRequest($request, $next, $first);
        }

        return $next($request);
    }
}