Completed
Push — master ( f43e52...716ed5 )
by Nikola
10s
created

createOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the Rate Limit package.
4
 *
5
 * Copyright (c) Nikola Posa
6
 *
7
 * For full copyright and license information, please refer to the LICENSE file,
8
 * located at the package root folder.
9
 */
10
11
declare(strict_types=1);
12
13
namespace RateLimit;
14
15
use RateLimit\Options\RequestsPerWindowOptions;
16
use Psr\Http\Message\RequestInterface;
17
use Psr\Http\Message\ResponseInterface;
18
use RateLimit\Identity\IpAddressIdentityGenerator;
19
use RateLimit\Storage\InMemoryStorage;
20
21
/**
22
 * @author Nikola Posa <[email protected]>
23
 */
24
final class RequestsPerWindowRateLimiterFactory
25
{
26
    const DEFAULT_LIMIT = 100;
27
    const DEFAULT_WINDOW = 15 * 60;
28
29 6
    public static function createInMemoryRateLimiter(array $options = []) : RequestsPerWindowRateLimiter
30
    {
31 6
        return new RequestsPerWindowRateLimiter(
32 6
            new InMemoryStorage(),
33 6
            new IpAddressIdentityGenerator(),
34 6
            self::createOptions($options)
35
        );
36
    }
37
38 6
    public static function createOptions(array $options = []) : RequestsPerWindowOptions
39
    {
40 6
        $options = array_merge(self::getDefaultOptions(), $options);
41
42 6
        return new RequestsPerWindowOptions(
43 6
            $options['limit'],
44 6
            $options['window'],
45 6
            $options['limitExceededHandler']
46
        );
47
    }
48 6
    private static function getDefaultOptions() : array
49
    {
50
        return [
51 6
            'limit' => self::DEFAULT_LIMIT,
52 6
            'window' => self::DEFAULT_WINDOW,
53 6
            'limitExceededHandler' => function (RequestInterface $request, ResponseInterface $response) {
54 2
                return $response;
55 6
            },
56
        ];
57
    }
58
}
59