Completed
Push — master ( 3d8aa7...80558b )
by Timo
02:25
created

ThrottleMiddleware::_requestHandler()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 1
nop 3
dl 0
loc 13
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace hamburgscleanest\GuzzleAdvancedThrottle\Middleware;
4
5
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Interfaces\CacheStrategy;
6
use hamburgscleanest\GuzzleAdvancedThrottle\RequestLimitRuleset;
7
use Psr\Http\Message\RequestInterface;
8
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
9
10
/**
11
 * Class ThrottleMiddleware
12
 * @package hamburgscleanest\GuzzleAdvancedThrottle\Middleware
13
 */
14
class ThrottleMiddleware
15
{
16
17
    /** @var \hamburgscleanest\GuzzleAdvancedThrottle\RequestLimitGroup */
18
    private $_requestLimitGroup;
19
    /** @var CacheStrategy|null */
20
    private $_cacheStrategy;
21
22
    /**
23
     * ThrottleMiddleware constructor.
24
     * @param RequestLimitRuleset $requestLimitRuleset
25
     * @param CacheStrategy|null $cacheStrategy
26
     * @throws \Exception
27
     */
28 4
    public function __construct(RequestLimitRuleset $requestLimitRuleset, CacheStrategy $cacheStrategy = null)
29
    {
30 4
        $this->_requestLimitGroup = $requestLimitRuleset->getRequestLimitGroup();
31 4
        $this->_cacheStrategy = $cacheStrategy;
32 4
    }
33
34
    /**
35
     * @return callable
36
     * @throws \Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException
37
     * @throws \Exception
38
     */
39
    public function handle() : callable
40
    {
41
        return function(callable $handler) : callable
42
        {
43 4
            return function(RequestInterface $request, array $options) use ($handler)
44
            {
45 4
                if ($this->_cacheStrategy !== null)
46
                {
47 3
                    return $this->_cacheStrategy->request($request, $this->_requestHandler($handler, $request, $options));
48
                }
49
50 1
                return $this->_requestHandler($handler, $request, $options)();
51 4
            };
52 4
        };
53
    }
54
55
    /**
56
     * @param callable $handler
57
     * @param RequestInterface $request
58
     * @param array $options
59
     * @return callable
60
     * @throws \Exception
61
     */
62
    private function _requestHandler(callable $handler, RequestInterface $request, array $options) : callable
63
    {
64 4
        return function() use ($handler, $request, $options)
65
        {
66 4
            if (!$this->_requestLimitGroup->canRequest())
67
            {
68 3
                throw new TooManyRequestsHttpException(
69 3
                    $this->_requestLimitGroup->getRetryAfter(),
70 3
                    'The rate limit was exceeded. Please try again later.'
71
                );
72
            }
73
74 4
            return $handler($request, $options);
75 4
        };
76
    }
77
}